0% found this document useful (0 votes)
21 views63 pages

Indian Institute of Information Technology, Nagpur: Course: Data Structures (CSL 102, Core)

The document provides an overview of Binary Search Trees (BST), detailing their structure, advantages, and operations such as searching, insertion, and deletion. It includes algorithms for these operations, examples, and problems related to BST traversal and construction. The time complexity of BST operations is also discussed, highlighting best and worst-case scenarios.

Uploaded by

rajpootayush2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views63 pages

Indian Institute of Information Technology, Nagpur: Course: Data Structures (CSL 102, Core)

The document provides an overview of Binary Search Trees (BST), detailing their structure, advantages, and operations such as searching, insertion, and deletion. It includes algorithms for these operations, examples, and problems related to BST traversal and construction. The time complexity of BST operations is also discussed, highlighting best and worst-case scenarios.

Uploaded by

rajpootayush2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Indian Institute of Information Technology,

Nagpur

Course: Data Structures


(CSL 102, Core)
2nd Semester

Topics Covered Dr. Aishwarya Ukey


Binary Search Tree Assistant Professor
Dept. of CSE, IIIT Nagpur
Introduction
• Binary Search tree (BST)
• Class of binary trees, in which the nodes are arranged in a
specific order
• Also called ordered binary tree
• In a binary search tree,
• The value of all the nodes in the left sub-tree is less than
the value of the root
• The value of all the nodes in the right sub-tree is greater
than or equal to the value of the root
• This rule will be recursively applied to all the left and right
sub-trees of the root
Cont…
T
Cont…
• Advantages
• Searching become very efficient in a binary search tree
• hint at each step- which sub-tree contains the desired
element
• Binary search tree is considered as efficient data structure
in compare to arrays and linked lists
• In searching, it removes half sub-tree at every step.
• Searching for an element takes O(log2n) time
• In worst case, the time to search element is O(n)
• Speed up insertion and deletion operations
Cont…
• Construct a BST for the following sequence of numbers
50, 70, 60, 20, 90, 10, 40, 100
Cont…
• Construct a BST for the following sequence of numbers
50, 70, 60, 20, 90, 10, 40, 100
Cont…
• Construct a BST for the following sequence of numbers
50, 70, 60, 20, 90, 10, 40, 100
Cont…
• Construct a BST for the following sequence of numbers
50, 70, 60, 20, 90, 10, 40, 100
Cont…
• Construct a BST for the following sequence of numbers
50, 70, 60, 20, 90, 10, 40, 100
Cont…
• Construct a BST for the following sequence of numbers
50, 70, 60, 20, 90, 10, 40, 100
Cont…
• Construct a BST for the following sequence of numbers
50, 70, 60, 20, 90, 10, 40, 100
Cont…
• Construct a BST for the following sequence of numbers
50, 70, 60, 20, 90, 10, 40, 100
Cont…
• Construct a BST for the following sequence of numbers
50, 70, 60, 20, 90, 10, 40, 100
Cont…
• Multiple Choice Question
• A binary search tree is generated by inserting in order of
the following integers-
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24

The number of nodes in the left subtree and right subtree


of the root respectively are _____ respectively.
a) 4, 7
b) 7, 4
c) 8, 3
d) 3, 8
Cont…
• Operations on Binary Search Tree
• Searching
• Finding the location of some specific element in a
binary search tree
• Insertion
• Adding a new element to the binary search tree at the
appropriate location so that the property of BST do
not violate
• Deletion
• Deleting some specific node from a binary search tree.
• Can be various cases in deletion depending upon the
number of children, the node have
Cont…
• Searching
• Compare the element with the root of the tree.
• If the item is matched then return the location of the
node.
• Otherwise check if item is less than the element present
on root, if so then move to the left sub-tree.
• If not, then move to the right sub-tree.
• Repeat this procedure recursively until match found.
• If element is not found then return NULL.
Cont…
• Search Algorithm
Search(ROOT, ITEM)
Step 1: IF ROOT->DATA = ITEM OR ROOT = NULL
return ROOT
ELSE IF ITEM < ROOT->DATA
return Search(ROOT->LEFT, ITEM)
ELSE
return Search(ROOT->RIGHT, ITEM)
[END OF IF]
[END OF IF]
Step 2: END
Cont…
• Example: Search an element with Key 60
Cont…
• Example: Search an element with Key 60
Cont…
• Example: Search an element with Key 60
Cont…
• Example: Search an element with Key 60
Cont…

