6746f8f754ac0_ppt
6746f8f754ac0_ppt
CP4 Make use of linear data structures and nonlinear data structures
solving real time applications.
2
Binary Search Trees
Binary Search Tree is a binary tree in which every node contains only
smaller values in its left sub tree and only larger values in its right sub
tree.
To enhance the performance of binary tree, we use a special type of
binary tree known as Binary Search Tree. Binary search tree mainly
focuses on the search operation in a binary tree.
Every binary search tree is a binary tree but every binary tree need not
to be binary search tree.
3
Binary Search Trees
The following tree is a Binary Search Tree. In this tree, left subtree of every node
contains nodes with smaller values and right subtree of every node contains larger
values.
4
Binary Search Trees
Construct a Binary Search Tree by inserting the following sequence
of numbers...
10,12,5,4,20,8,7,15 and 13
5
Binary Search Trees
Construct a Binary Search Tree by inserting the following sequence
of numbers... 10,12,5,4,20,8,7,15 and 13
6
Binary Search Trees
Construct a Binary Search Tree by inserting the following sequence
of numbers... 10,12,5,4,20,8,7,15 and 13
7
Binary Search Trees
Construct a Binary Search Tree by inserting the following sequence of
numbers... 10,12,5,4,20,8,7,15 and 13
8
Binary Search Trees
9
Binary Search Trees
Construct a Binary Search Tree by inserting the following sequence
of numbers... 43, 10, 79, 90, 12, 54, 11, 9, 50
10
Binary Search Trees
Construct a Binary Search Tree by inserting the following sequence of
numbers... 43, 10, 79, 90, 12, 54, 11, 9, 50
11
Binary Search Trees
Construct a Binary Search Tree by inserting the following sequence of
numbers... 43, 10, 79, 90, 12, 54, 11, 9, 50
12
Properties of Binary Search Trees
Binary Search Tree is a node-based binary tree data structure which
has the following properties:
The left subtree of a node contains only nodes with keys lesser than
the node’s key.
The right subtree of a node contains only nodes with keys greater than
the node’s key.
The left and right subtree each must also be a binary search tree.
Duplicate node keys are not allowed.
An Inorder traversal of a BST visits the keys in increasing order
(ascending sorted elements)
13
Advantages of Binary Search Trees
Searching become very efficient in a binary search tree since, we get a hint at
each step, about which sub-tree contains the desired element.
It also speed up the insertion and deletion operations as compare to that in
array and linked list.
14
Operations on Binary Search Trees
The following operations are performed on a binary search tree...
15
Operations on Binary Search Trees
The following operations are performed on a binary search tree...
SN Operation Description
1 Searching in BST Finding the location of some specific element in a binary
search tree.
2 Insertion in BST Adding a new element to the binary search tree at the
appropriate location so that the property of Binary Search
Tree do not violate.
3 Deletion in BST Deleting some specific node from a binary search tree.
However, there can be various cases in deletion depending
upon the number of children, the node have.
16
Searching in Binary Search Trees
Search Operation is performed to search a particular element in the Binary
Search Tree.
Whenever an element is to be searched, start searching from the root node.
Then if the data is less than the key value, search for the element in the left
subtree. Otherwise, search for the element in the right subtree. Follow the
same algorithm for each node.
17
Searching in Binary Search Trees
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the value of root node in the
tree.
Step 3 - If both are matched, then display "Given node is found!!!" and
terminate the function
Step 4 - If both are not matched, then check whether search element is
smaller or larger than that node value.
Step 5 - If search element is smaller, then continue the search process
in left subtree.
Step 6 - If search element is larger, then continue the search process in
right subtree. 18
Searching in Binary Search Trees
Step 7 - Repeat the same until we find the exact element or until the
search element is compared with the leaf node
Step 8 - If we reach to the node having the value equal to the search
value then display "Element is found" and terminate the function.
Step 9 - If we reach to the last leaf node and if it is also not matched
with the search element, then display "Element is not found" and
terminate the function.
19
Searching in Binary Search Trees
20
Consider key = 45 has to be searched in the
given Binary Search Tree -
• We start our search from the root node 25.
• As 45 > 25, so we search in 25’s right
subtree.
• As 45 < 50, so we search in 50’s left subtree.
• As 45 > 35, so we search in 35’s right
subtree.
• As 45 > 44, so we search in 44’s right subtree
but 44 has no subtrees.
• So, we conclude that 45 is not present in the
above Binary Search Tree.
21
Insertion in Binary Search Trees
Insertion Operation is performed to insert an element in the Binary
Search Tree.
Whenever an element is to be inserted, first locate its proper location.
The insertion of a new key always takes place as the child of some leaf
node.
For finding out the suitable leaf node,
• Search the key to be compared from the root node till some leaf
node is reached.
• Once a leaf node is reached, insert the key as child of that leaf
node.
22
Insertion in Binary Search Trees
Step 1 - Create a newNode with given value and set its left and right to NULL.
Step 2 - Check whether tree is Empty.
Step 3 - If the tree is Empty, then set root to newNode.
Step 4 - If the tree is Not Empty, then check whether the value of newNode is
smaller or larger than the root node .
Step 5 - If newNode is smaller than or equal to the node then move to its left child. If
newNode is larger than the node then move to its right child.
Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches to
NULL).
Step 7 - After reaching the leaf node, insert the newNode as left child if the
newNode is smaller or equal to that leaf node or else insert it as right child.
23
Insertion in Binary Search Trees
Consider the following example where key = 4 is inserted in the
given BST-
29
Deletion operation in BST
Case 1: Deleting a Leaf node (A node with no children)
Just remove / disconnect the leaf node that is to deleted from the tree.
Algorithm
Step 1 - Find the node to be deleted using search operation
Step 2 - Delete the node by setting null in the parent address field and
terminate the function.
30
Case 1: Deleting a Leaf node (A node with no children)
31
Deletion operation in BST
Case 1: Deleting a Leaf node (A node with no children)
Example
Consider the following example where node with value
= 20 is deleted from the BST-
32
Deletion operation in BST
Case 2: Deleting a node with one child
•Just remove / disconnect the node that is to deleted from the tree.
•Algorithm
•Step 2 - If it has only one child then create a link between its
parent node and child node.
33
Case 2: Deleting a node with one child
This is the second case of deletion in which you delete a node that has 1 child,
as you can see in the diagram that 9 has one child.
Delete the node 9 and replace it with its child 10 and add a link from 7 to 10
View the new structure of the BST without 9
34
Deletion operation in BST
Case 2: Deleting a node with one child
Consider the following example where node with value
= 30 is deleted from the BST-
35
Deletion operation in BST
Case 3: Deleting a node with two children
•A node with two children may be deleted from the BST in the
following two ways- i)right subtree
ii)left subtree
•Method-01: right subtree
•Step 1- Visit to the right subtree of the deleting node.
•Step 2- Pluck the least value element in right subtree called as
inorder successor.
•Step 3- Replace the deleting element with its inorder successor.
36
Deletion operation in BST
Case 3: Deleting a node with two children
Consider the following example where node with value
= 15 is deleted from the BST-
37
Deletion operation in BST
Case 3: Deleting a node with two children
•A node with two children may be deleted from the BST in the
following procedure
•Method-02: left subtree
•Step 1- Visit to the left subtree of the deleting node.
•Step 2- Pluck the greatest value element called in left subtree as
inorder predecessor.
•Step 3- Replace the deleting element with its inorder predecessor.
38
Deletion operation in BST
Case 3: Deleting a node with two children
39
Deletion operation in BST
Case 3: Deleting a node with two children
Consider the following example where node with value
= 15 is deleted from the BST-
40
Thank You
41