Lecture 3.1.4 Binary Search Tree
Lecture 3.1.4 Binary Search Tree
BCA/BSC CS
DATA STRUCTURES
(22CAT-211/22SCT-211/22CAT231)
1
Topics to be Studied
• Binary Search tree
Course Outcome: CO5- Design an algorithm that is the best fit for finding solutions
to complex problems using data structures.
2
Binary Search tree
• What is a Binary Search tree?
• A binary search tree follows some order to arrange the elements. In a Binary search tree, the value
of left node must be smaller than the parent node, and the value of right node must be greater than
the parent node. This rule is applied recursively to the left and right subtrees of the root.
3
Binary Tree
• In the above figure, we
can observe that the root
node is 40, and all the
nodes of the left subtree
are smaller than the root
node, and all the nodes of
the right subtree are
greater than the root node
4
Binary search TREE
• Similarly, we can see the left child of root node is
greater than its left child and smaller than its right
child. So, it also satisfies the property of binary
search tree. Therefore, we can say that the tree in
the above image is a binary search tree
• Suppose if we change the value of node 35 to 55 in
the above tree, check whether the tree will be
binary search tree or not.
• In the above tree, the value of root node is 40,
which is greater than its left child 30 but smaller
than right child of 30, i.e., 55. So, the above tree
does not satisfy the property of Binary search tree.
Therefore, the above tree is not a binary search tree
5
Advantages of Binary search tree
• Searching an element in the Binary search tree is easy as we always have a hint
that which subtree has the desired element.
• As compared to array and linked lists, insertion and deletion operations are
faster in BST.
6
creating a binary search tree
• Now, let's see the creation of binary search tree using an example.
• Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
• First, we have to insert 45 into the tree as the root of the tree.
• Then, read the next element; if it is smaller than the root node, insert it as the
root of the left subtree, and move to the next element.
• Otherwise, if the element is larger than the root node, then insert it as the root
of the right subtree.
• Now, let's see the process of creating the Binary search tree using the given
data element. The process of creating the BST is shown below -
7
• Step 1 - Insert 45.
8
• Step 2 - Insert 15.
• As 15 is smaller than 45, so insert it as the root node of the left subtree
9
• Step 3: Insert 79
• As 79 is greater than 45, so insert it as the root node of the right subtree.
10
• Step 4 - Insert 90.
• 90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
11
• Step 5 - Insert 10.
• 10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.
12
• Step 6 - Insert 55.
• 55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree
of 79
13
• Step 7 - Insert 12.
• 12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the
right subtree of 10.
14
• Step 8 - Insert 20.
• 20 is smaller than 45 but greater than 15, so it will be inserted as the right
subtree of 15.
15
• Step 9 - Insert 50.
• 50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a left
subtree of 55.
16
• Now, the creation of binary search tree is completed. After that, let's move
towards the operations that can be performed on Binary search tree.
• We can perform insert, delete and search operations on the binary search tree.
17
Searching in Binary search tree
• Searching means to find or locate a specific element or node in a data structure. In Binary
search tree, searching a node is easy because elements in BST are stored in a specific order.
The steps of searching a node in Binary Search tree are listed as follows -
• First, compare the element to be searched with the root element of the tree.
• If root is matched with the target element, then return the node's location.
• If it is not matched, then check whether the item is less than the root element, if it is smaller
than the root element, then move to the left subtree.
• If it is larger than the root element, then move to the right subtree.
• Repeat the above procedure recursively until the match is found.
• If the element is not found or not present in the tree, then return NULL.
• Now, let's understand the searching in binary tree using an example. We are taking the
binary search tree formed above. Suppose we have to find node 20 from the below tree.
18
Step 1
19
Step 2
20
Step 3
21
Algorithm for Binary search tree
• Search (root, item)
• Step 1 - if (item = root → data) or (root = NULL)
• return root
• else if (item < root → data)
• return Search(root → left, item)
• else
• return Search(root → right, item)
• END if
• Step 2 - END
22
REFERENCE IMAGES
[1]https://www.tutorialspoint.com/data_structures_algorithms/
[2] https://www.tutorialride.com/data-structures/graphs-in-data-structure.htm
23
REFERENCES
Reference Books:
• Seymour Lipschutz, Schaum's Outlines Series Data structures TMH
• Introduction to Data Structures Applications, Trembley & Soreson, Second
Edition, Pearson Education
24
THANK YOU