Expression Trees: Data Structures & Algorithms
Expression Trees: Data Structures & Algorithms
+ + g
a * * f
b c d e
Expression Trees
• Inorder Traversal (left, Root, Right)
+
+ + g
a * * f
b c d e
Expression Trees
• Inorder Traversal (left, Root, Right)
– a+ b * c + d * e + f * g (infix notation)
+
*
+ + g
a * * f
b c d e
Expression Trees
• Preorder Traversal (Root, Left, Right)
+
+ + g
a * * f
b c d e
Expression Trees
• Preorder Traversal (Root, Left, Right)
– + + a * b c * + * d e f g (Prefix Notation)
+
*
+ + g
a * * f
b c d e
Expression Trees
• Postorder Traversal (Left, Right, Root)
+
+ + g
a * * f
b c d e
Expression Trees
• Postorder Traversal (Left, Right, Root)
– a b c * + d e * f + g * + (Postfix Notation)
+
*
+ + g
a * * f
b c d e
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+**
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** a, b are operands push pointers to
their nodes in the stack
b
a
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** + operator , pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.
a b
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** push pointers to the nodes of the
operands c, d & e
e
d
c
+
a b
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** + operator , pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.
c
+
+
d e
a b
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** * operator , pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.
c +
+
d e
a b
Expression Trees
• Postfix Expression to Expression Tree
–ab+cde+** * operator again, pop the last two
pointers, create a new expression
tree. Push pointer to its root back in
stack.
*
*
c +
+
d e
a b