AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
BinaryTree.java
Go to the documentation of this file.
1package aud;
2
3
7import aud.util.Sys;
8
25public class BinaryTree<T>
27 BinaryTree<T> parent_= null;
28 BinaryTree<T> left_ = null;
29 BinaryTree<T> right_ = null;
30 T data_ = null;
31
33 //@<binarytree:ctor
34 public BinaryTree(T data) {
35 data_=data;
36 }
37 //@>binarytree:ctor
38
40 //@<binarytree:ctor2
41 public BinaryTree(T data,
42 BinaryTree<T> left,BinaryTree<T> right) {
43 this(data);
44 left_=left; right_=right;
45 if (left!=null) left_.parent_ =this;
46 if (right!=null) right_.parent_=this;
47 }
48 //@>binarytree:ctor2
49
53 //@<binarytree:setleft
54 public BinaryTree<T>
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 }
64 //@>binarytree:setleft
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 }
77
79 public void setData(T data) { data_=data; }
81 public T getData() { return data_; }
82
84 public BinaryTree<T> getParent() { return parent_; }
86 public BinaryTree<T> getLeft() { return left_; }
88 public BinaryTree<T> getRight() { return right_; }
89
91 //@<binarytree:isroot
92 public boolean isRoot() { return getParent()==null; }
93 //@>binarytree:isroot
95 //@<binarytree:isleaf
96 public boolean isLeaf() {
97 return getLeft()==null && getRight()==null;
98 }
99 //@>binarytree:isleaf
100
102 //@<binarytree:getroot
104 BinaryTree<T> node=this;
105 while (!node.isRoot())
106 node=node.getParent();
107 return node;
108 }
109 //@>binarytree:getroot
110
119 return new BinaryTreeTraversal<T>().preorder(this);
120 }
127 return new BinaryTreeTraversal<T>().inorder(this);
128 }
135 return new BinaryTreeTraversal<T>().postorder(this);
136 }
143 return new BinaryTreeTraversal<T>().levelorder(this);
144 }
145
154 @Override public String toString() {
155 return getData()!=null ? getData().toString() : "null";
156 }
157
158 @Override public GraphvizDecorator getDecorator() {
159 return null;
160 }
161
162 @Override public String toDot() {
163 String dot="graph BinaryTree {\n";
164
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 }
179
186 private String dotRef() {
187 return getData().toString()+"-"+hashCode();
188 }
189
190 /* get text representation of node for {@link #toDot}
191 @return {@code getData().toString()} as default
192 */
193 protected String dotLabel() { return getData().toString(); }
194
196 private String treeToDot() {
197 // Currently no "root" or "same rank" tags. Do we require them?
198
200 String dot =" \""+dotRef()+"\" [label=\""+dotLabel()+"\",";
201 dot+=(decorator!=null) ? decorator.getFullNodeDecoration(this) : "shape=circle";
202 dot+="];\n";
203
204 if (getLeft()!=null)
205 dot+=getLeft().treeToDot();
206 else
207 dot+=dotLeaf("left");
208
209 if (getRight()!=null)
210 dot+=getRight().treeToDot();
211 else
212 dot+=dotLeaf("right");
213
214 dot+="\n";
215
216 if (!isRoot()) { // edge is identified by "lower" node
217 dot+=" \""+getParent().dotRef()+"\" -- \""+
218 dotRef()+"\" ["; // no edge labels currently
219 dot+=(decorator!=null) ? decorator.getFullEdgeDecoration(this) : "";
220 dot+="];\n";
221 }
222
223 return dot;
224 }
225
227 private String dotLeaf(String side) {
228 String dummy="\""+dotRef()+"-"+side+"\"";
229 String dot=" "+dummy+" [shape=point];\n";
230 dot+=" \""+dotRef()+"\" -- "+dummy+" [];\n";
231 return dot;
232 }
233
235 public String toText() {
236 return toText(0);
237 }
238
240 private String toText(int level) {
241 String text="";
242 text+=Sys.indent(level)+"+- "+textLabel()+"\n";
243
244 if (getLeft()==null && getRight()==null)
245 return text;
246
247 if (getLeft()!=null)
248 text+=getLeft().toText(level+1);
249 else
250 text+=Sys.indent(level+1)+"+-\n";
251
252 if (getRight()!=null)
253 text+=getRight().toText(level+1);
254 else
255 text+=Sys.indent(level+1)+"+-\n";
256
257 return text;
258 }
259
263 protected String textLabel() { return getData().toString(); }
264
266 public String toTikZ() {
267 return "\\"+toTikZ(0)+";\n";
268 }
269 protected String tikzNodeStyle() { return ""; }
270 protected String toTikZ(int level) {
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 }
280
281 public static void main(String[] args) {
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 }
302}
helper: generates InorderIterator
helper: generates LevelorderIterator
helper: generates PostorderIterator
helper: generates PreorderIterator
Provide traversals of binary trees.
Postorder postorder(BinaryTree< T > tree)
return instance of generator
Preorder preorder(BinaryTree< T > tree)
return instance of generator
Inorder inorder(BinaryTree< T > tree)
return instance of generator
Levelorder levelorder(BinaryTree< T > tree)
return instance of generator
Simple binary tree.
Definition: BinaryTree.java:26
static void main(String[] args)
String toText()
get multiline text visualization
String toDot()
Get dot representation.
void setData(T data)
set node data
Definition: BinaryTree.java:79
BinaryTree< T > setRight(BinaryTree< T > tree)
set right subtree
Definition: BinaryTree.java:68
String dotLabel()
BinaryTreeTraversal< T >.Inorder inorder()
Get inorder iterator over nodes in tree .
BinaryTree< T > setLeft(BinaryTree< T > tree)
set left subtree
Definition: BinaryTree.java:55
BinaryTree(T data, BinaryTree< T > left, BinaryTree< T > right)
create new root node with children
Definition: BinaryTree.java:41
BinaryTree< T > getLeft()
get left child or null
Definition: BinaryTree.java:86
BinaryTree< T > getRoot()
traverse upwards to find root node
T getData()
get node data
Definition: BinaryTree.java:81
String toTikZ(int level)
String tikzNodeStyle()
BinaryTreeTraversal< T >.Preorder preorder()
Get preorder iterator over nodes in tree .
BinaryTreeTraversal< T >.Postorder postorder()
Get postorder iterator over nodes in tree .
String textLabel()
Get string representation of data in toText.
String toTikZ()
get TikZ code for LaTeX export
BinaryTree< T > getRight()
get right child or null)
Definition: BinaryTree.java:88
boolean isLeaf()
Is this a leaf?
Definition: BinaryTree.java:96
BinaryTree(T data)
create new root node without children
Definition: BinaryTree.java:34
String toString()
Get string presentation of node data.
BinaryTree< T > getParent()
get node's parent or null for root
Definition: BinaryTree.java:84
GraphvizDecorator getDecorator()
get decoration or null
BinaryTreeTraversal< T >.Levelorder levelorder()
Get level-order iterator over nodes in tree .
boolean isRoot()
Iscode this} root?
Definition: BinaryTree.java:92
Decorator for items of Graphvizable objects.
String getFullNodeDecoration(GraphvizDecorable object)
concatenates getAllNodesDecoration and getNodeDecoration
String getFullEdgeDecoration(GraphvizDecorable object)
concatenates getAllEdgesDecoration and getEdgeDecoration
String getGlobalStyle()
get global style (returns same for all nodes/edges)
String getGraphDecoration(GraphvizDecorable object)
get graph decoration (returns same for all nodes/edges)
System related utilities.
Definition: Sys.java:58
static String indent(int level)
get indentation string filled with spaces
Definition: Sys.java:183
Interface for decorating items of Graphvizable objects.
Interface for GraphViz rendering.
utilities (not related to AuD lecture)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.