0% found this document useful (0 votes)
13 views23 pages

DS Unit2 BST

The document provides an overview of Binary Search Trees (BST), including their definition, construction, operations, and time complexity. It explains how nodes are organized, the process of inserting and deleting nodes, and the different traversal methods (preorder, inorder, postorder). Additionally, it discusses the time complexity of BST operations in both best and worst-case scenarios.

Uploaded by

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

DS Unit2 BST

The document provides an overview of Binary Search Trees (BST), including their definition, construction, operations, and time complexity. It explains how nodes are organized, the process of inserting and deleting nodes, and the different traversal methods (preorder, inorder, postorder). Additionally, it discusses the time complexity of BST operations in both best and worst-case scenarios.

Uploaded by

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

Binary Search

Binary Search Tree

Tree
Learning Objectives

At the end of this session, you will be able to:


• Define the Binary Search Tree
• Construction of Binary Search tree
• Operations on Binary Search Tree
• Time Complexity of BST operations
Binary Search Tree

Binary Search Tree is a special kind of binary tree in which nodes are
arranged in a specific order. In a binary search tree (BST), each node
contains-
1. Only smaller values in its left sub tree
2. Only larger values in its right sub tree
We can represent such a tree by a
linked data structure in which each
node is an object.
In addition to a key and satellite
data, each node contains attributes
left, right, and p that point to the
nodes corresponding to its left child,
its right child, and its parent,
respectively. If a child or the parent is
missing, the appropriate attribute
contains the value NIL. The root node
Binary Search Tree

umber of Binary Search Trees-

Number of distinct binary search trees possible with 3 distinct keys


= 2×3C3 / 3+1
= 6C3 / 4
=5

If three distinct keys are A, B and C, then 5 distinct binary search trees are-
Binary Search Tree
Binary Search Tree Construction
Construct a Binary Search Tree (BST) for the following sequence of
numbers-

50, 70, 60, 20, 90, 10, 40, 100


When elements are given in a sequence,
Always consider the first element as the root node.
Consider the given elements and insert them in the BST one by one.
The binary search tree will be constructed as explained below-
Insert 50

ow inert 70
70 > 50, so insert 70 to the right of 50.
Binary Search Tree Construction

Now Insert 60-


As 60 > 50, so insert 60 to the right
of 50.
As 60 < 70, so insert 60 to the left of
70.

Now insert 20-


As 20 < 50, so insert 20 to the left of
50.
Binary Search Tree Construction

Now insert 90-


As 90 > 50, so insert 90 to the right
of 50.
As 90 > 70, so insert 90 to the right
of 70.

Now insert 10-


As 10 < 50, so insert 10 to the left of
50.
As 10 < 20, so insert 10 to the left of
20.
Binary Search Tree Construction

Now insert 40-


As 40 < 50, so insert 40 to the left of
50.
As 40 > 20, so insert 40 to the right
of 20.

Now insert 100-


As 100 > 50, so insert 100 to the
right of 50.
As 100 > 70, so insert 100 to the
right of 70.
As 100 > 90, so insert 100 to the
right of 90.
Binary Search Tree Traversal

Preorder Traversal:

100 , 20 , 10 , 30 , 200 , 150 , 300

Rule: Root Left Right

Root is 100
In left 20 is again parent(like root)
Then left node is 10 and right node
Is 30.
Similarly in right 200 is parent
Then left node is 150 and then right
Node is 300.
Binary Search Tree Traversal

Inorder Traversal-
10 , 20 , 30 , 100 , 150 , 200 , 300

Rule: Left Root Right

First left node is 10 then root (parent)


is 20
And then right node 30.
Then 100 is root node and then 150 is
left node and 200 is root(parent) and
300 is right
Node.
Binary Search Tree Traversal

Postorder Traversal-

10 , 30 , 20 , 150 , 300 , 200 , 100

Rule: Left Right Root

First left node is 10 and right node is


30
Then root (parent) is 20.
Now in right subtree 150 is left node,
300 is right node and 200 is
root(parent) and finally
100 is root.
Binary Search Tree Operations

Search Operation is performed to search a particular element in the Binary


Search Tree

Rules-

For searching a given key in the BST,


1. Compare the key with the value of root node.
2. If the key is present at the root node, then return the root node.
3. If the key is greater than the root node value, then recur for the root
node’s right subtree.
4. If the key is smaller than the root node value, then recur for the root
node’s left subtree.
Binary Search Tree Operation

Consider key = 45 has to


be searched in the given
BST-
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 Operation

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 inserted from the root node till some leaf We start
node is reached. searching for
Once a leaf node is reached, insert the key as child of that leafvalue 40 from
node. the root node
Consider the following example where key = 40 is inserted in the 100.
given BST- As 40 < 100,
so we search
in 100’s left
subtree.
As 40 > 20, so
we search in
20’s right
subtree.
Binary Search Tree Operation

When it comes to deleting a node from the binary search tree, following
three cases are possible-
Case-01: Deletion Of A Node Having No Child (Leaf Node)-
Just remove / disconnect the leaf node that is to deleted from the tree.
Consider the following example where node with value = 20 is deleted from
the BST-
Binary Search Tree Operation

Case-02: Deletion Of A Node Having Only One Child-


Just make the child of the deleting node, the child of its grandparent.
Consider the following example where node with value = 30 is deleted from
the BST-
Binary Search Tree Operation

Deletion Of A Node Having Two Children-


A node with two children may be deleted from the BST in the following
ways-
Visit to the right subtree of the deleting node.
Pluck the least value element called as inorder successor.
Replace the deleting element with its inorder successor.
Binary Search Tree Operation

Time Complexity:
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 i.e. number of nodes.
So, Time complexity of BST Operations
= O(n).
In this case, binary search tree is as
good as unordered list with no benefits.
Binary Search Tree Operation

In best case,

The binary search tree is


a balanced binary search
tree.
Height of the binary
search tree becomes
log(n).
So, Time complexity of
BST Operations =
O(logn)
Summary

In this session, we have discussed about:

• Binary Search Tree


• Construction of Binary Search tree
• Operations on Binary Search Tree
• Time Complexity of BST operations
.
MCQ
Q.1. 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

Answer: c
MCQ

Q.2. The preorder traversal sequence of a binary search tree is-

30 , 20 , 10 , 15 , 25 , 23 , 39 , 35 , 42

Which one of the following is the postorder traversal sequence of the


above 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

Answer: d

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