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 MyNode(T _value){
value =_value;
rcNode = null;
lcNode = null;
}
public void view(){
//view nodes
for(int i = nodes.size()-1; i >-1; i--){
System.out.println(nodes.get(i));
}
}
}
public class MyBinaryTree>{
MyNode root;
int size =0;
public MyBinaryTree(){
root = null;
}
public void delete(T value){
MyNode parent = null;
MyNode current = root;
while (current != null){
if (value.compareTo(current.value)<0){
parent = current;
current = current.lcNode;
} else if (value.compareTo(current.value)>0){
parent = current;
current = current.rcNode;
} else {
break; // value is in the tree pointed at by current
}
}
// Case 1: current has no lcNode child
if (current.lcNode == null){
// Connect the parent with the rcNode child of the current node
if (parent == null){
root = current.rcNode;
} else {
if (value.compareTo(parent.value)<0){
parent.lcNode = current.rcNode;
} else {
parent.rcNode = current.rcNode;
}
}
} else {
// Case 2: 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 (rcNodeMost.rcNode != 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 (parentOfrcNodeMost.rcNode == rcNodeMost){
parentOfrcNodeMost.rcNode = rcNodeMost.lcNode;
} else // Special case: parentOfrcNodeMost == current
{
parentOfrcNodeMost.lcNode = rcNodeMost.lcNode;
}
}
size--;
}
private MyNode deleteOpenNode(MyNode inNode){
MyNode parent = null;
MyNode current = root;
while (current != null){
if (inNode.value.compareTo(current.value)<0){
parent = current;
current = current.lcNode;
} else if (inNode.value.compareTo(current.value)>0){
parent = current;
current = current.rcNode;
} else {
break; // value is in the tree pointed at by current
}
}
if (current == null){
return current;
}
if (current.lcNode == null && current.rcNode == null){
return current;
}
size--;
return current; //is this the right return element?
}
public MyNode deleteClosedNode(MyNode inNode){
// Locate the node to be deleted and also locate its parent node
MyNode parent = null;
MyNode current = root;
while (current != null){
if (inNode.value.compareTo(current.value)<0){
parent = current;
current = current.lcNode;
} else if (inNode.value.compareTo(current.value)>0){
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 1: current has no left children
if (current.lcNode == null){
// Connect the parent with the right child of the current node
if (parent == null){
root = current.rcNode;
} else {
if (inNode.value.compareTo(parent.value)<0){
parent.lcNode = current.rcNode;
} else {
parent.rcNode = current.rcNode;
}
}
} else {
// Case 2: 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!