SlideShare a Scribd company logo
Expression Trees
Algorithm for evaluating postfix
expressions
 Start scanning the postfix expressions from left to right
 If operand is encountered then push it on to the stack
 If an operator is encountered, pop last two operands from the
stack, apply the operator on the operands and then push the
resultant value on to the stack
 Finally the stack must contain the single value which is the result
Contd…
 Converting infix to postfix and then evaluating the postfix
expression is not useful
 It could be useful if we evaluate the postfix expression while it is
being constructed
 Using two stacks
 Operand stack and
 Operator stack will be useful
Infix to postfix- operand stack
Postfix evaluation- operator stack
Algorithm for evaluating the infix
expression
 Start scanning the infix expression from left to right
 If an operand is encountered, push it on to the operand stack
 If an operator is encountered, do the following
 Pop until operator stack top has higher or equal precedence than
current operator, before pushing the current operator on to the stack
 While popping an operator, pop last two operands from the operand
stack, apply the operator on the operands and then push the result on
to the operand stack
 Finally the operand stack has the single value which is the result.
Algorithm for constructing a
binary expression tree
 Start scanning the infix expression from left to right
 If an operand is encountered, create a binary node and push its
pointer onto the operand stack
 If an operator is encountered, create a binary node and do the
following
 Pop until operator stack top has higher or equal precedence than
current operator, before pushing the pointer on to the operand stack
 While popping an operator pointer, pop last two pointers from the
operand stack, and make the pointers left and right child and then
operator pointer on to the operand stack
 Finally top of the operand stack has the root of the expression
tree.
Note:
The only differences between last two algorithms are the
following.
instead of just pushing the operator / operand, we create
a binarynode and we push the pointers
instead of applying the operator on two operands, we
make the left and right child
instead of pushing the resultant value into the stack, we
push the operator node pointer into the operand stack.
A Binary Expression Tree is . . .
A special kind of binary tree in which:
1. Each leaf node contains a single operand
2. Each nonleaf node contains a single binary
operator
3. The left and right subtrees of an operator
node represent subexpressions that must be
evaluated before applying the operator at the
root of the subtree.
7
A Four-Level Binary Expression 8
‘*’
‘-’
‘8’ ‘5’
‘/’
‘+’
‘4’
‘3’
‘2’
Levels Indicate Precedence
The levels of the nodes in the tree indicate
their relative precedence of evaluation (we
do not need parentheses to indicate precedence).
Operations at higher levels of the tree are
evaluated later than those below them.
The operation at the root is always the last
operation performed.
9
A Binary Expression Tree
10
What value does it have?
( 4 + 2 ) * 3 = 18
‘*’
‘+’
‘4’
‘3’
‘2’
Easy to generate the infix, prefix, postfix
expressions (how?)
11
Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) )
Prefix: * - 8 5 / + 4 2 3
Postfix: 8 5 - 4 2 + 3 / *
‘*’
‘-’
‘8’ ‘5’
‘/’
‘+’
‘4’
‘3’
‘2’
Do by yourself
 Evaluate Following Postfix Expression:
