-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathTree.java
More file actions
157 lines (117 loc) · 4.2 KB
/
Tree.java
File metadata and controls
157 lines (117 loc) · 4.2 KB
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
// submitted by xam4lor
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
public class Tree {
public Node root;
public Tree(int rowCount, int childrenCount) {
// this.root is the root node of the Tree
this.root = new Node(1);
this.createAllChildren(this.root, rowCount, childrenCount);
}
public void dfsRecursive() {
this.dfsRecursive(this.root);
}
private void dfsRecursive(Node node) {
System.out.println(node.id);
for (Node n : node.children) {
dfsRecursive(n);
}
}
public void dfsRecursivePostOrder() {
this.dfsRecursivePostOrder(this.root);
}
private void dfsRecursivePostOrder(Node node) {
for (Node n : node.children) {
dfsRecursivePostOrder(n);
}
// Here we are doing something ...
System.out.println(node.id);
}
public void dfsRecursiveInOrderBinary() {
dfsRecursiveInOrderBinary(this.root);
}
// This assumes only 2 children
private void dfsRecursiveInOrderBinary(Node node) {
if (node.children.size() > 2) {
System.err.println("Not a binary tree at dfsRecursiveInOrderBinary()!");
return;
}
if (node.children.size() > 1) {
dfsRecursiveInOrderBinary(node.children.get(0));
System.out.println(node.id);
dfsRecursiveInOrderBinary(node.children.get(1));
} else {
System.out.println(node.id);
}
}
public void dfsStack() {
Stack<Node> stack = new Stack<Node>();
stack.push(this.root);
Node tmp;
while (stack.size() != 0) {
System.out.println(stack.peek().id);
tmp = stack.pop();
for (Node c : tmp.children) {
stack.push(c);
}
}
}
public void bfsQueue() {
Queue<Node> queue = new PriorityQueue<Node>();
queue.add(this.root);
while (queue.size() != 0) {
System.out.println(queue.peek().id);
Node temp = queue.poll(); // return null if the queue is empty
if (temp != null) {
for (Node c : temp.children) {
queue.add(c);
}
}
}
}
private void createAllChildren(Node node, int rowCount, int childrenCount) {
if (rowCount <= 1) {
return;
}
for (int i = 0; i < childrenCount; i++) {
node.children.add(new Node(node.id * 10 + i + 1));
createAllChildren(node.children.get(i), rowCount - 1, childrenCount);
}
}
private class Node implements Comparable<Node> {
public ArrayList<Node> children;
public int id;
public Node(int id) {
this.children = new ArrayList<Node>();
this.id = id;
}
@Override
public int compareTo(Node other) {
// Need to implement Comparable<Node> and override this
// method because of the method BFSQueue() which uses Queues
// and must know how to check if two nodes are the same or not
return Integer.compare(this.id, other.id);
}
}
public static void main(String[] args) {
System.out.println("Creating Tree");
Tree tree = new Tree(3, 3);
System.out.println("Using recursive DFS :");
tree.dfsRecursive();
System.out.println("Using stack-based DFS :");
tree.dfsStack();
System.out.println("Using queue-based BFS :");
tree.bfsQueue();
System.out.println("Using post-order recursive DFS :");
tree.dfsRecursivePostOrder();
// Uncommenting the following 2 lines will result in an exception thrown because at least one Node of the Tree has more than 2 children and therefor a DFSRecursiveInorderBinary doesn't work.
System.out.println("Using in-order binary recursive DFS : (fail)");
tree.dfsRecursiveInOrderBinary();
tree = new Tree(3, 2);
System.out.println("Using in-order binary recursive DFS : (succeed)");
tree.dfsRecursiveInOrderBinary();
System.out.println("");
}
}