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

example: visualize binary tree traversal More...

+ Inheritance diagram for aud.example.BinaryTreeTraversal:
+ Collaboration diagram for aud.example.BinaryTreeTraversal:

Classes

class  MyTree
 simple tree with decorator for visualization
 

Public Member Functions

 BinaryTreeTraversal (MyTree tree)
 create traversal application for tree
More...
 
void traverse (String type)
 start traversal More...
 
- Public Member Functions inherited from aud.util.SingleStepper
 SingleStepper (JFrame parent)
 create new instance More...
 
 SingleStepper (String caption)
 create new instance More...
 
JFrame parent ()
 get parent widget More...
 
void halt (String text, int timeout)
 display text and wait for user or timeout
More...
 
void setTimeout (int timeout)
 Set global timeout. More...
 
SingleStepper whereAmI ()
 print location of calling code More...
 
SingleStepper showSource ()
 jmp to caller's location in editor (emacs only) More...
 
void halt (String text)
 display text and wait for user (or global timeout) More...
 
void halt ()
 wait for user More...
 

Static Public Member Functions

static MyTree exampleTree ()
 generate some tree More...
 
static void main (String[] args)
 
static void main (String[] args)
 

Protected Member Functions

void onHalt ()
 
void output (MyTree node)
 output node during traversal More...
 
void see (MyTree node)
 arrived node for first time (for visualization) More...
 
void preorder (MyTree node)
 recursive preorder traversal More...
 
void inorder (MyTree node)
 recursive inorder traversal More...
 
void postorder (MyTree node)
 recursive postorder traversal More...
 
void levelorder (MyTree root)
 level order traversdal More...
 
- Protected Member Functions inherited from aud.util.SingleStepper
JComponent createComponents ()
 
void onNext ()
 call on button pressed More...
 
void println (String text)
 print to both, text area and stdout More...
 
void onHalt ()
 

Protected Attributes

MyTree tree_ = null
 
DotViewer viewer_
 
SimpleDecorator decorator
 
- Protected Attributes inherited from aud.util.SingleStepper
JFrame frame
 
JTextArea history
 
JButton next
 
Object monitor = new Object()
 
int timeout = 0
 

Detailed Description

example: visualize binary tree traversal

See also
aud.BinaryTree
aud.example.IterativePreorderTraversal
aud.BinaryTreeTraversal BinaryTreeTraversal (iterators)

Definition at line 12 of file example/BinaryTreeTraversal.java.

Constructor & Destructor Documentation

◆ BinaryTreeTraversal()

create traversal application for tree

Definition at line 20 of file example/BinaryTreeTraversal.java.

20 {
21 super("aud.example.BinaryTreeTraversal");
22 tree_=tree;
23 decorator=(SimpleDecorator) tree_.getDecorator(); // same per tree
24 }
Example for a simple decorator.

References aud.example.BinaryTreeTraversal.decorator, and aud.example.BinaryTreeTraversal.tree_.

Member Function Documentation

◆ exampleTree()

static MyTree aud.example.BinaryTreeTraversal.exampleTree ( )
static

generate some tree

Definition at line 45 of file example/BinaryTreeTraversal.java.

45 {
46 MyTree root=new MyTree
47 ("A",
48 new MyTree("B",
49 new MyTree("C"),
50 new MyTree("D")),
51 new MyTree("E",
52 new MyTree("F"),
53 new MyTree("G",
54 new MyTree("H",
55 new MyTree("I",new MyTree("J"),null),
56 new MyTree("K")
57 ),
58 null
59 )
60 )
61 );
62 return root;
63 }

Referenced by aud.example.BinaryTreeTraversal.main().

+ Here is the caller graph for this function:

◆ inorder()

void aud.example.BinaryTreeTraversal.inorder ( MyTree  node)
protected

recursive inorder traversal

Definition at line 123 of file example/BinaryTreeTraversal.java.

123 {
124 if (node!=null) {
125 see(node);
126
127 inorder((MyTree) node.getLeft());
128 output(node);
129 inorder((MyTree) node.getRight());
130 }
131 }
void output(MyTree node)
output node during traversal
void see(MyTree node)
arrived node for first time (for visualization)
void inorder(MyTree node)
recursive inorder traversal

References aud.example.BinaryTreeTraversal.inorder(), aud.example.BinaryTreeTraversal.output(), and aud.example.BinaryTreeTraversal.see().

Referenced by aud.example.BinaryTreeTraversal.inorder(), and aud.example.BinaryTreeTraversal.traverse().

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

◆ levelorder()

void aud.example.BinaryTreeTraversal.levelorder ( MyTree  root)
protected

level order traversdal

Definition at line 143 of file example/BinaryTreeTraversal.java.

143 {
144 Queue<MyTree> queue=new Queue<MyTree>();
145 queue.enqueue(null); // marker: next level
146 queue.enqueue(root);
147 int level=0; // keep track of/count levels
148 while (!queue.is_empty()) {
149 MyTree node=queue.dequeue();
150
151 if (node==null) {
152 halt("enter next level "+(level++));
153 if (!queue.is_empty())
154 queue.enqueue(null); // marker: next level
155 }
156 else {
157 see(node);
158
159 if (node.getLeft()!=null)
160 queue.enqueue((MyTree) node.getLeft());
161 if (node.getRight()!=null)
162 queue.enqueue((MyTree) node.getRight());
163 output(node);
164 }
165 }
166 }
void halt()
wait for user

References aud.Queue< T >.dequeue(), aud.Queue< T >.enqueue(), aud.util.SingleStepper.halt(), aud.Queue< T >.is_empty(), aud.example.BinaryTreeTraversal.output(), and aud.example.BinaryTreeTraversal.see().

