Professional Documents
Culture Documents
Given the binary tree, delete a node from it by making sure that tree shrinks from the
bottom(i.e. the deleted node is replaced by the bottom most and rightmost node). This
different from BST deletion. Here we do not have any order among elements, so we replace
with last element
We discussed BST search and insert opeartion. In thispost, deletion operation is
discussed. When we deleted a node, three possibilities arise.
Examples:
Delete 10 in below tree.
10
20 30
Output:
30
20
20 30
40
Output:
10
40 30
Algorithms
1.Start at root, find the deepest and rightmost node in binary tree and node which we want to
delete.
2.Replace th deepest rightmost nodes data with node to be deleted.
3. Then delete the deepest rightmost node.
50 50
delete(20)
30 70 -------------------> 30 70
20 40 60 80 40 60 80
2)Node to be deleted has only one child: Copy the child to the node and delete the child
50 50
delete(30)
30 70 -------------------> 40 70
40 60 80 60 80
3) Node to be deleted has two children: Find the inorder successor of the the node. Copy
contents of the inorder successur to the node and delete the in order predecessure can also be
used.
50 60
delete(50)
40 70 -------------------> 40 70
60 80 80
The important thing to be note is, inorder successor is needed only when right child is
empty. In this particular case, inorder successor can be obtained byfinding the minimum value
in thr right child of the node.