DSA Lec 8
DSA Lec 8
Trees
Course Instructor
Engr. Anum Raza
Lecture Outline
Insertion in BST
Deletion in BST
Algorithm1 for Finding Loc for Insertion
(This algorithm finds location Loc of Item and the location Par
of Parent of Item)
We want to delete the node, but preserve the sub-trees, that the
node links to.
One solution is to -
a) find a position in the right subtree to attach the left subtree.
b) attach the node’s right subtree to the parent
Binary Search Tree – Deletion
Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from subtree
Observation
Deletions may unbalance tree
Example Deletion (Internal Node)
Delete ( 10 )
10 5 5
5 30 5 30 2 30
2 25 45 2 25 45 2 25 45
10 25 25
5 30 5 30 5 30
2 25 45 2 25 45 2 45