Lecture 23
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)
1) If Root= NULL then Loc=NULL, Par=NULL and Exit
2) If Item= Data[Root], Loc=Root and Par=NULL and Exit
3) If Item< Data[Root], then Save=Root and Ptr=Left[Root]
Else Save=Root and Ptr=Right[Root]
4) Repeat while (Ptr ≠ NULL)
If Item=Data[Ptr], Loc=Ptr and Par= Save and Exit
Else if Item < Data[Ptr], Save= Ptr, and Ptr=Left [Ptr]
Else Save=Ptr and Ptr=Right[Ptr]
5) [Search Unsuccessful] Set Loc=NULL and Par=Save and Exit
Algorithm 2 for Insertion
(This algorithm finds location Loc of Item or adds Item as new
Node in Tree at Location Loc)
1) If Loc=NULL then Insertion is not possible
2) If Avail=NULL then Overflow and Return
3) Set New=Avail and Avail=Left[Avail]
4) Data[New]=Item 20
Left[New]=NULL
Right[New]=NULL 15 44
5) If Par ≠ NULL
If Item< Data[Par]
Left[Par]= New 22 50
Else
Right[Par]=New
Else 23
Root=New
6) Exit
Deleting a Node
To delete a leaf node is easy -
a) Find its parent
b) Set the child pointer that links to it to NULL
c) Free the node’s memory
Example Deletion (Leaf)
Delete ( 25 )
10 10
10 < 25, right
5 30 30 > 25, left 5 30
25 = 25, delete
2 25 45 2 45
How to delete a node if it has child nodes?
We want to delete the node, but preserve the sub-trees, that the
node links to.
There are 2 possible situations to be faced when deleting a non leaf
node -
1) The node has one child.
2) The node has two children.
Example: Node to be removed has
one child..
Remove 18
The problem is not as easily solved if the node has two children.
We cannot attach both of the node’s subtrees to its parent.
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
Replacing 10 Replacing 5 Deleting leaf
with largest with largest
value in left value in left
subtree subtree
Example Deletion (Internal Node)
Delete ( 10 )
10 25 25
5 30 5 30 5 30
2 25 45 2 25 45 2 45
Replacing 10 Deleting leaf Resulting tree
with smallest
value in right
subtree
Now the code - to delete a node from the IntBinaryTree, call the
public member remove. The argument passed to the function is the
value of the node you want to delete.
Deletion Algorithm for less than
two children
(This procedure deletes node N at location Loc where N does not
have two children. The pointer Par gives location of parent of N.
the pointer Child gives the location of the only child of N)
1) If Left[Loc]=NULL and Right[Loc]=NULL
Then Child=NULL
Else If Left[Loc] ≠ NULL, Child=Left[Loc]
Else 50
Child=Right[Loc]
2) If Par ≠ NULL 30 44
If Loc=Left[Par]
Left[Par]=Child
Else 20 35
Right[Par]=Child Loc=6, Par=5
Else Loc=5, Par=2
Root=Child 32