Computer Science 105 (semester 1 2012)
Please send comments and corrections to your tutor
COMPSCI 105 Tutorial 10
Trees
[Part 1] An Expression Tree is a tree that contains a full mathematical expression in computer
memory as a tree. Since our mathematical operators (like addition and multiplication) are normally
binary (i.e. they have two arguments) the trees will be binary trees.
In an expression tree all the leaves are the Values and the nodes are the Operations.
Now consider a mathematical expression: ( ( 6 / 3 ) - ( 2 * 10 ) ). This is equivalent to the expression
tree
Notice that the root node is the operation evaluated last. Now try building some trees.
Show the expression tree equivalent to ( ( ( 6 - 3 ) * 12 ) / 4 ).
Computer Science 105 (semester 1 2012)
Please send comments and corrections to your tutor
Show the in-order traversal of the tree you constructed. Assume that the nodes’ value is printed
when they are visited in this ordering.
Show each operation in brackets, making the whole expression fully bracketed: to do this, consider
the “-” node: this evaluates the left child node as “6”, the right child node as “3” and itself as “-”,
when you compute the value of a node, delimit it recursively with brackets: “(6 - 3)”.
( ( ( 6 - 3 ) * 12 ) / 4 )
Show the pre-order traversal of the tree. Fully bracket the expression again.
( / ( * ( - 6 3 ) 12 ) 4 )
Show the post-order traversal of the tree. Again, fully bracket your expression.
( ( ( 6 3 - ) 12 * ) 4 / )
[Part 2] For this question our expression tree is going to be built using the following class TreeItem
that is from the book.
public class TreeNode {
private Object item;
private TreeNode leftChild;
private TreeNode rightChild;
public TreeNode(Object newItem) {...
public TreeNode(Object newItem, TreeNode left, TreeNode right) {...
public Object getItem() {...
public void setItem(Object newItem) {...
public TreeNode getLeft() {...
public void setLeft(TreeNode left) {...
public TreeNode getRight() {...
public void setRight(TreeNode right) {...
public String toString() {...
public boolean isLeaf() {...
}
Computer Science 105 (semester 1 2012)
Please send comments and corrections to your tutor
Build a method that returns the pre-order traversal of an expression tree as a string. Make sure that
you put the expression in brackets. Use Recursion.
public String preorder(TreeNode node) {
if (node.isLeaf())
return (String) node.getItem();
return
"( " + node.getItem() + " " +
preorder(node.getLeft()) + " " +
preorder(node.getRight()) + " )";
}
Build a method that returns the post-order traversal of an expression tree as a string. Make sure
that you put the expression in brackets. Use recursion.
public String postorder(TreeNode node) {
if (node.isLeaf())
return (String) node.getItem();
return
"( " + postorder (node.getLeft()) + " " +
postorder (node.getRight()) + " " +
node.getItem() + " )";
}
Build a method that returns the in-order traversal of an expression tree as a string. Make sure that
you put the expression in brackets. Use recursion.
public String inorder(TreeNode node) {
if (node.isLeaf())
return (String) node.getItem();
return
"( " + inorder (node.getLeft()) + " " +
node.getItem() + " " +
inorder (node.getRight()) + " )";
}
What is the only thing that changes for these three traversal methods?
The
For
For
For
order that the node’s value is added to the string.
pre-order is is first.
post-order it is last.
in-order it is inbetween.
Computer Science 105 (semester 1 2012)
Please send comments and corrections to your tutor
Show how to evaluate the expression tree for ( ( ( 6 - 3 ) * 12 ) / 4 )
Implement the evaluate method by using in-order traversal (hint, evaluate rather than print).
public int evaluateTree(TreeNode node) {
if (node.isLeaf())
return Integer.parseInt((String) node.getItem());
return evaluate( evaluateTree(node.getLeft()),
(String) node.getItem(),
evaluateTree(node.getRight())
);
}
Computer Science 105 (semester 1 2012)
Please send comments and corrections to your tutor
[Discussion] How would using interfaces allow me to add more operators without re-writing the
evaluate method? How would the code look?
public interface Operator{
public int eval(int left, int right);
}
public int evaluateTree2(TreeNode node) {
if (node.isLeaf())
return (Integer) node.getItem();
return ((Operator)node).eval(
evaluateTree2(node.getLeft()),
evaluateTree2(node.getRight()));
}
What would be the advantage of doing this over the switch statement in the evaluate method?
The actual operator is implemented by the method
eval(int,int). You can add new operators by extending the
class. This means that evaluate() does not need to be rewritten.
© Copyright 2026 Paperzz