CS210 – HW6 Due:Tuesday July 24th 11:59pm (Firm deadline).

advertisement
CS210 – HW6
Due:Tuesday July 24th 11:59pm (Firm deadline).
In this assignment we are going to represent a basic arithmetic expression using a tree. We assume
that the only operations involved in our expressions are /,*,- and +. The way that we show an
expression is very simple. Each operation is an internal node with two children, one for the left
operand and one for right operand. Each number (an integer) is represented with a leaf node.
Example: The infix expression ( (3 * 5) – (10 + 16) ) / -12 is represented with tree:
where rectangular nodes are leaf nodes corresponding to numbers and circular nodes are internal
nodes corresponding to operations. We call the above tree the Expression tree.
Note that different traversal of the same expression tree results in different presentations of the
same expression. For example a preorder traversal of the tree results in prefix notation of the
expression and postorder traversal leads to postfix notation of the arithmetic expression. The prefix
notation of the above expression is:
/ - * 3 5 + 10 16 -12
and the postfix notation of it is:
3 5 * 10 16 + - -12 /
To construct the tree in Java we define the nodes (internal node or leaf) of the tree by the Node
interface:
public interface Node {
//returns the String representation of the expression in prefix notation
public String prefix();
//returns the String representation of the expression in postfix notation
public String postfix();
//returns the height of the expression tree
public int height();
//returns the result of evaluating the expression.
public int evaluate();
}
1-Your job is to define Two classes which implement the Node interface: the InternalNode class
which represents an internal node of the tree and the Leaf class that represents a leaf of the tree.
The Leaf class has a single instance variable of type int which represents the number. The Internal
node has three instance variables:
public Node left; //left operand
public Node right; //right operand
public String op; //the operation
Note that the left and right operands are of type Node, since an operand can be an InternalNode or
a Leaf.
You need to implement the methods of the Node interface in both classes to perform the services
explained in the comments of this interface.
Here is a class name ExpressionTree that tests you implementation by creating two expression trees:
public class ExpressionTreeTest1 {
public static void main(String[] args) {
example1();
System.out.println("--------------------------------------------");
example2();
}
public static void example1() {
InternalNode node1 = new InternalNode(new Leaf(3), new Leaf(5), "*");
InternalNode node2 = new InternalNode(new Leaf(10), new Leaf(16), "+");
InternalNode node3 = new InternalNode(node1, node2, "-");
InternalNode root = new InternalNode(node3, new Leaf(-11), "/");
System.out.println("Prefix: " + root.prefix());
System.out.println("Postfix: " + root.postfix());
System.out.println("Height: " + root.height());
System.out.println("Expression Value: " + root.evaluate());
}
public static void example2() {
InternalNode node1 = new InternalNode(new Leaf(3), new Leaf(5), "+");
InternalNode node2 = new InternalNode(node1, new Leaf(16), "+");
InternalNode node3 = new InternalNode(node2, new Leaf(-92), "-");
InternalNode node4 = new InternalNode(node3, new Leaf(2), "/");
InternalNode node5 = new InternalNode(node4, new Leaf(21), "*");
InternalNode root = new InternalNode(node5, new Leaf(-7), "/");
System.out.println("Prefix: " + root.prefix());
System.out.println("Postfix: " + root.postfix());
System.out.println("Height: " + root.height());
System.out.println("Expression Value: " + root.evaluate());
}
}
The output of the execution of the above class is:
Prefix: / - * 3 5 + 10 16 -11
Postfix: 3 5 * 10 16 + - -11 /
Height: 3
Expression Value: 1
-------------------------------------------Prefix: / * / - + + 3 5 16 -92 2 21 -7
Postfix: 3 5 + 16 + -92 - 2 / 21 * -7 /
Height: 6
Expression Value: -174
Hint: As part of the implementation of the methods of the Node interface in InternalNode class, you
need to call the same method on its left and right children.
2-Your next task is to create a class name ExpressionTreeTest2 with a main method. Inside the
main method you need to create an expression tree of height at least 7 with at least 10 operations.
Then, it should print the prefix and postfix representations, height of the tree and the value of the
expression. Please specify the expression that corresponds to the tree in infix notation as comments.
3-(Optional)[40 points] Create a class name ExpressionTreeReader which accepts a valid postfix
expression from the command line and generates its corresponding Expression tree. Then it prints
the prefix and postfix representations, height of the tree and the value of the expression.
Hint: You need to create and use a stack similar to the algorithm we used for evaluating a postfix
expression. You just need to replace the numerical evaluation with creating a node (Leaf if we have
a number or InternalNode if we have an operation.)
To Be Delivered:
1. Leaf.java
2. InternalNode.java
3. ExpressionTreeTest2.java
4. ExpreesionTreeReader.java [optional]
Download