AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.example.expr.ExpressionTree Class Reference

Tree representation of arithmetic expression. More...

+ Inheritance diagram for aud.example.expr.ExpressionTree:
+ Collaboration diagram for aud.example.expr.ExpressionTree:

Public Member Functions

 ExpressionTree (AtomicExpression atom, ExpressionTree left, ExpressionTree right)
 creare and link tree node More...
 
 ExpressionTree (AtomicExpression atom)
 create tree node More...
 
double getValue ()
 compute value of expression More...
 
void setData (AtomicExpression data)
 Set AtomicExpression for this node. More...
 
String toString ()
 Get string presentation of node data. More...
 
GraphvizDecorator getDecorator ()
 get decoration or null
More...
 
- Public Member Functions inherited from aud.BinaryTree< T >
 BinaryTree (T data)
 create new root node without children More...
 
 BinaryTree (T data, BinaryTree< T > left, BinaryTree< T > right)
 create new root node with children More...
 
BinaryTree< T > setLeft (BinaryTree< T > tree)
 set left subtree More...
 
BinaryTree< T > setRight (BinaryTree< T > tree)
 set right subtree More...
 
void setData (T data)
 set node data More...
 
getData ()
 get node data More...
 
BinaryTree< T > getParent ()
 get node's parent or null for root More...
 
BinaryTree< T > getLeft ()
 get left child or null
More...
 
BinaryTree< T > getRight ()
 get right child or null) More...
 
boolean isRoot ()
 Iscode this} root? More...
 
boolean isLeaf ()
 Is this a leaf? More...
 
BinaryTree< T > getRoot ()
 traverse upwards to find root node More...
 
BinaryTreeTraversal< T >.Preorder preorder ()
 Get preorder iterator over nodes in tree . More...
 
BinaryTreeTraversal< T >.Inorder inorder ()
 Get inorder iterator over nodes in tree . More...
 
BinaryTreeTraversal< T >.Postorder postorder ()
 Get postorder iterator over nodes in tree . More...
 
BinaryTreeTraversal< T >.Levelorder levelorder ()
 Get level-order iterator over nodes in tree . More...
 
String toDot ()
 Get dot representation. More...
 
String toText ()
 get multiline text visualization More...
 
String toTikZ ()
 get TikZ code for LaTeX export More...
 
String toDot ()
 Get dot representation. More...
 
GraphvizDecorator getDecorator ()
 get decoration or null
More...
 

Static Public Member Functions

static void main (String[] args)
 test and example for usage More...
 
- Static Public Member Functions inherited from aud.BinaryTree< T >

Additional Inherited Members

- Protected Member Functions inherited from aud.BinaryTree< T >
String dotLabel ()
 
String textLabel ()
 Get string representation of data in toText. More...
 
String tikzNodeStyle ()
 
String toTikZ (int level)
 

Detailed Description

Tree representation of arithmetic expression.

Each node is associated with an AtomicExpression, which can be either an Terminal or an Operator.

See also
AtomicExpression

Definition at line 14 of file ExpressionTree.java.

Constructor & Destructor Documentation

◆ ExpressionTree() [1/2]

creare and link tree node

Definition at line 16 of file ExpressionTree.java.

17 {
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 }

References aud.example.expr.AtomicExpression.node_.

◆ ExpressionTree() [2/2]

create tree node

Definition at line 25 of file ExpressionTree.java.

25 {
26 this(atom,null,null);
27 }

Member Function Documentation

◆ getDecorator()

GraphvizDecorator aud.example.expr.ExpressionTree.getDecorator ( )

get decoration or null

Reimplemented from aud.BinaryTree< T >.

Definition at line 70 of file ExpressionTree.java.

70 {
71 return decorator;
72 }

Referenced by aud.example.expr.ExpressionTreeTraversal.ExpressionTreeTraversal().

+ Here is the caller graph for this function:

◆ getValue()

double aud.example.expr.ExpressionTree.getValue ( )

compute value of expression

Exceptions
UnsupportedOperationExceptionif value cannot be determined
Returns
value obtained from getData()
See also
AtomicExpression::getValue

Definition at line 34 of file ExpressionTree.java.

34{ return getData().getValue(); }
T getData()
get node data
Definition: BinaryTree.java:81

References aud.BinaryTree< T >.getData().

Referenced by aud.example.expr.ExpressionTree.main(), and aud.example.expr.ExpressionTreeExample.main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ main()

static void aud.example.expr.ExpressionTree.main ( String[]  args)
static

test and example for usage

Reimplemented from aud.BinaryTree< T >.

Definition at line 76 of file ExpressionTree.java.

76 {
78 (new Minus(),
79 new ExpressionTree
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 }
ExpressionTree(AtomicExpression atom, ExpressionTree left, ExpressionTree right)
creare and link tree node

References aud.example.expr.ExpressionTree.getValue(), aud.BinaryTree< T >.postorder(), and aud.BinaryTreeTraversal< T >.Traversal.toString().

+ Here is the call graph for this function:

◆ setData()

void aud.example.expr.ExpressionTree.setData ( AtomicExpression  data)

Set AtomicExpression for this node.

Applies missing "uplink" to node for, i.e., this method manipulates its argument!

For this reason prefer constructing a new node/tree!

Definition at line 43 of file ExpressionTree.java.

43 {
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 }

References aud.example.expr.AtomicExpression.node_.

◆ toString()

String aud.example.expr.ExpressionTree.toString ( )

Get string presentation of node data.

This method only encodes the node's content as string, it does not encode the entire tree! (This makes it easier to, e.g., print out a Stack of nodes.)

Returns
getData.toString
See also
BinaryTreeTraversal.Traversal::toString

Reimplemented from aud.BinaryTree< T >.

Definition at line 51 of file ExpressionTree.java.

51 {
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 }
BinaryTree< T > getLeft()
get left child or null
Definition: BinaryTree.java:86
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

References aud.BinaryTree< T >.getData(), aud.BinaryTree< T >.getLeft(), aud.BinaryTree< T >.getRight(), aud.BinaryTree< T >.isLeaf(), and aud.BinaryTree< T >.isRoot().

+ Here is the call graph for this function:

The documentation for this class was generated from the following file: