-
Notifications
You must be signed in to change notification settings - Fork 28
/
TrinaryTreeTraversalDemo.java
171 lines (138 loc) · 4.51 KB
/
TrinaryTreeTraversalDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package ds_010_trees;
public class TrinaryTreeTraversalDemo {
public static void main(String[] args) {
/*
* 100
* |
* __________________|__________________
* | | |
* | | |
* 30 60 90
* ____|____ ____|____ ____|____
* | | | | | | | | |
* 12 17 28 35 39 55 67 88 89
* | |
* __|__ __|__
* | | | | | |
* 3 9 52
*
*/
// Level 0
TrinaryNode<Integer> root = new TrinaryNode<>(100);
// Level 1
root.left = new TrinaryNode<>(30);
root.middle = new TrinaryNode<>(60);
root.right = new TrinaryNode<>(90);
// Level 2
root.left.left = new TrinaryNode<>(12);
root.left.middle = new TrinaryNode<>(17);
root.left.right = new TrinaryNode<>(28);
root.middle.left = new TrinaryNode<>(35);
root.middle.middle = new TrinaryNode<>(39);
root.middle.right = new TrinaryNode<>(55);
root.right.left = new TrinaryNode<>(67);
root.right.middle = new TrinaryNode<>(88);
root.right.right = new TrinaryNode<>(89);
// Level 2
root.left.left.left = new TrinaryNode<>(3);
root.left.left.right = new TrinaryNode<>(9);
root.middle.right.right = new TrinaryNode<>(52);
printPreOrder(root); // first data
printPostOrder(root); // first children
printInOrderMiddleFirst(root); // left-middle-DATA-right
printInOrderMiddleLast(root); // left-DATA-middle-right
printBreadthFirst(root); // breadth-first level order
}
/*
* PreOrder Traversal Helper and Implementation
*/
private static <T> void printPreOrder(TrinaryNode<T> root) {
System.out.printf("Tree PreOrder : ");
preOrderTraversal(root);
System.out.print("\n");
}
private static <T> void preOrderTraversal(TrinaryNode<T> node) {
if(node == null) {
return;
}
System.out.printf("%3s ", node.data);
preOrderTraversal(node.left);
preOrderTraversal(node.middle);
preOrderTraversal(node.right);
}
/*
* PostOrder Traversal Helper and Implementation
*/
private static <T> void printPostOrder(TrinaryNode<T> root) {
System.out.printf("Tree PostOrder : ");
postOrderTraversal(root);
System.out.print("\n");
}
private static <T> void postOrderTraversal(TrinaryNode<T> node) {
if(node == null) {
return;
}
postOrderTraversal(node.left);
postOrderTraversal(node.middle);
postOrderTraversal(node.right);
System.out.printf("%3s ", node.data);
}
/*
* InOrder Traversal Helper and Implementation
* Mod 1: Middle Child Processed First
*/
private static <T> void printInOrderMiddleFirst(TrinaryNode<T> root) {
System.out.printf("Tree InOrder Middle First : ");
InOrderTraversalMiddleFirst(root);
System.out.print("\n");
}
private static <T> void InOrderTraversalMiddleFirst(TrinaryNode<T> node) {
if(node == null) {
return;
}
InOrderTraversalMiddleFirst(node.left);
InOrderTraversalMiddleFirst(node.middle);
System.out.printf("%3s ", node.data);
InOrderTraversalMiddleFirst(node.right);
}
/*
* InOrder Traversal Helper and Implementation
* Mod 2: Middle Child Processed Last
*/
private static <T> void printInOrderMiddleLast(TrinaryNode<T> root) {
System.out.printf("Tree InOrder Middle Last : ");
InOrderTraversalMiddleLast(root);
System.out.print("\n");
}
private static <T> void InOrderTraversalMiddleLast(TrinaryNode<T> node) {
if(node == null) {
return;
}
InOrderTraversalMiddleLast(node.left);
System.out.printf("%3s ", node.data);
InOrderTraversalMiddleLast(node.middle);
InOrderTraversalMiddleLast(node.right);
}
/*
* Breadth-First Traversal
*/
private static <T> void printBreadthFirst(TrinaryNode<T> root) {
System.out.print("Tree BreadthFirst : ");
SQueue<TrinaryNode<T>> queue = new SQueue<>();
queue.enqueue(root);
while(!queue.isEmpty()) {
TrinaryNode<T> curr = queue.dequeue();
System.out.printf("%3s ", curr.data);
if(curr.left != null) {
queue.enqueue(curr.left);
}
if(curr.middle != null) {
queue.enqueue(curr.middle);
}
if(curr.right != null) {
queue.enqueue(curr.right);
}
}
System.out.print("\n");
}
}