-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathTree.java
127 lines (98 loc) · 3.23 KB
/
Tree.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
// 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);
}
}
}