AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
ExpressionTree.java
Go to the documentation of this file.
1package aud.example.expr;
2
3import aud.BinaryTree;
4
7
14public class ExpressionTree extends BinaryTree<AtomicExpression> {
17 ExpressionTree left,ExpressionTree right) {
18 super(atom,left,right);
19 if (atom.node_!=null)
20 throw new RuntimeException("ExpressionTree: AtomicExpression "+
21 "is already bound to another node!");
22 atom.node_=this;
23 }
26 this(atom,null,null);
27 }
28
34 public double getValue() { return getData().getValue(); }
35
43 @Override public void setData(AtomicExpression data) {
44 if (data.node_!=null)
45 throw new RuntimeException("ExpressionTree#setData: AtomicExpression "+
46 "is already bound to an ExpressionTree node!");
47 super.setData(data);
48 data.node_=this; // Note: Side effect on data!
49 }
50
51 @Override public String toString() {
52 if (getData().isTerminal())
53 return getData().toString();
54
55 assert(!isLeaf());
56 assert(getData().isOperator());
57
58 String term=(!isRoot() ? "(" : "");
59 if (getRight()!=null)
60 term+=getLeft().toString()+getData().toString()+getRight().toString();
61 else
62 term+=getData().toString()+getLeft().toString(); // unary operator
63
64 if (!isRoot()) term+=")";
65
66 return term;
67 }
68
69 static final GraphvizDecorator decorator = new SimpleDecorator();
70 @Override public GraphvizDecorator getDecorator() {
71 return decorator;
72 }
73
74
76 public static void main(String[] args) {
78 (new Minus(),
80 (new Times(),
81 new ExpressionTree(new Plus(),
82 new ExpressionTree(new Number(2)),
83 new ExpressionTree(new Number(3))),
84 new ExpressionTree(new Number(4))
85 ),
86 new ExpressionTree(new Number(5))
87 );
88
89 System.out.println(t);
90 System.out.println(t.getValue());
91 System.out.println(t.postorder().toString());
92
93 t=new ExpressionTree(new Plus(),
94 t,new ExpressionTree(new Symbol("x")));
95 System.out.println(t);
96 System.out.println(t.getValue()); // throws
97 }
98}
Simple binary tree.
Definition: BinaryTree.java:26
BinaryTree< T > getLeft()
get left child or null
Definition: BinaryTree.java:86
T getData()
get node data
Definition: BinaryTree.java:81
BinaryTreeTraversal< T >.Postorder postorder()
Get postorder iterator over nodes in tree .
BinaryTree< T > getRight()
get right child or null)
Definition: BinaryTree.java:88
boolean isLeaf()
Is this a leaf?
Definition: BinaryTree.java:96
boolean isRoot()
Iscode this} root?
Definition: BinaryTree.java:92
Superclass for data associated with a tree node.
ExpressionTree node_
uplink reference to node: node_.getData()==this @endiliteral
Tree representation of arithmetic expression.
GraphvizDecorator getDecorator()
get decoration or null
ExpressionTree(AtomicExpression atom)
create tree node
double getValue()
compute value of expression
void setData(AtomicExpression data)
Set AtomicExpression for this node.
String toString()
Get string presentation of node data.
static void main(String[] args)
test and example for usage
ExpressionTree(AtomicExpression atom, ExpressionTree left, ExpressionTree right)
creare and link tree node
binary - operator: A-B
Definition: Minus.java:4
Node representing constant number.
Definition: Number.java:6
binary + operator: A+B
Definition: Plus.java:4
Node representing a symbolic parameter, e.g., a varibale.
Definition: Symbol.java:6
binary * operator: A*B
Definition: Times.java:4
Decorator for items of Graphvizable objects.
Example for a simple decorator.
utilities (not related to AuD lecture)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.