16. Binary Search Tree
16. Binary Search Tree
Jayanta Pal
Dept. of IT
Tripura University
1
Binary Search Tree(BST)
• The left and right sub-trees of the root are again binary search trees
2
Binary Search Tree(BST)
3
Binary Search Tree(BST)
• A binary search tree is basically a binary tree, and therefore it can be
traversed in inorder, preorder and postorder.
Inorder Traversal: 1 3 4 6 7 8 10 13 14
4
Why Binary Search Tree?
• Let us consider a problem of searching a list.
5
Why Binary Search Tree?
• So we may think of using a linked list because it permits insertion and
deletion to be carried out by adjusting only few pointers.
• But in an n-linked list, there is no way to move through the list other
than one node at a time, permitting only sequential access.
6
Binary Search Tree(BST)
Time Complexity
Array Linked List BST
Search O(n) O(n) O(logn)
Insert O(1) O(1) O(logn)
Remove O(n) O(n) O(logn)
7
Operations on Binary Search Tree (BST)
• Following operations can be done in BST:
• Search(k, T): Search for key k in the tree T. If k is found in some node of tree
then return true otherwise return false.
• Insert(k, T): Insert a new node with value k in the info field in the tree T such
that the property of BST is maintained.
• Delete(k, T):Delete a node with value k in the info field from the tree T such
that the property of BST is maintained.
8
Searching Through The BST
• Compare the target value with the element in the root node
If the target value is equal, the search is successful.
If target value is less, search the left subtree.
If target value is greater, search the right subtree.
If the subtree is empty, the search is unsuccessful.
9
Algorithm to Search a Value in a BST
Step 2: End
10
11
Insertion of a node in BST
• To insert a new item in a tree, we must first verify that its key is different
from those of existing elements.
• If a new value is less, than the current node's value, go to the left subtree,
else go to the right subtree.
• Following this simple rule, the algorithm reaches a node, which has no left
or right subtree.
• By the moment a place for insertion is found, we can say for sure, that a
new value has no duplicate in the tree.
12
Algorithm for insertion in BST
• Check, whether value in current node and a new value are equal. If so,
duplicate is found. Otherwise,
13
Algorithm to Insert a Value in a BST
Insert (TREE, VAL)
Step 2: End
14
15
16
Deleting a node from the BST
• While deleting a node from BST, there may be three cases:
1. The node to be deleted may be a leaf node:
• In this case simply delete a node and set null pointer to its parents those side
at which this deleted node exist.
17
Deleting a node from the BST
2. The node to be deleted has one child
• In this case the child of the node to be deleted is appended to its parent node.
Suppose node to be deleted is 18
18
Deleting a node from the BST
19
Predecessor and Successor
In the inorder traversal of a binary tree, the neighbors of
given node are called Predecessor (the node lies behind
of given node) and Successor (the node lies ahead of
given node).
Inorder: 5 10 15 18 19 20 25 35 40 44 45
20
49
Deleting a node from the BST
21
Algorithm to Delete from a BST
Delete (TREE, VAL)
24
Deleting a BST
• To delete/remove the entire binary search tree from the memory,
we will first delete the elements/nodes in the left sub-tree and
then delete the right sub-tree.
deleteTree (TREE)
Step 1: IF TREE != NULL , then
deleteTree (TREE->LEFT)
deleteTree (TREE->RIGHT)
Free (TREE)
[END OF IF]
Step 2: End
25
Finding the Smallest Node in a BST
• The basic property of a BST states that the smaller value will occur
in the left sub-tree.
• If the left sub-tree is NULL, then the value of root node will be
smallest as compared with nodes in the right sub-tree.
• So, to find the node with the smallest value, we will find the value
of the leftmost node of the left sub-tree.
• However, if the left sub-tree is empty then we will find the value of
the root node.
findSmallestElement (TREE)
Step 1: IF TREE = NULL OR TREE->LEFT = NULL, then
Return TREE
ELSE
Return findSmallestElement(TREE->LEFT)
[END OF IF]
Step 2: End
26