2. BINARY SEARCH TREES
A Binary Search Tree is a binary tree, which is either empty or
satisfies the following properties :
1. Every node has a value and no two nodes have the same value (i.e.,
all the values are unique) root.
2. If there exists a left child or left sub tree then its value is less than the
value of the
3. The value(s) in the right child or right sub tree is larger than the value of
the root node.
4. Example: Constructing BST
Figure 8.12 shows a binary search tree.
Notice that this tree is obtained by inserting
the values
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
in that order, starting from an empty tree.
Fig 8.12
8. 10/29/08 Shaily Kabir,Dept. of CSE, DU 8
Inserting a Node into a BST
Rules
• Examine the root R.
• If the new value is less than the root’s, then it must be inserted at the left subtree.
• Otherwise it must be inserted at the right subtree.
• This process continues until the new value is compared with a leaf node and then it is
added as that node’s (leaf node) left or right child depending on its value.
Algorithm
• Insert_BST(Root, Left, Right, Item)
1. Set NewNode.Value := Item.
2. Insert(Root, NewNode).
End.
Here,
Root = Root of BST
Left = Left Subtree
Right = Right Subtree
Item = New Item to be inserted
9. 10/29/08 Shaily Kabir,Dept. of CSE, DU 9
Insert(Node, NewNode)
1. If Node = NULL then Node := NewNode and return.
2. If NewNode.Value < Node.Value then Insert(Node.Left, NewNode).
3. Else Insert(Node.Right, NewNode).
Example:
Figure: BST with 5 Nodes
New Item: 110
FF.Value=110 (FF is the New Node address)
Steps:
1. Insert(Root, FF) Insert(AA, FF)
2. Insert(AA->Right, FF) Insert(CC, FF)
3. Insert(CC->Right, FF)
AA
BB CC
DD EE
15
13 100
12 90 110
FF
10. DELETING A NODE
Often you need to delete an element from a binary search tree. Doing
so is far more complex than adding an element into a binary search
tree.
To delete an element from a binary search tree, you need to first locate
the node that contains the element and also its parent node. Let current
point to the node that contains the element in the binary search tree
and parent point to the parent of the current node. The current node
may be a left child or a right child of the parent node.
11. There are two cases to consider:
Case 1: The current node does not have a left child, as shown in Figure
1(a). Simply connect the parent with the right child of the current node, as
shown in Figure 1(b).
DELETING A NODE cont.
Figure 1 Case 1: the current node has no left child.
12. DELETING A NODE: Case 1
Figure 2 Case 1: deleting node 10 from (a) results in (b).
For example, to delete node 10 in Figure 2(a). Connect the
parent of node 10 with the right child of node 10, as shown in
Figure 2(b).
13. Case 2: The current node has a left child.
Let rightMost point to the node that contains the largest element in the
left subtree of the current node and parentOfRightMost point to the
parent node of the rightMost node, as shown in Figure 3(a). Note that the
rightMost node cannot have a right child but may have a left child.
Replace the element value in the current node with the one in the
rightMost node, connect the parentOfRightMost node with the left child
of the rightMost node, and delete the rightMost node, as shown in Figure
3(b).
DELETING A NODE: Case 2
14. DELETING A NODE: Case 2
Figure 3 Case 2: the current node has a left child.
15. For example, consider deleting node 20 in Figure 4(a). The rightMost node
has the element value 16. Replace the element value 20 with 16 in the
current node and make node 10 the parent for node 14, as shown in
Figure 4(b).
DELETING A NODE: Case 2
Figure 4 Case 2: deleting node 20 from (a) results in (b).