Binary Search Tree
Binary Search Tree
6 19
4 43
8 17
5 31
10
Insertion
• 14, 23, 7, 10, 33, 56, 80, 66, 70
14
23
7
33
10
56
80
66
70
BST Operation
• Deletion
– To delete a node from a binary search tree, we
must first locate it.
– There are four possible cases when we delete a
node:
• The node to be deleted has no children. In this case, all
we need to do is delete the node.
• The node to be deleted has only a right subtree. We
delete the node and attach the right subtree to the
deleted node’s parent.
• The node to be deleted has only a left subtree. We
delete the node and attach the left subtree to the deleted
node’s parent.
BST Operation
• Deletion
– The node to be deleted has two subtrees.
• Find the largest node in the deleted node’s left
subtree and move its data to replace the
deleted node’s data.
• Or find the smallest node on the deleted node’s
right subtree and move its data to replace the
deleted node’s data.
BST Operation
• Deletion: Example
deletion
• Delete node 8
• Delete11
• Delete 31 11
17
6 19
4 43
8
10 17
5 31
10
End of Lecture