




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
In this document description about Expression Trees, In-Order Traversal , Pre-Order Traversal , Post-Order Traversal , binary tree , expression .
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Expression trees are useful as a vehicle for discussing the traversal of a
tree.
An expression tree is a binary tree which is used to represent a
mathematical expression.
For example, if we have the expression (2 * (4 + (5 + 3))), we could
construct a tree to represent it.
In an expression tree, the parent nodes are the operators, and the
children are the operands.
To find the result of this expression, we need to first solve (5 + 3), which
is 8, then solve (4 + 8), which is 12, and then finally solve 2 * 12, which is
So our root node will contain the operator within the outermost set of
parentheses, its left child will be the value "2", and the right child will be
the remaining expression that needs to be solved, which would be (4 +
In-Order Traversal
In an infix expression, the operator comes between its operands, so if we
want to generate the infix expression from an expression tree, we will need to
print the operand on the left before we print out the operator.
But what if the left operand is another expression to evaluate? We use
recursion.
We print out the entire left subtree, then print the current node, then print out
the entire right subtree.
void inOrder(BinaryTreeNode root)
{ if (null == root) return; inOrder(root.left()); // print the entire left subtree
System.out.println(root.data());
inOrder(root.right());
// print the entire right subtree return;
In this code, "root" refers to the root of the current subtree, not the root of the
whole tree
We start by calling inOrder(1) (the root).
(1) is not null, so we call inOrder(2) (1's left).
(2) is not null, so we call inOrder(2's left).
(2)'s left is null, so it just returns back to (2).
We've done the left, so now we print the data at (2), which in this case is "5".
We've printed (2), so now we call inOrder(2's right).
(2)'s right is null, so it just returns back to (2).
(2) has now finished, so it returns back to (1).
We've printed (1)'s left, so now we print the data at (1), which in this case is "+".
We've printed (1), so now we call inOrder(3).
(3) is not null, so we call inOrder(3's left).
(3)'s left is null, so it just returns back to (3).
We've done the left, so now we print the data at (3), which in this case is "3".
We've printed (3), so now we call inOrder(3's right).
(3)'s right is null, so it just returns back to (3).
(3) has now finished, so it returns back to (1).
(1) has now finished, so the result is "5+3", which is the infix representation of the tree.
Post-Order Traversal
By now, you should see a pattern. The last representation is postfix, and we will
use a post-order traversal to obtain it.
Since in postfix the operator comes after its two operands, will recursively print
the left and right subtrees before we print out the data at the current node.
void postOrder(BinaryTreeNode root)
if (null == root) return; postOrder(root.left()); // print the entire left subtree
postOrder(root.right()); // print the entire right subtree
System.out.println(root.data());
return;