Lec-15 Binary Search Tree
Lec-15 Binary Search Tree
1
Binary Search Tree (BST)
- It is a hierarchical, nonlinear data structure.
- Its properties are given bellow:
- Each node has a value.
- A total order is defined on these node values.
- Left subtree of a node contains the values less than the node’s value.
- Right subtree of a node contains the values greater than or equal to
the node’s value.
Example: 8
Root R
3 12
Left Subtree
1 6 9 14
Right Subtree
2 10
2
Searching a node with a specific value from BST Rules
• Examine the root R.
• If the searching value equals the root, then the value exists in the
tree.
• If it is less than the root, then it must be in the left subtree.
• If it is greater than the root, then it must be in the right subtree.
• This process continues until the searching value is found or a leaf
node is reached without having that value where it would be.
Algorithm Here,
Root = Root of BST
• Search_BST(Root, Left, Right, Key )
Left = Left Subtree
1. Loc := Search(Root, Key, NULL)
Right = Right Subtree
2. If Loc = NULL then Print: “Item not in BST”.
Key = Searching Item
3. Else Print: “Item is in BST”.
4. End.
3
• Search(Node, Key, Par)
1. If Node = NULL then return NULL.
2. If Key < Node.Key then Search(Node.Left, Key, Node).
3. Else if Key > Node.Key then Search(Node.Right, Key, Node).
4. Else return Node
Example:
Searching Item: 12
AA
15 Steps:
BB CC
1. Search(Root, 12) Search(AA,12)
13 100 2. Search(AA->Left,12) Search(BB, 12)
DD EE
3. Search(BB->Left, 12) Search(DD, 12)
12 90 4. Return DD and Print: Item is in BST.
4
Complexity of Searching a Node from BST
5
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
Here,
• Insert_BST(Root, Left, Right, Item)
Root = Root of BST
1. Set NewNode.Value := Item.
Left = Left Subtree
2. Insert(Root, NewNode).
Right = Right Subtree
End.
Item = New Item to be inserted
6
Insert(Node, NewNode)
1. If Node = NULL then Node := NewNode and return.
Example:
New Item: 110
AA
15
FF.Value=110 (FF is the New Node address)
BB CC Steps:
13 100 1. Insert(Root, FF) Insert(AA, FF)
7
Complexity of Inserting a Node into BST
• Insertion of a Node =1
8
Deleting a Node from BST
Rules
- A node N has no children. N is deleted from BST by simply replacing the
location of N in the parent node P(N) by the NULL pointer.
- A node N has one child. N is deleted from BST by simply replacing the location
of N in the parent node P(N) by the location of the only child of N.
- A node N has two children. Let S(N) denote the inorder successor of N. N is
deleted from BST by first deleting S(N) from BST and then replacing node N
by S(N).
Algorithm
Delete_BST(Root, Left, Right, Key)
1. Set Loc := Search_BST(Root, Left, Right, Key)
2. If Loc = NULL then Print: “Item not in BST” and return
3. If Loc.Left ≠ NULL and Loc.Right, Delete_A(Root, Left, Right, Loc)
4. Else Delete_B(Root, Left, Right, Loc)
5. End.
9
Delete_B(Root, Left, Right, Loc, Par)
8. Return
10
1.Delete_A(Root, Left, Right, Loc, Par)
10. Return.
11
Example:
Deleted Item: 15
AA Loc = AA and Par = NULL
15
Steps:
BB CC
1. Delete_A(Root, Loc, Par)
13 100
2. Inorder Successor, S(Loc) = EE
DD EE FF 3. Delete EE from BST
12 90 110 4. Replace AA by EE by EE.Left = AA.Left and
EE.Right := AA.Right.
Figure: Given BST
EE
90
BB CC
13 100
After Delete Operation DD Figure: Resulting BST
FF
12 110
12
Complexity of Deleting a Node into BST
• No. of Nodes =N
13
Traversal of BST
- Once the binary search tree has been created, its elements can be traversed in 3 ways.
Traverse_InOrder(TreeNode) Traverse_PostOrder(TreeNode)
1.If TreeNode is NULL, then Return 1.If TreeNode is NULL, then Return
2.Traverse_Inorder(TreeNode.Left) 2.Traverse_Inorder(TreeNode.Left)
3.Visit(TreeNode) 3.Traverse_Inorder(TreeNode.Right)
4.Traverse_Inorder(TreeNode.Right) 4.Visit(TreeNode)
(2) Preorder
Traverse_PreOrder(TreeNode)
1.If TreeNode is NULL, then Return
2.Visit(TreeNode)
3.Traverse_PreOrder(TreeNode.Left)
4.Traverse_PreOrder(TreeNode.Right)
14
Example:
AA
15
Inorder Traversal: 12, 13, 15, 90, 100, 110
BB CC
Preorder Traversal : 15, 13, 12, 100, 90, 110
13 100
Postorder Traversal : 12, 13, 90, 110, 100, 15
DD EE FF
12 90 110
Since every node of BST is visited, so the complexity of traversing a BST is O(N), where
N is the no. of nodes in BST.
15
Sorting of A List of Items Using BST
• A binary search tree can be used to implement a simple but inefficient sorting algorithm.
- Traverse_InOrder(TreeNode)
• Example:
List of Items: 50, 10, 5, 100, 120, 90, 20
BST: 50
10 100
5 20 90 120
Inorder Traverse: 5, 10, 20, 50, 90, 100, 120 [Sorted List]
16
END!!!
17