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

example: transform recursive preoder traversal to iterative algorithm More...

Static Public Member Functions

static void recursive_traversal (BinaryTree< String > node, int level)
 recursive algorithm More...
 
static void iterative_traversal (BinaryTree< String > root)
 iterative algorithm More...
 
static void level_order_traversal (BinaryTree< String > root)
 implement level-order traversal More...
 
static BinaryTree< String > exampleTree ()
 generate some tree More...
 
static void indent (int n)
 indent output on System.err
More...
 
static void main (String[] args)
 

Detailed Description

example: transform recursive preoder traversal to iterative algorithm

We use VerboseStack to show that recursive_traversal and iterative_traversal effectively do the same.

In addition, a level_order_traversal is obtained by replacing the stack by a VerboseQueue.

See also
BinaryTree
BinaryTreeTraversal

Definition at line 17 of file IterativePreorderTraversal.java.

Member Function Documentation

◆ exampleTree()

static BinaryTree< String > aud.example.IterativePreorderTraversal.exampleTree ( )
static

generate some tree

Definition at line 67 of file IterativePreorderTraversal.java.

67 {
68 BinaryTree<String> root=new BinaryTree<String>
69 ("A",
70 new BinaryTree<String>("B",
71 new BinaryTree<String>("C"),
72 new BinaryTree<String>("D")),
73 new BinaryTree<String>("E",
74 new BinaryTree<String>("F"),
75 new BinaryTree<String>("G",
76 new BinaryTree<String>("H",
77 new BinaryTree<String>("I",
78 new BinaryTree<String>("J"),null),
79 new BinaryTree<String>("K")
80 ),
81 null
82 )
83 )
84 );
85 return root;
86 }

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

+ Here is the caller graph for this function:

◆ indent()

static void aud.example.IterativePreorderTraversal.indent ( int  n)
static

indent output on System.err

Definition at line 89 of file IterativePreorderTraversal.java.

89 {
90 for (int i=0;i<n;++i)
91 System.err.print(" ");
92 }

Referenced by aud.example.IterativePreorderTraversal.recursive_traversal().

+ Here is the caller graph for this function:

◆ iterative_traversal()

static void aud.example.IterativePreorderTraversal.iterative_traversal ( BinaryTree< String >  root)
static

iterative algorithm

Definition at line 34 of file IterativePreorderTraversal.java.

34 {
35 VerboseStack<BinaryTree<String>> stack=
36 new VerboseStack<BinaryTree<String>>();
37 stack.push(root); // initialize
38
39 while(!stack.is_empty()) {
40 BinaryTree<String> node=stack.pop();
41 if (node!=null) {
42 System.out.println("output "+node);
43 stack.push(node.getRight()); // mind order: right first
44 stack.push(node.getLeft());
45 }
46 }
47 }

References aud.BinaryTree< T >.getLeft(), aud.BinaryTree< T >.getRight(), aud.Stack< T >.is_empty(), aud.example.VerboseStack< T >.pop(), and aud.example.VerboseStack< T >.push().

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

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

◆ level_order_traversal()

static void aud.example.IterativePreorderTraversal.level_order_traversal ( BinaryTree< String >  root)
static

implement level-order traversal

Definition at line 50 of file IterativePreorderTraversal.java.

50 {
51 VerboseQueue<BinaryTree<String>> queue=
52 new VerboseQueue<BinaryTree<String>>();
53 queue.enqueue(root); // initialize
54
55 while(!queue.is_empty()) {
56 BinaryTree<String> node=queue.dequeue();
57 if (node!=null) {
58 System.out.println("output "+node);
59 queue.enqueue(node.getLeft()); // mind order: left first
60 queue.enqueue(node.getRight());
61 }
62 }
63 }

References aud.example.VerboseQueue< T >.dequeue(), aud.example.VerboseQueue< T >.enqueue(), aud.BinaryTree< T >.getLeft(), aud.BinaryTree< T >.getRight(), and aud.Queue< T >.is_empty().

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

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

◆ main()

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

Definition at line 94 of file IterativePreorderTraversal.java.

94 {
95 BinaryTree<String> tree=exampleTree();
96
97 System.out.println("recursive traversal:");
98 recursive_traversal(tree,0);
99 System.out.println();
100
101 System.out.println("iterative traversal:");
103 System.out.println();
104
105 System.out.println("level order traversal:");
107 }
static void level_order_traversal(BinaryTree< String > root)
implement level-order traversal
static void recursive_traversal(BinaryTree< String > node, int level)
recursive algorithm
static BinaryTree< String > exampleTree()
generate some tree
static void iterative_traversal(BinaryTree< String > root)
iterative algorithm

References aud.example.IterativePreorderTraversal.exampleTree(), aud.example.IterativePreorderTraversal.iterative_traversal(), aud.example.IterativePreorderTraversal.level_order_traversal(), and aud.example.IterativePreorderTraversal.recursive_traversal().

+ Here is the call graph for this function:

◆ recursive_traversal()

static void aud.example.IterativePreorderTraversal.recursive_traversal ( BinaryTree< String >  node,
int  level 
)
static

recursive algorithm

Definition at line 21 of file IterativePreorderTraversal.java.

21 {
22 indent(level);
23 System.err.println("enter with node="+node);
24 if (node!=null) {
25 System.out.println("output "+node);
26 recursive_traversal(node.getLeft(),level+1);
27 recursive_traversal(node.getRight(),level+1);
28 }
29 indent(level);
30 System.err.println("leave with node="+node);
31 }
BinaryTree< T > getLeft()
get left child or null
Definition: BinaryTree.java:86
BinaryTree< T > getRight()
get right child or null)
Definition: BinaryTree.java:88
static void indent(int n)
indent output on System.err

References aud.BinaryTree< T >.getLeft(), aud.BinaryTree< T >.getRight(), aud.example.IterativePreorderTraversal.indent(), and aud.example.IterativePreorderTraversal.recursive_traversal().

Referenced by aud.example.IterativePreorderTraversal.main(), and aud.example.IterativePreorderTraversal.recursive_traversal().

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

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