I think it starts like this but I’m not sure. Else (current == parent) And then
ID: 3705963 • Letter: I
Question
I think it starts like this but I’m not sure.
Else (current == parent)
And then I think you need to use the getsuccessor method as pictured below but I’m not sure how to do that. I know there should be 3 statements in total though. //put your code here //check if there is no children on current - then you know it is a leaf if (current.rightChild null && current.leftchild null) f //check if what you are deleting is the root means only one node in the tr if(current parent) root null; if parent, leftChild null; else parent.rightchild - null; /1 if no right child, replace with left subtree //put your code here //check if there is no right children on current but there is a left child else if (current.rightChild null && current.leftchild !- null) f //check if what you are deleting is the root - means root only has left child if (current parent) root curent . leftChild; if(isLeftchild) else parent.leftChild current.leftChild; parent.rightchild current.leftChild; // if no left child, replace with right subtree //put your code here else if (current.rightchild!- null 88 current.leftchild null) ( //check if what you are deleting is the root - means root only has left child if(current-parent) root = current.rightChild; if(! ?sLeftChild) parent, leftChild current. leftChild; else parent.rightchild current.leftChild; //else // two ch ldren, so replace with inorder successor //put your code here
Explanation / Answer
Please note its not much clear to where you are asking to add the code.
But as much i can see from the screenshot you already have a getSuccessor() and if we are to assume that given a node you want to find the inorder successor.
In general, its always easy to make functions even more modular (as compared to the given method in screenshot)
Please find below the refactored code.
Please find below the code containing required methods. Hope this helps.
private Node minimumValueOfChild( Node node) {
Node presentNode = node;
/*
* here we will loop through starting from
* given node until we find the left most child
*/
while (presentNode.leftChild ! = null)
presentNode = presentNode.leftChild;
//return the found node
return presentNode;
}
Now for calculating in order successor
private Node getSuccessor( Node delNode) {
if (delNode.rightChild ! = null)
return minimumValueOfChild(delNode.rightChild) ;
Node parent = delNode.parent;
while(parent! = null && delNode ==rightChild){
delNode = parent;
parent = parent.parent;
}
return parent;
}
The desired Node class impl for the above should be as follows:
class Node {
int data;
Node leftChild, rightChild, parent;
Node (int data) {
this.data = data;
leftChild = rightChild = parent = null;
}
}