Referenced by aud.example.BinaryTreeTraversal.traverse().

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

◆ main()

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

Reimplemented from aud.util.SingleStepper.

Definition at line 168 of file example/BinaryTreeTraversal.java.

168 {
170 new BinaryTreeTraversal(BinaryTreeTraversal.exampleTree());
171
172 if (args.length==0)
173 app.traverse("preorder");
174 else
175 for (String type : args)
176 app.traverse(type);
177
178 app.halt("QUIT");
179 System.exit(0);
180 }
BinaryTreeTraversal(MyTree tree)
create traversal application for tree

References aud.example.BinaryTreeTraversal.exampleTree(), aud.util.SingleStepper.halt(), and aud.example.BinaryTreeTraversal.traverse().

+ Here is the call graph for this function:

◆ onHalt()

void aud.example.BinaryTreeTraversal.onHalt ( )
protected

Reimplemented from aud.util.SingleStepper.

Definition at line 26 of file example/BinaryTreeTraversal.java.

26 {
27 if (tree_!=null)
29 }
void display(String code)
display dot code
Definition: DotViewer.java:108

References aud.util.DotViewer.display(), aud.example.BinaryTreeTraversal.tree_, and aud.example.BinaryTreeTraversal.viewer_.

+ Here is the call graph for this function:

◆ output()

void aud.example.BinaryTreeTraversal.output ( MyTree  node)
protected

◆ postorder()

void aud.example.BinaryTreeTraversal.postorder ( MyTree  node)
protected

recursive postorder traversal

Definition at line 133 of file example/BinaryTreeTraversal.java.

133 {
134 if (node!=null) {
135 see(node);
136
137 postorder((MyTree) node.getLeft());
138 postorder((MyTree) node.getRight());
139 output(node);
140 }
141 }
void postorder(MyTree node)
recursive postorder traversal

References aud.example.BinaryTreeTraversal.output(), aud.example.BinaryTreeTraversal.postorder(), and aud.example.BinaryTreeTraversal.see().

Referenced by aud.example.BinaryTreeTraversal.postorder(), and aud.example.BinaryTreeTraversal.traverse().

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

◆ preorder()

void aud.example.BinaryTreeTraversal.preorder ( MyTree  node)
protected

recursive preorder traversal

Definition at line 113 of file example/BinaryTreeTraversal.java.

113 {
114 if (node!=null) {
115 see(node);
116
117 output(node);
118 preorder((MyTree) node.getLeft());
119 preorder((MyTree) node.getRight());
120 }
121 }
void preorder(MyTree node)
recursive preorder traversal

References aud.example.BinaryTreeTraversal.output(), aud.example.BinaryTreeTraversal.preorder(), and aud.example.BinaryTreeTraversal.see().

Referenced by aud.example.BinaryTreeTraversal.preorder(), and aud.example.BinaryTreeTraversal.traverse().

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

◆ see()

void aud.example.BinaryTreeTraversal.see ( MyTree  node)
protected

arrived node for first time (for visualization)

Definition at line 76 of file example/BinaryTreeTraversal.java.

76 {
77 decorator.markEdge(node);
78 }
void markEdge(GraphvizDecorable object)

References aud.example.BinaryTreeTraversal.decorator, and aud.util.SimpleDecorator.markEdge().

Referenced by aud.example.BinaryTreeTraversal.inorder(), aud.example.BinaryTreeTraversal.levelorder(), aud.example.BinaryTreeTraversal.postorder(), and aud.example.BinaryTreeTraversal.preorder().

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

◆ traverse()

void aud.example.BinaryTreeTraversal.traverse ( String  type)

start traversal

Parameters
typedenotes the type of traversal "preorder", "inorder","postorder"
Exceptions
RuntimeExceptionfor unknown type

Definition at line 85 of file example/BinaryTreeTraversal.java.

85 {
87 decorator.setGraphLabel("Traversal: ");
88
89 if (type.toLowerCase().startsWith("pre")) {
90 halt("START preorder traversal");
92 }
93 else if (type.toLowerCase().startsWith("in")) {
94 halt("START inorder traversal");
96 }
97 else if (type.toLowerCase().startsWith("post")) {
98 halt("START postorder traversal");
100 }
101 else if (type.toLowerCase().startsWith("level")) {
102 halt("START level-order traversal");
104 }
105 else
106 throw new RuntimeException("unknown traversal '"+type+"'");
107
109 halt("FINISHED");
110 }
void levelorder(MyTree root)
level order traversdal
void clear()
Clear all decorations.

References aud.util.CommonGraphvizDecorator.clear(), aud.example.BinaryTreeTraversal.decorator, aud.util.SingleStepper.halt(), aud.util.SimpleDecorator.highlightNode(), aud.example.BinaryTreeTraversal.inorder(), aud.example.BinaryTreeTraversal.levelorder(), aud.example.BinaryTreeTraversal.postorder(), aud.example.BinaryTreeTraversal.preorder(), aud.util.CommonGraphvizDecorator.setGraphLabel(), and aud.example.BinaryTreeTraversal.tree_.

Referenced by aud.example.BinaryTreeTraversal.main().

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

Member Data Documentation

◆ decorator

◆ tree_

MyTree aud.example.BinaryTreeTraversal.tree_ = null
protected

◆ viewer_

DotViewer aud.example.BinaryTreeTraversal.viewer_
protected
Initial value:
= DotViewer.displayWindow
((String) null,"aud.example.BinaryTreeTraversal")

Definition at line 15 of file example/BinaryTreeTraversal.java.

Referenced by aud.example.BinaryTreeTraversal.onHalt().


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