Question: In Java * * * Help, I keep getting mixed up on the unfinished methods - any help on those and cleaning up the code
In Java Help, I keep getting mixed up on the unfinished methods any help on those and cleaning up the code is appreciated:
public class MyNode
MyNode rcNode;
MyNode lcNode;
T value;
ArrayList nodes new ArrayList;
public MyNodeT value
value value;
rcNode null;
lcNode null;
public void view
view nodes
forint i nodes.size; i ; i
System.out.printlnnodesgeti;
public class MyBinaryTree
MyNode root;
int size ;
public MyBinaryTree
root null;
public void deleteT value
MyNode parent null;
MyNode current root;
while current null
if valuecompareTocurrentvalue
parent current;
current current.lcNode;
else if valuecompareTocurrentvalue
parent current;
current current.rcNode;
else
break; value is in the tree pointed at by current
Case : current has no lcNode child
if currentlcNode null
Connect the parent with the rcNode child of the current node
if parent null
root current.rcNode;
else
if valuecompareToparentvalue
parent.lcNode current.rcNode;
else
parent.rcNode current.rcNode;
else
Case : The current node has a lcNode child
Locate the rcNodemost node in the lcNode subtree of
the current node and also its parent
MyNode parentOfrcNodeMost current;
MyNode rcNodeMost current.lcNode;
while rcNodeMostrcNode null
parentOfrcNodeMost rcNodeMost;
rcNodeMost rcNodeMost.rcNode; Keep going to the rcNode
Replace the value in current by the value in rcNodeMost
current.value rcNodeMost.value;
Eliminate rcNodemost node
if parentOfrcNodeMostrcNode rcNodeMost
parentOfrcNodeMost.rcNode rcNodeMost.lcNode;
else Special case: parentOfrcNodeMost current
parentOfrcNodeMost.lcNode rcNodeMost.lcNode;
size;
private MyNode deleteOpenNodeMyNode inNode
MyNode parent null;
MyNode current root;
while current null
if inNodevalue.compareTocurrentvalue
parent current;
current current.lcNode;
else if inNodevalue.compareTocurrentvalue
parent current;
current current.rcNode;
else
break; value is in the tree pointed at by current
if current null
return current;
if currentlcNode null && current.rcNode null
return current;
size;
return current; is this the right return element?
public MyNode deleteClosedNodeMyNode inNode
Locate the node to be deleted and also locate its parent node
MyNode parent null;
MyNode current root;
while current null
if inNodevalue.compareTocurrentvalue
parent current;
current current.lcNode;
else if inNodevalue.compareTocurrentvalue
parent current;
current current.rcNode;
else
break; Element is in the tree pointed at by current
if current null
return current; Element is not in the tree
Case : current has no left children
if currentlcNode null
Connect the parent with the right child of the current node
if parent null
root current.rcNode;
else
if inNodevalue.compareToparentvalue
parent.lcNode current.rcNode;
else
parent.rcNode current.rcNode;
else
Case : The current node has a left child
Locate the rightmost node in the left subtree of
the current node and also its parent
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
