AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.BinaryTree< T > Class Template Reference

Simple binary tree. More...

+ Inheritance diagram for aud.BinaryTree< T >:
+ Collaboration diagram for aud.BinaryTree< T >:

Public Member Functions

 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...
 
String toDot ()
 Get dot representation. More...
 
GraphvizDecorator getDecorator ()
 get decoration 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 toString ()
 Get string presentation of node data. More...
 
GraphvizDecorator getDecorator ()
 get decoration or null
More...
 
String toDot ()
 Get dot representation. More...
 
String toText ()
 get multiline text visualization More...
 
String toTikZ ()
 get TikZ code for LaTeX export More...
 
String dotLabel ()
 
String textLabel ()
 Get string representation of data in toText. More...
 
String tikzNodeStyle ()
 
String toTikZ (int level)
 
static void main (String[] args)
 

Detailed Description

Simple binary tree.

The BinaryTree class represents a node and simultaneously its subtree. We do not explclitly distinguish between nodes and a (rooted) tree.

Used for demos (e.g., aud.example.BinaryTreeTraversal) and as base class for various binary trees.

For every node, we store an uplink to its parent, see getParent.

See also
aud.BinaryTreeTraversal BinaryTreeTraversal (iterators)
aud.example.BinaryTreeTraversal example.BinaryTreeTraversal (recursive traversal demo)

Definition at line 25 of file BinaryTree.java.

Constructor & Destructor Documentation

◆ BinaryTree() [1/2]

aud.BinaryTree< T >.BinaryTree ( data)

create new root node without children

Definition at line 34 of file BinaryTree.java.

34 {
35 data_=data;
36 }

◆ BinaryTree() [2/2]

aud.BinaryTree< T >.BinaryTree ( data,
BinaryTree< T >  left,
BinaryTree< T >  right 
)

create new root node with children

Definition at line 41 of file BinaryTree.java.

42 {
43 this(data);
44 left_=left; right_=right;
45 if (left!=null) left_.parent_ =this;
46 if (right!=null) right_.parent_=this;
47 }

Member Function Documentation

◆ dotLabel()

String aud.BinaryTree< T >.dotLabel ( )
protected

Definition at line 193 of file BinaryTree.java.

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

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

+ Here is the call graph for this function:

◆ getData()

◆ getDecorator()

GraphvizDecorator aud.BinaryTree< T >.getDecorator ( )

get decoration or null

Implements aud.util.GraphvizDecorable.

Reimplemented in aud.example.expr.ExpressionTree.

Definition at line 158 of file BinaryTree.java.

158 {
159 return null;
160 }

Referenced by aud.BinaryTree< T >.toDot().

+ Here is the caller graph for this function:

◆ getLeft()

◆ getParent()

BinaryTree< T > aud.BinaryTree< T >.getParent ( )

get node's parent or null for root

Definition at line 84 of file BinaryTree.java.

84{ return parent_; }

Referenced by aud.BinaryTree< T >.getRoot(), aud.BinaryTree< T >.isRoot(), and aud.test.BinaryTreeTest.testBinaryTree().

+ Here is the caller graph for this function:

◆ getRight()

◆ getRoot()

BinaryTree< T > aud.BinaryTree< T >.getRoot ( )

traverse upwards to find root node

Definition at line 103 of file BinaryTree.java.

103 {
104 BinaryTree<T> node=this;
105 while (!node.isRoot())
106 node=node.getParent();
107 return node;
108 }

References aud.BinaryTree< T >.getParent(), and aud.BinaryTree< T >.isRoot().

Referenced by aud.test.BinaryTreeTest.testBinaryTree().

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

◆ inorder()

BinaryTreeTraversal< T >.Inorder aud.BinaryTree< T >.inorder ( )

Get inorder iterator over nodes in tree .

Returns
instance of Iterable
See also
preorder
BinaryTreeTraversal.InorderIterator

Definition at line 126 of file BinaryTree.java.

126 {
127 return new BinaryTreeTraversal<T>().inorder(this);
128 }

References aud.BinaryTreeTraversal< T >.inorder().

Referenced by aud.BinaryTree< T >.main(), and aud.test.BinaryTreeTest.testBinaryTree().

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

◆ isLeaf()

boolean aud.BinaryTree< T >.isLeaf ( )

Is this a leaf?

Definition at line 96 of file BinaryTree.java.

96 {
97 return getLeft()==null && getRight()==null;
98 }
BinaryTree< T > getLeft()
get left child or null
Definition: BinaryTree.java:86
BinaryTree< T > getRight()
get right child or null)
Definition: BinaryTree.java:88

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