struct node* search(struct node *root, int item)


{
if(root == NULL || root->data == item)
return root;
else if(item < root->data)
return search(root->leftchild, item);
else
return search(root->rightchild, item);
}
Cont…
• Insertion
• Allocate the memory for tree.
• Set the data part to the value and set the left and right
pointer of tree, point to NULL.
• If the item to be inserted, will be the first element of the
tree, then the left and right of this node will point to
NULL.
• Else, check if the item is less than the root element of the
tree, if this is true, then recursively perform this operation
with the left of the root.
• If this is false, then perform this operation recursively with
the right sub-tree of the root.
Cont…
• Insertion Algorithm
Insert (ROOT, ITEM)
IF ROOT = NULL
Allocate memory for Node
SET ROOT->DATA = ITEM
SET ROOT->LEFT = ROOT->RIGHT = NULL
ELSE
IF ITEM < ROOT->DATA
Insert(ROOT->LEFT, ITEM)
ELSE
Insert(ROOT->RIGHT, ITEM)
[END OF IF]
[END OF IF]
Cont…
• Example: Insert a new node with Key 95
Cont…
• Example: Insert a new node with Key 95
Cont…
• Example: Insert a new node with Key 95
Cont…
• Example: Insert a new node with Key 95
Cont…
struct node* insert(struct node *root, int item)
{
if(root == NULL)
return new_node(item);
else if(item < root->data)
root->leftchild = insert(root->leftchild, item);
else
root->rightchild = insert(root->rightchild, item);
return root;
}
Cont…
struct node* new_node(int item)
{
struct node *temp = malloc(sizeof(struct node));
temp->data = item;
temp->leftchild = NULL;
temp->rightchild = NULL;
return temp;
}
Cont…
• Deletion
• Delete the specified node from a binary search tree.
• Deletion of a node is performed in such a way, that the
property of binary search tree doesn't violate.
• There are three situations of deleting a node from binary
search tree.
• The node to be deleted is a leaf node
• The node to be deleted has only one child
• The node to be deleted has two children
Cont…
• Deletion
• Case I - The node to be deleted is a leaf node
Cont…
• Deletion
• Case II - The node to be deleted has only one child
Cont…
• Deletion
• Case III - The node to be deleted has two children
Cont…
Cont…
Cont…
Cont…
• BST Traversal
• BST is traversed – exactly same way a binary tree
• BST traversal is same as binary tree traversal
Cont…
• BST Traversal
• BST is traversed – exactly same way a binary tree
• BST traversal is same as binary tree traversal
Cont…
• BST Traversal
• BST is traversed – exactly same way a binary tree
• BST traversal is same as binary tree traversal
Cont…
• BST Traversal
• BST is traversed – exactly same way a binary tree
• BST traversal is same as binary tree traversal
Cont…
• Note-01:
• Inorder traversal of a binary search tree always yields all
the nodes in increasing order.
• Note-02:
• A binary search tree can be constructed using only
preorder or only postorder traversal result.
• This is because inorder traversal can be obtained by
sorting the given result in increasing order.
Cont…
• Time Complexity of Binary Search Tree
• Time complexity of all BST Operations = O(h)
Here, h = Height of binary search tree
• In worst case,
• The binary search tree is a skewed binary search tree.
• Height of the binary search tree becomes n.
• So, Time complexity of BST Operations = O(n)
• In best case,
• BST is a balanced binary search tree
• Height of the binary search tree becomes log(n)
• So, Time complexity of BST Operations = O(log n)
Cont…
• Problem-01:
• Suppose the numbers 7 , 5 , 1 , 8 , 3 , 6 , 0 , 9 , 4 , 2 are
inserted in that order into an initially empty binary search
tree. The binary search tree uses the usual ordering on
natural numbers.
What is the inorder traversal sequence of the resultant
tree?
a) 7 , 5 , 1 , 0 , 3 , 2 , 4 , 6 , 8 , 9
b) 0 , 2 , 4 , 3 , 1 , 6 , 5 , 9 , 8 , 7
c) 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
d) 9 , 8 , 6 , 4 , 2 , 3 , 0 , 1 , 5 , 7
Ans: c
Cont…
• Problem-02:
• The preorder traversal sequence of a binary search tree is-
30 , 20 , 10 , 15 , 25 , 23 , 39 , 35 , 42
What one of the following is the postorder traversal
sequence of the same tree?
a) 10 , 20 , 15 , 23 , 25 , 35 , 42 , 39 , 30
b) 15 , 10 , 25 , 23 , 20 , 42 , 35 , 39 , 30
c) 15 , 20 , 10 , 23 , 25 , 42 , 35 , 39 , 30
d) 15 , 10 , 23 , 25 , 20 , 35 , 42 , 39 , 30
• Here, we are provided with the preorder
traversal sequence.
• We write the inorder traversal sequence by
arranging all the numbers in ascending order.
• Postorder Traversal :
30 , 20 , 10 , 15 , 25 , 23 , 39 , 35 , 42
• Inorder Traversal :
10 , 15 , 20 , 23 , 25 , 30 , 35 , 39 , 42
• We draw a binary search tree using these
traversal results.
• The binary search tree so obtained is as
shown-