(1) 3 * 2 + 4 * (A + B)
(2) A * (B + C) * D
(3) A * (B+C–D/E)/F
(4) A * B + (C – D/E)
(5) (a + b – c) * (e / f) – ( g – h/i)
(6) 1*1*8-5*(6-4*3-5*(7-(2*5)))
(7) (300+23)*(43-21)/(84+7)
(8) (4+8)*(6-5)/((3-2)*(2+2))
(9) 6-6/7-4*5/7-1/4*4
(10) 5*4-2+3-7+2/2*1/8/4
How many squares can you create in this figure by connecting any 4 dots (the
corners of a square must lie upon a grid dot?
TRIANGLES:
How many triangles are located in the image below?
There are 11 squares total; 5 small, 4 medium, and 2 large.
27 triangles. There are 16 one-cell
triangles, 7 four-cell triangles, 3 nine-
cell triangles, and 1 sixteen-cell
triangle.
GUIDED READING
MIND MAP
ASSESSMENT
1. The postfix expression for the infix expression A +
B*(C+D)/F + D*E is
A. AB+CD+*F/D+E*
B. ABCD+*F/DE*++
C. A*B+CD/F*DE++
D. A+*BCD/F*DE++
Contd..
2. The leaf nodes of binary expression tree contains
A. both operators and operands
B. only operators
C. only operands
D. neither operators nor operands
Contd..
3. The root of the expression tree of (a/b*c)+(5-4+(d*e+f)*g) is
A. +
B. *
C. -
D. /
Contd..
4. Which one of the following statement is correct.
A. ‘(‘ has highest precedence in the expression and lowest
precedence when present in the stack
B. ‘)‘ has highest precedence in the expression and lowest
precedence when present in the stack
C. ‘(‘ has lowest precedence in the expression and lowest
precedence when present in the stack
D. ‘(‘ has highest precedence in the expression and highest
precedence when present in the stack
Contd..
5. The traversal technique which output the postfix notation of
binary expression tree is
A. pre-order traversal
B. converse post-order traversal
C. post order traversal
D. converse pre-order traversal

More Related Content

What's hot

Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
Kamran Zafar
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
Anandhasilambarasan D
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
shameen khan
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
Meghaj Mallick
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Red black tree
Red black treeRed black tree
Red black tree
Dr Sandeep Kumar Poonia
 
Linked list
Linked listLinked list
Linked list
KalaivaniKS1
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
United International University
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
shameen khan
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
Heap tree
Heap treeHeap tree
Heap tree
JananiJ19
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
Ikhlas Rahman
 
Heaps
HeapsHeaps
Binary tree
Binary treeBinary tree
Binary tree
Rajendran
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
Dr. Jasmine Beulah Gnanadurai
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
Elavarasi K
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
Md. Israil Fakir
 

What's hot (20)

Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Red black tree
Red black treeRed black tree
Red black tree
 
Linked list
Linked listLinked list
Linked list
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Heap tree
Heap treeHeap tree
Heap tree
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
 
Heaps
HeapsHeaps
Heaps
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 

Viewers also liked

Computer notes - Expression Tree
Computer notes - Expression TreeComputer notes - Expression Tree
Computer notes - Expression Tree
ecomputernotes
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression tree
Shab Bi
 
Lec21
Lec21Lec21
Implementation of trees
Implementation of trees Implementation of trees
Implementation of trees
Mubashar Mehmood
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
Ahmed Khateeb
 
Binary tree in java
Binary tree in javaBinary tree in java
Binary tree in java
Programming Homework Help
 
Expression tree
Expression treeExpression tree
Expression tree
LearningTech
 
vim
vimvim
Stack and queue -polish notations
Stack and queue -polish notationsStack and queue -polish notations
Stack and queue -polish notations
jyoti_lakhani
 
Trees (slides)
Trees (slides)Trees (slides)
Trees (slides)
IIUM
 
Trees
Trees Trees
Trees
Gaditek
 
Discrete Mathematics Tree
Discrete Mathematics  TreeDiscrete Mathematics  Tree
Discrete Mathematics Tree
Masud Parvaze
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
Akhil Ahuja
 
문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?
Choi Man Dream
 
4.2 bst 03
4.2 bst 034.2 bst 03
4.2 bst 03
Krish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
Krish_ver2
 
4.1 webminig
4.1 webminig 4.1 webminig
4.1 webminig
Krish_ver2
 
2.4 mst prim &kruskal demo
2.4 mst  prim &kruskal demo2.4 mst  prim &kruskal demo
2.4 mst prim &kruskal demo
Krish_ver2
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithm
Krish_ver2
 
Top Forex Brokers
Top Forex BrokersTop Forex Brokers
Top Forex Brokers
Fxmoneyworld LTD
 

Viewers also liked (20)

Computer notes - Expression Tree
Computer notes - Expression TreeComputer notes - Expression Tree
Computer notes - Expression Tree
 
Binary expression tree
Binary expression treeBinary expression tree
Binary expression tree
 
Lec21
Lec21Lec21
Lec21
 
Implementation of trees
Implementation of trees Implementation of trees
Implementation of trees
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
Binary tree in java
Binary tree in javaBinary tree in java
Binary tree in java
 
Expression tree
Expression treeExpression tree
Expression tree
 
vim
vimvim
vim
 
Stack and queue -polish notations
Stack and queue -polish notationsStack and queue -polish notations
Stack and queue -polish notations
 
Trees (slides)
Trees (slides)Trees (slides)
Trees (slides)
 
Trees
Trees Trees
Trees
 
Discrete Mathematics Tree
Discrete Mathematics  TreeDiscrete Mathematics  Tree
Discrete Mathematics Tree
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?문제는 한글이 잘 구현되는가?
문제는 한글이 잘 구현되는가?
 
4.2 bst 03
4.2 bst 034.2 bst 03
4.2 bst 03
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
4.1 webminig
4.1 webminig 4.1 webminig
4.1 webminig
 
2.4 mst prim &kruskal demo
2.4 mst  prim &kruskal demo2.4 mst  prim &kruskal demo
2.4 mst prim &kruskal demo
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithm
 
Top Forex Brokers
Top Forex BrokersTop Forex Brokers
Top Forex Brokers
 

Similar to 1.4 expression tree

The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdf
arihantsherwani
 
2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf
aniarihant
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
LavanyaJ28
 
COMP1603 Stacks and RPN 2023 Recording (2).pptx
COMP1603 Stacks and RPN 2023 Recording (2).pptxCOMP1603 Stacks and RPN 2023 Recording (2).pptx
COMP1603 Stacks and RPN 2023 Recording (2).pptx
asdadad5
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
Yogesh Pawar
 
computer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionscomputer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressions
ecomputernotes
 
Stack application
Stack applicationStack application
Stack application
Ravi solanki
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
greatqadirgee4u
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
ashish bansal
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
JAYAPRIYAR7
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
shashankbhadouria4
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
KALPANAC20
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Using RustBecause postfix expressions are simpler and faster to ev.pdf
Using RustBecause postfix expressions are simpler and faster to ev.pdfUsing RustBecause postfix expressions are simpler and faster to ev.pdf
Using RustBecause postfix expressions are simpler and faster to ev.pdf
deepaksatrker
 
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt
partho5958
 
Applications of Stack
Applications of StackApplications of Stack
Applications of Stack
Christalin Nelson
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
sotlsoc
 
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfMerge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
mdameer02
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
mailmerk
 

Similar to 1.4 expression tree (20)

The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdf
 
2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
 
COMP1603 Stacks and RPN 2023 Recording (2).pptx
COMP1603 Stacks and RPN 2023 Recording (2).pptxCOMP1603 Stacks and RPN 2023 Recording (2).pptx
COMP1603 Stacks and RPN 2023 Recording (2).pptx
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
computer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionscomputer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressions
 
Stack application
Stack applicationStack application
Stack application
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Using RustBecause postfix expressions are simpler and faster to ev.pdf
Using RustBecause postfix expressions are simpler and faster to ev.pdfUsing RustBecause postfix expressions are simpler and faster to ev.pdf
Using RustBecause postfix expressions are simpler and faster to ev.pdf
 
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt
16-StacksQueuesCVCJUCGTCXYFRSTTIUGIUFTY.ppt
 
Applications of Stack
Applications of StackApplications of Stack
Applications of Stack
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
 
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfMerge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 

More from Krish_ver2

5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
Krish_ver2
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
Krish_ver2
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
Krish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
Krish_ver2
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
Krish_ver2
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
Krish_ver2
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
Krish_ver2
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
Krish_ver2
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
Krish_ver2
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
Krish_ver2
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
Krish_ver2
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
Krish_ver2
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
Krish_ver2
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing ext
Krish_ver2
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashing
Krish_ver2
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
Krish_ver2
 
4.2 bst 02
4.2 bst 024.2 bst 02
4.2 bst 02
Krish_ver2
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
Krish_ver2
 

More from Krish_ver2 (20)

5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing ext
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashing
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
 
4.2 bst 02
4.2 bst 024.2 bst 02
4.2 bst 02
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 

Recently uploaded

What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
christianmathematics
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
ArianaBusciglio
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 

Recently uploaded (20)

What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 

1.4 expression tree

  • 2. Algorithm for evaluating postfix expressions  Start scanning the postfix expressions from left to right  If operand is encountered then push it on to the stack  If an operator is encountered, pop last two operands from the stack, apply the operator on the operands and then push the resultant value on to the stack  Finally the stack must contain the single value which is the result
  • 3. Contd…  Converting infix to postfix and then evaluating the postfix expression is not useful  It could be useful if we evaluate the postfix expression while it is being constructed  Using two stacks  Operand stack and  Operator stack will be useful Infix to postfix- operand stack Postfix evaluation- operator stack
  • 4. Algorithm for evaluating the infix expression  Start scanning the infix expression from left to right  If an operand is encountered, push it on to the operand stack  If an operator is encountered, do the following  Pop until operator stack top has higher or equal precedence than current operator, before pushing the current operator on to the stack  While popping an operator, pop last two operands from the operand stack, apply the operator on the operands and then push the result on to the operand stack  Finally the operand stack has the single value which is the result.
  • 5. Algorithm for constructing a binary expression tree  Start scanning the infix expression from left to right  If an operand is encountered, create a binary node and push its pointer onto the operand stack  If an operator is encountered, create a binary node and do the following  Pop until operator stack top has higher or equal precedence than current operator, before pushing the pointer on to the operand stack  While popping an operator pointer, pop last two pointers from the operand stack, and make the pointers left and right child and then operator pointer on to the operand stack  Finally top of the operand stack has the root of the expression tree.
  • 6. Note: The only differences between last two algorithms are the following. instead of just pushing the operator / operand, we create a binarynode and we push the pointers instead of applying the operator on two operands, we make the left and right child instead of pushing the resultant value into the stack, we push the operator node pointer into the operand stack.
  • 7. A Binary Expression Tree is . . . A special kind of binary tree in which: 1. Each leaf node contains a single operand 2. Each nonleaf node contains a single binary operator 3. The left and right subtrees of an operator node represent subexpressions that must be evaluated before applying the operator at the root of the subtree. 7
  • 8. A Four-Level Binary Expression 8 ‘*’ ‘-’ ‘8’ ‘5’ ‘/’ ‘+’ ‘4’ ‘3’ ‘2’
  • 9. Levels Indicate Precedence The levels of the nodes in the tree indicate their relative precedence of evaluation (we do not need parentheses to indicate precedence). Operations at higher levels of the tree are evaluated later than those below them. The operation at the root is always the last operation performed. 9
  • 10. A Binary Expression Tree 10 What value does it have? ( 4 + 2 ) * 3 = 18 ‘*’ ‘+’ ‘4’ ‘3’ ‘2’
  • 11. Easy to generate the infix, prefix, postfix expressions (how?) 11 Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) ) Prefix: * - 8 5 / + 4 2 3 Postfix: 8 5 - 4 2 + 3 / * ‘*’ ‘-’ ‘8’ ‘5’ ‘/’ ‘+’ ‘4’ ‘3’ ‘2’
  • 12. Do by yourself  Evaluate Following Postfix Expression: (1) 3 * 2 + 4 * (A + B) (2) A * (B + C) * D (3) A * (B+C–D/E)/F (4) A * B + (C – D/E) (5) (a + b – c) * (e / f) – ( g – h/i) (6) 1*1*8-5*(6-4*3-5*(7-(2*5))) (7) (300+23)*(43-21)/(84+7) (8) (4+8)*(6-5)/((3-2)*(2+2)) (9) 6-6/7-4*5/7-1/4*4 (10) 5*4-2+3-7+2/2*1/8/4
  • 13. How many squares can you create in this figure by connecting any 4 dots (the corners of a square must lie upon a grid dot? TRIANGLES: How many triangles are located in the image below?
  • 14. There are 11 squares total; 5 small, 4 medium, and 2 large. 27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine- cell triangles, and 1 sixteen-cell triangle.
  • 17. ASSESSMENT 1. The postfix expression for the infix expression A + B*(C+D)/F + D*E is A. AB+CD+*F/D+E* B. ABCD+*F/DE*++ C. A*B+CD/F*DE++ D. A+*BCD/F*DE++
  • 18. Contd.. 2. The leaf nodes of binary expression tree contains A. both operators and operands B. only operators C. only operands D. neither operators nor operands
  • 19. Contd.. 3. The root of the expression tree of (a/b*c)+(5-4+(d*e+f)*g) is A. + B. * C. - D. /
  • 20. Contd.. 4. Which one of the following statement is correct. A. ‘(‘ has highest precedence in the expression and lowest precedence when present in the stack B. ‘)‘ has highest precedence in the expression and lowest precedence when present in the stack C. ‘(‘ has lowest precedence in the expression and lowest precedence when present in the stack D. ‘(‘ has highest precedence in the expression and highest precedence when present in the stack
  • 21. Contd.. 5. The traversal technique which output the postfix notation of binary expression tree is A. pre-order traversal B. converse post-order traversal C. post order traversal D. converse pre-order traversal