Referenced by aud.example.expr.AtomicExpression.isTerminal(), aud.test.BinaryTreeTest.testBinaryTree(), and aud.example.expr.ExpressionTree.toString().

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

◆ isRoot()

boolean aud.BinaryTree< T >.isRoot ( )

Iscode this} root?

Definition at line 92 of file BinaryTree.java.

92{ return getParent()==null; }
BinaryTree< T > getParent()
get node's parent or null for root
Definition: BinaryTree.java:84

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

Referenced by aud.BinaryTree< T >.getRoot(), aud.test.BinaryTreeTest.testBinaryTree(), and aud.example.expr.ExpressionTree.toString().

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

◆ levelorder()

BinaryTreeTraversal< T >.Levelorder aud.BinaryTree< T >.levelorder ( )

Get level-order iterator over nodes in tree .

Returns
instance of Iterable
See also
preorder
BinaryTreeTraversal.LevelorderIterator

Definition at line 142 of file BinaryTree.java.

142 {
143 return new BinaryTreeTraversal<T>().levelorder(this);
144 }

References aud.BinaryTreeTraversal< T >.levelorder().

Referenced by aud.BinaryTree< T >.main(), and aud.test.BinaryTreeTest.testBinaryTree().

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

◆ main()

static void aud.BinaryTree< T >.main ( String[]  args)
static

Reimplemented in aud.example.expr.ExpressionTree.

Definition at line 281 of file BinaryTree.java.

281 {
282 BinaryTree<String> a=new BinaryTree<String>("A");
283 BinaryTree<String> b=new BinaryTree<String>("B");
284 BinaryTree<String> c=new BinaryTree<String>("C");
285 BinaryTree<String> d=new BinaryTree<String>("D");
286
287 a.setLeft(b);
288 a.setRight(c);
289 c.setLeft(d);
290
291 System.out.println(a);
292 System.out.println(a.toText());
293 System.out.println(a.toTikZ());
294
295 System.out.println("preorder: "+a.preorder().toString());
296 System.out.println("inorder: "+a.inorder().toString());
297 System.out.println("postorder: "+a.postorder().toString());
298 System.out.println("level-order: "+a.levelorder().toString());
299
300 //System.out.println(a.toDot());
301 }

References aud.BinaryTree< T >.inorder(), aud.BinaryTree< T >.levelorder(), aud.BinaryTree< T >.postorder(), aud.BinaryTree< T >.preorder(), aud.BinaryTree< T >.setLeft(), aud.BinaryTree< T >.setRight(), aud.BinaryTreeTraversal< T >.Traversal.toString(), aud.BinaryTree< T >.toText(), and aud.BinaryTree< T >.toTikZ().

+ Here is the call graph for this function:

◆ postorder()

BinaryTreeTraversal< T >.Postorder aud.BinaryTree< T >.postorder ( )

Get postorder iterator over nodes in tree .

Returns
instance of Iterable
See also
preorder
BinaryTreeTraversal.PostorderIterator

Definition at line 134 of file BinaryTree.java.

134 {
135 return new BinaryTreeTraversal<T>().postorder(this);
136 }

References aud.BinaryTreeTraversal< T >.postorder().

Referenced by aud.BinaryTree< T >.main(), aud.example.expr.ExpressionParser.main(), aud.example.expr.ExpressionParser2.main(), aud.example.expr.ExpressionTree.main(), and aud.test.BinaryTreeTest.testBinaryTree().

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

◆ preorder()

BinaryTreeTraversal< T >.Preorder aud.BinaryTree< T >.preorder ( )

Get preorder iterator over nodes in tree .

Examples:

Iterator<BinaryTree<T> > ii=tree.preorder().iterator();

for (BinaryTree<T> node : tree.preorder()) { ... }

Returns
instance of Iterable
See also
BinaryTreeTraversal.PreorderIterator

Definition at line 118 of file BinaryTree.java.

118 {
119 return new BinaryTreeTraversal<T>().preorder(this);
120 }

References aud.BinaryTreeTraversal< T >.preorder().

Referenced by aud.BinaryTree< T >.main(), and aud.test.BinaryTreeTest.testBinaryTree().

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

◆ setData()

void aud.BinaryTree< T >.setData ( data)

set node data

Definition at line 79 of file BinaryTree.java.

79{ data_=data; }

◆ setLeft()

BinaryTree< T > aud.BinaryTree< T >.setLeft ( BinaryTree< T >  tree)

set left subtree

Returns
previous subtree or null

Definition at line 55 of file BinaryTree.java.

