FALLSEM2024-25 BCSE202L TH VL2024250101821 2024-09-24 Reference-Material-I
FALLSEM2024-25 BCSE202L TH VL2024250101821 2024-09-24 Reference-Material-I
FALLSEM2024-25 BCSE202L TH VL2024250101821 2024-09-24 Reference-Material-I
‘+’ ‘3’
‘4’ ‘2’
‘/’
‘+’ ‘-’
‘/’
‘+’ ‘-’
‘/’
‘+’ ‘-’
Step2
Step4
ab+cde+**
The first two symbols are operands, so we create one-node
trees and push pointers to them onto a stack.*
Finally, the last symbol is read, two trees are merged, and a
pointer to the final tree is left on the stack.
Exercise
Draw the expression tree for the following
a b + c - d * e / f %
a b c d e f + - * / %
a b c + - d e f * / %
Data Structures
(Binary Search Tree)
Dr.N.S.Nithya
ASP/CSE
Binary Search Tree
A binary search tree is a binary tree in which satisfies the
following properties:
The key value of the left sub-tree is less than the value of
parent or root node's key.
The key value of the right sub-tree is greater than the value of
its parent or root node's key.
left_subtree (keys) < node (key) < right_subtree (keys)
Value 2 will be less than 3 so it is placed in left
subtree but it is located in right sub tree .
Can be used to build
◦ Dictionaries.
◦ Priority Queues.
Dictionary Data Structure:
Requirements
Fast insertion
Fast searching
Fast deletion
Construct a BST with nodes 2,4,5,7,1
Operations on a Binary Search
Tree
Operations on a binary tree require comparisons
between nodes. These comparisons are made with
calls to a comparator, which is a subroutine that
computes the total order (linear order) on any
two values.
Searching(Find, Find Min and Find Max)
Insertion
Deletion
Searching(Find, Find Min and Find
Max)
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.
Search the element 6
12
Iterative Find
10
Node *
find(Comparable key, Node * t)
5 15 {
while (t != NULL && t->key != key)
{
2 9 20 if (key < t->key)
t = t->left;
7 17 30 else
t = t->right;
}
return t;
}
Find Min value
Left most element in the left
subtree 10
Node * min(Node * t) { 5 15
if (t == NULL)
return t;
else 2 9 20
return min(t->left);
}
7 17 30
14
Find Max value
Right most element in the
Right subtree 10
Node * max(Node * t) { 5 15
if (t== NULL)
return t;
else 2 9 20
return max(t->right);
}
7 17 30
15
Insertion Operation in BST
}
else if (x < t->key)
{
insert( x, t->left );
}
else if (x > t->key)
{
insert( x, t->right );
Deletion Operation