FALLSEM2024-25 BCSE202L TH VL2024250101821 2024-09-24 Reference-Material-I

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Expression trees

 An expression tree is a binary tree


represents an arithmetic expression.
◦ In an expression tree each operator is an
interior node whose children are operands or
sub expressions. Operands are in leaf nodes.
 Example expression tree for 3 + ((5+9)*2)
would be:
Levels Indicate Precedence of
operation
 The levels of the nodes in the tree
indicate their relative precedence of
operation.(we do not need parentheses to
indicate precedence).
 Operator is placed in the leaf
node(high level) operation is
performed at first.
 Operator is placed in the root node
(level 0)operation is performed at
last.
A Binary Expression Tree
‘*’

‘+’ ‘3’

‘4’ ‘2’

What is the value of above tree?


( 4+2) * 3 =18
Leaf node operation first(high level)
1. 4 + 2 =6
Root node operation second(level 0)
2. 6* 3 = 18
5
Types of Expression trees
 Prefix expression:
Preorder Traversal-Root,Left,Right
 Infix expression :
Inorder Traversal-Left,Root,Right
 Postfix expression:
Post order Traversal-Left,Right,Root
Infix Expression
Inorder Traversal: (A + H) / (M - Y)
Print second
tree

‘/’

‘+’ ‘-’

‘A’ ‘H’ ‘M’ ‘Y’

Print left subtree first Print right subtree last


7
Prefix Expression
Preorder Traversal: / + A H - M Y
Print first
tree

‘/’

‘+’ ‘-’

‘A’ ‘H’ ‘M’ ‘Y’

Print left subtree second Print right subtree last


8
Postorder Expression
Postorder Traversal: A H + M Y - /
Print last
tree

‘/’

‘+’ ‘-’

‘A’ ‘H’ ‘M’ ‘Y’

Print left subtree first Print right subtree second


9
Building a Binary Expression Tree
Build an expression tree from a postfix
expression by following steps:
Read one symbol at a time from the postfix
expression.
 If the symbol is an operand, create a leaf node
whose value is the operand and whose left and
right subtrees are null. Push this node onto a
stack .
 If the symbol is an operator, create a new node
with the operator as its value. Pop the two
child nodes from the stack and attach them to
the new node.
◦ The first child popped from the stack becomes the
right subtree of the new node and the second child
popped from the stack becomes the left subtree.
Building a Binary Expression Tree
(continued)
a b c * +
Step1 Step3

Step2
Step4
ab+cde+**
 The first two symbols are operands, so we create one-node
trees and push pointers to them onto a stack.*

 Next, a '+' is read, so two pointers to trees are popped, a


new tree is formed,and a pointer to it is pushed onto the
stack.*
ab+cde+**
 Next, c, d, and e are read, and for each a one-node tree is
created and a pointer to the corresponding tree is pushed
onto the stack.

 Now a '+' is read, so two trees are merged.


ab+cde+**
 Continuing, a '*' is read, so we pop two tree pointers and
form a new tree with a '*' as root.

 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

6 is not a root and 6<8, traverse through the left subtree of


8
6 is not equal to 3 and 6>3, so traverse through the
right subtree of 3
6 is found
Recursive Find
10
Node *
Find(int key, Node * t)
5 15 {
if (t == NULL) return t;
2 9 20 else if (key < t->key)
return find(key, t->left);
7 17 30 else if (key > t->key)
return find(key, t->right);
else
return t;
}

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

 Whenever an element is to be inserted, first


locate its proper location.
 If the tree is empty set the data as root node.
If the tree is not empty then do the following.
 Start searching from the root node, then if the
data is less than the key value, search for the
empty location in the left subtree and insert the
data.
 Otherwise, search for the empty location in the
right subtree and insert the data.
construct a Binary Search Tree by inserting the
following sequence of numbers...
10,12,5,4,20,8,7,15 and 13
Insert Algorithm
Void insert(Comparable x, Node * t)
{
if ( t == NULL )
{
t = new Node(x);

}
else if (x < t->key)
{
insert( x, t->left );

}
else if (x > t->key)
{
insert( x, t->right );
Deletion Operation

 There are three cases for deleting a node


from a binary search tree.
 Case 1: Deleting a leaf node
 Case 2: Deleting a node with one child
 Case 3: Deleting a node with two
children
Case 1: Deleting a leaf node
In the first case, the node to be deleted is the leaf
node. In such a case, simply delete the node from
the tree.
For Example node 4 is to be deleted
Before deletion After deleion
Case 2: Deleting a node with one child
 In the second case, the node to be deleted
lies has a single child node. In such a case
follow the steps below:

1.Replace that node with its child node.

2.Remove the child node from its original


position.
For example 6 is to deleted
Before Deletion After deletion
Case 3: Deleting a node with two children
 In the third case, the node to be deleted has
two children. In such a case follow the steps
below:

1.Get the inorder successor of that node.

2.Replace the node with the inorder successor.

3.Remove the inorder successor from its


original position.

For example 3 is to deleted replace the inorder sucessor 4
is replaced after deletion

Before Deletion After deletion


Routine for deletion
struct node *deleteNode(struct node *root, int
key)
{
// Return if the tree is empty
if (root == NULL) return root;

// Find the node to be deleted


if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
// If the node is with no child
if(root->left == NULL &&
root→right==NULL) {
free(root);
else {
// If the node is with only one child or no
child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
// If the node has two children
struct node *temp = minValueNode(root->right);

// Place the inorder successor in position of the


node to be deleted
root->key = temp->key;

// Delete the inorder successor


root->right = deleteNode(root->right, temp->key);
}
return root;
}
// Find the inorder successor
struct node*minValueNode(struct
node *node)
{ struct node *current = node;
// Find the leftmost leaf
while (current && current->left !=
NULL)
current = current->left;
return current;}

You might also like