55 {
56 BinaryTree<T> old=left_;
57 left_=tree;
58 if (tree!=null)
59 tree.parent_=this;
60 if (old!=null)
61 old.parent_=null;
62 return old;
63 }

Referenced by aud.BinaryTree< T >.main(), and aud.test.BinaryTreeTest.testBinaryTree().

+ Here is the caller graph for this function:

◆ setRight()

BinaryTree< T > aud.BinaryTree< T >.setRight ( BinaryTree< T >  tree)

set right subtree

Returns
previous subtree or null

Definition at line 68 of file BinaryTree.java.

68 {
69 BinaryTree<T> old=right_;
70 right_=tree;
71 if (tree!=null)
72 tree.parent_=this;
73 if (old!=null)
74 old.parent_=null;
75 return old;
76 }

Referenced by aud.BinaryTree< T >.main(), and aud.test.BinaryTreeTest.testBinaryTree().

+ Here is the caller graph for this function:

◆ textLabel()

String aud.BinaryTree< T >.textLabel ( )
protected

Get string representation of data in toText.

Returns
getData().toString() (default implementation)

Definition at line 263 of file BinaryTree.java.

263{ return getData().toString(); }

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

+ Here is the call graph for this function:

◆ tikzNodeStyle()

String aud.BinaryTree< T >.tikzNodeStyle ( )
protected

Definition at line 269 of file BinaryTree.java.

269{ return ""; }

Referenced by aud.BinaryTree< T >.toTikZ().

+ Here is the caller graph for this function:

◆ toDot()

String aud.BinaryTree< T >.toDot ( )

Get dot representation.

Implements aud.util.Graphvizable.

Definition at line 162 of file BinaryTree.java.

162 {
163 String dot="graph BinaryTree {\n";
164
165 GraphvizDecorator decorator=getDecorator();
166 if (decorator!=null) {
167 String d=decorator.getGlobalStyle();
168 if (d!=null) dot+=" "+d+";\n";
169 d=decorator.getGraphDecoration(this);
170 if (d!=null) dot+=" graph ["+d+"];\n";
171 }
172
173 dot+=treeToDot();
174
175 dot+="\n}\n";
176 //System.out.println(dot);
177 return dot;
178 }
GraphvizDecorator getDecorator()
get decoration or null

References aud.BinaryTree< T >.getDecorator(), aud.util.GraphvizDecorator.getGlobalStyle(), and aud.util.GraphvizDecorator.getGraphDecoration().

+ Here is the call graph for this function:

◆ toString()

String aud.BinaryTree< T >.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 in aud.example.expr.ExpressionTree.

Definition at line 154 of file BinaryTree.java.

154 {
155 return getData()!=null ? getData().toString() : "null";
156 }

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

+ Here is the call graph for this function:

◆ toText()

String aud.BinaryTree< T >.toText ( )

get multiline text visualization

Definition at line 235 of file BinaryTree.java.

235 {
236 return toText(0);
237 }
String toText()
get multiline text visualization

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

Referenced by aud.BinaryTree< T >.main(), and aud.BinaryTree< T >.toText().

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

◆ toTikZ() [1/2]

String aud.BinaryTree< T >.toTikZ ( )

get TikZ code for LaTeX export

Definition at line 266 of file BinaryTree.java.

266 {
267 return "\\"+toTikZ(0)+";\n";
268 }
String toTikZ()
get TikZ code for LaTeX export

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

Referenced by aud.BinaryTree< T >.main(), aud.example.expr.ExpressionParser.main(), aud.example.expr.ExpressionParser2.main(), and aud.BinaryTree< T >.toTikZ().

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

◆ toTikZ() [2/2]

String aud.BinaryTree< T >.toTikZ ( int  level)
protected

Definition at line 270 of file BinaryTree.java.

270 {
271 String spaces=Sys.indent(level);
272 String tex=spaces+"node "+tikzNodeStyle()+" {"+getData().toString()+"}\n";
273 if (getLeft()!=null)
274 tex+=spaces+" child[left] {\n"+getLeft().toTikZ(level+1)+spaces+" }\n";
275 if (getRight()!=null)
276 tex+=spaces+" child[right] {\n"+getRight().toTikZ(level+1)+spaces+" }\n";
277
278 return tex;
279 }
String tikzNodeStyle()

References aud.BinaryTree< T >.getData(), aud.BinaryTree< T >.getLeft(), aud.BinaryTree< T >.getRight(), aud.util.Sys.indent(), and aud.BinaryTree< T >.tikzNodeStyle().

+ Here is the call graph for this function:

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