-
Notifications
You must be signed in to change notification settings - Fork 28
/
BinaryTreeTraversalDemo.java
118 lines (93 loc) · 2.48 KB
/
BinaryTreeTraversalDemo.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
package ds_010_trees;
public class BinaryTreeTraversalDemo {
public static void main(String[] args) {
/*
* 10
* / \
* / \
* 5 15
* / \ \
* 3 7 20
*/
// Level 0
BinaryNode<Integer> root = new BinaryNode<>(10);
// Level 1
root.left = new BinaryNode<>(5);
root.right = new BinaryNode<>(15);
// Level 2
root.left.left = new BinaryNode<>(3);
root.left.right = new BinaryNode<>(7);
root.right.right = new BinaryNode<>(20);
printPreOrder(root); // first data
printPostOrder(root); // first children
printInOrder(root); // left-order-right
printBreadthFirst(root); // breadth-first level order
}
/*
* PreOrder Traversal Helper and Implementation
*/
private static <T> void printPreOrder(BinaryNode<T> root) {
System.out.printf("Tree PreOrder : ");
preOrderTraversal(root);
System.out.print("\n");
}
private static <T> void preOrderTraversal(BinaryNode<T> node) {
if(node == null) {
return;
}
System.out.printf("%3s ", node.data);
preOrderTraversal(node.left);
preOrderTraversal(node.right);
}
/*
* PostOrder Traversal Helper and Implementation
*/
private static <T> void printPostOrder(BinaryNode<T> root) {
System.out.printf("Tree PostOrder : ");
postOrderTraversal(root);
System.out.print("\n");
}
private static <T> void postOrderTraversal(BinaryNode<T> node) {
if(node == null) {
return;
}
postOrderTraversal(node.left);
postOrderTraversal(node.right);
System.out.printf("%3s ", node.data);
}
/*
* PreOrder Traversal Helper and Implementation
*/
private static <T> void printInOrder(BinaryNode<T> root) {
System.out.printf("Tree InOrder : ");
InOrderTraversal(root);
System.out.print("\n");
}
private static <T> void InOrderTraversal(BinaryNode<T> node) {
if(node == null) {
return;
}
InOrderTraversal(node.left);
System.out.printf("%3s ", node.data);
InOrderTraversal(node.right);
}
/*
* Breadth-First Traversal
*/
private static <T> void printBreadthFirst(BinaryNode<T> root) {
System.out.print("Tree BreadthFirst: ");
SQueue<BinaryNode<T>> queue = new SQueue<>();
queue.enqueue(root);
while(!queue.isEmpty()) {
BinaryNode<T> curr = queue.dequeue();
System.out.printf("%3s ", curr.data);
if(curr.left != null) {
queue.enqueue(curr.left);
}
if(curr.right != null) {
queue.enqueue(curr.right);
}
}
System.out.print("\n");
}
}