How to delete a node with 2 children nodes in a binary tree?
Is there any kind of method to remove it? I googled it. But didn't get clear idea about it. Anybody explain it with diagrammatic representation?
How to delete the node '5' from the this image and what might be the outcome?
The node to be deleted has two children. However, the node which is to be deleted, is replaced with its in-order successor or predecessor recursively until the node value (to be deleted) is placed on the leaf of the tree.
Starting at the root, find the deepest and rightmost node in the binary tree and the node which we want to delete. Replace the deepest rightmost node's data with the node to be deleted. Then delete the deepest rightmost node.
If the node has two children, we must first find the In-Order Predecessor (IOP): the largest node in our node's left subtree. The IOP is always a leaf node, and can be found by starting at the left subtree's root and moving right. We can then swap the node being removed with its IOP and delete it, as it is now a leaf.
A binary tree is a tree in which no node has more than two children, and every child is either a left child or a right child even if it is the only child its parent has.
you replace said node with the left most child on its right side, or the right most child on its left side. You then delete the child from the bottom that it was replaced with.
Deleting five could yield either a tree with 3 as its root or 18 as its root, depending on which direction you take.
It looks like you got that image from this site: http://www.algolist.net/Data_structures/Binary_search_tree/Removal
It shows the algorithm you want with images too.
For deleting a Node there are three scenarios possible..
Before knowing the concept of deletion, we should understand the concept of successor and predecessors
Successor: In a binary tree successor of a node is the smallest node of the tree that is strictly greater then this node.
There are two possible cases with a node
A node has right children: In this case the leftmost child of the right sub-tree will be successor.
A node has no children: Go to the parent node and find a node for which this node is part of left sub-tree.
public Node sucessor(Node node) {
Node sucessor = null;
if (node.getRightNode() != null) {
Node newNode = node.getRightNode();
while (newNode != null) {
sucessor = newNode;
newNode = newNode.getLeftNode();
}
} else {
sucessor = findRightParent(node);
}
return sucessor;
}
private Node findRightParent(Node node) {
Node newNode = null;
if (node.getParentNode() != null) {
if (node.getParentNode().getLeftNode() == node) {
newNode = node.getParentNode();
} else {
newNode = findRightParent(node.getParentNode());
}
}
return newNode;
}
Now the deletion logic
public void deleteNode(Node node) {
// Node is a leaf node //
if( node.getLeftNode() == null && node.getRightNode() == null){
if(isRightNode(node.getParentNode(), node)){
node.getParentNode().setRightNode(null);
}else{
node.getParentNode().setLeftNode(null);
}
// Only left child is there//
}else if( node.getLeftNode() != null && node.getRightNode() == null){
if(isRightNode(node.getParentNode(), node)){
node.getParentNode().setRightNode(node.getLeftNode());
}else{
node.getParentNode().setLeftNode(node.getLeftNode());
}
// Only Right child is there //
}else if( node.getLeftNode() == null && node.getRightNode() != null){
if( isRightNode(node.getParentNode(), node)){
node.getParentNode().setRightNode(node.getRightNode());
}else{
node.getParentNode().setLeftNode(node.getRightNode());
}
// Both Left and Right children are their//
}else{
Node psNode = predessor(node);
deleteNode(psNode);
psNode.setParentNode(node.getParentNode());
psNode.setLeftNode(node.getLeftNode());
psNode.setRightNode(node.getRightNode());
if( isRightNode(node.getParentNode(), node)){
node.getParentNode().setRightNode(psNode);
}else{
node.getParentNode().setLeftNode(psNode);
}
}
}
Solution is taken from http://coder2design.com/binary-tree/
Also they have explained the flow of traversal and insertion with full source code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With