Ans: d
• Problem 03:
• If the binary tree in figure is traversed in
inorder, then the order in which the nodes will
be visited is ____?
Ans:
• Problem 04:
• Which of the following sequences denotes the
postorder traversal sequence of the tree
shown in figure?
a) FEGCBDBA
b) GCBDAFE
c) GCDBFEA
d) FDEGCBA

Ans: c
• Problem 05:
• Which of the following binary trees has its
inorder and preorder traversals as BCAD and
ABCD respectively.

• Ans: d
• Problem 06:
• Traverse the following binary tree in pre, post,
inorder and level order.
• Ans:
• Preorder
• P, F , B, H, G , S, R, Y, T, W, Z
• Postorder
• B, G , H, F , R, W, T, Z, Y, S, P
• Inorder
• B, F , G , H, P, R, S, T, W, Y, Z
• Level order
• P, F , S, B, H, R, Y, G , T, Z, W
• Problem 07:
• Traverse the following binary tree in pre, post,
inorder and level order.
• Ans:
• Preorder
• A, B, D, G , K, H, L, M , C , E
• Postorder
• K, G , L, M , H, D, B, E, C , A
• Inorder
• K, G , D, L, H, M , B, A, E, C
• Level order
• A, B, C , D, E, G , H, K, L, M
• Problem 08:
• Construct a binary tree from a given preorder and
inorder sequence:
Preorder: A B D G C E H I F
Inorder: D G B A H E I C F
Ans:
• From Preorder sequence A B D G C E H I F, the root is: A
• From Inorder sequence D G B A H E I C F, we get the
left and right sub trees:
• Left sub tree is: D G B
• Right sub tree is: H E I C F
• To find the root, left and right sub trees for D G
B:
• From the preorder sequence B D G, the root of
tree is: B
• From the inorder sequence D G B, we can find
that D and G are to the left of B.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for D G:
• From the preorder sequence D G, the root of the
tree is: D
• From the inorder sequence D G, we can find that
there is no left node to D and G is at the right of D.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for H E I C
F:
• From the preorder sequence C E H I F, the root of
the left sub tree is: C
• From the inorder sequence H E I C F, we can find
that H E I are at the left of C and F is at the right of
C.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for H E I:
• From the preorder sequence E H I, the root of the
tree is: E
• From the inorder sequence H E I, we can find that
H is at the left of E and I is at the right of E.
• The Binary tree upto this point looks like:
• Problem 09:
• Construct a binary tree from a given postorder and inorder
sequence:
Inorder: D G B A H E I C F
Postorder: G D B H I E F C A
Ans:
• From Postorder sequence G D B H I E F C A, the root is: A
• From Inorder sequence D G B A H E I C F, we get the left and
right sub trees:
• Left sub tree is: D G B
• Right sub tree is: H E I C F
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for D G B:
• From the postorder sequence G D B, the root of
tree is: B
• From the inorder sequence D G B, we can find
that D G are to the left of B and there is no right
subtree for B.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for D G:
• From the postorder sequence G D, the root of the
tree is: D
• From the inorder sequence D G, we can find that
is no left subtree for D and G is to the right of D.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for H E I C
F:
• From the postorder sequence H I E F C, the root of
the left sub tree is: C
• From the inorder sequence H E I C F, we can find
that H E I are to the left of C and F is the right
subtree for C.
• The Binary tree upto this point looks like:
• To find the root, left and right sub trees for H E I:
• From the postorder sequence H I E, the root of the
tree is: E
• From the inorder sequence H E I, we can find that
H is left subtree for E and I is to the right of E.
• The Binary tree upto this point looks like:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy