forked from rangken/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreePrinter.java
More file actions
172 lines (152 loc) · 5 KB
/
Copy pathBinaryTreePrinter.java
File metadata and controls
172 lines (152 loc) · 5 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
public class BinaryTreePrinter{
static public <T> void printPreOrder(BinaryTree.Node<T> node){
if(node == null){
return;
}
node.visit();
System.out.print(" ");
printPreOrder(node.left);
printPreOrder(node.right);
}
static public <T> void printInOrder(BinaryTree.Node<T> node){
if(node == null){
return;
}
printInOrder(node.left);
node.visit();
System.out.print(" ");
printInOrder(node.right);
}
static public <T> void printPostOrder(BinaryTree.Node<T> node){
if(node == null){
return;
}
printPostOrder(node.left);
printPostOrder(node.right);
node.visit();
System.out.print(" ");
}
static public <T> void printLevelOrder(BinaryTree.Node<T> node){
// 레벨 별로 순회 하는 방법
java.util.Queue<BinaryTree.Node<T>> queue = new java.util.LinkedList<BinaryTree.Node<T>>();
queue.offer(node);
while(queue.peek() != null){
BinaryTree.Node<T> t = queue.poll();
t.visit();
System.out.print(" ");
if(t.left != null){
queue.offer(t.left);
}
if(t.right != null){
queue.offer(t.right);
}
}
}
static public <T> void printBFS(BinaryTree.Node<T> node){
java.util.Stack<BinaryTree.Node<T>> stack = new java.util.Stack<BinaryTree.Node<T>>();
stack.push(node);
while(!stack.isEmpty()){
BinaryTree.Node<T> t = stack.pop();
t.visit();
System.out.print(" ");
if(t.right != null){
stack.push(t.right);
}
if(t.left != null){
stack.push(t.left);
}
}
}
static public <T> void printInternal(java.util.List<BinaryTree.Node<T>> nodes,int level, int maxLevel){
if (nodes.isEmpty() || isAllElementsNull(nodes))
return;
int floor = maxLevel - level;
int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0)));
int firstSpaces = (int) Math.pow(2, (floor)) - 1;
int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1;
printWhitespaces(firstSpaces);
java.util.List<BinaryTree.Node<T>> newNodes = new java.util.ArrayList<BinaryTree.Node<T>>();
for (BinaryTree.Node<T> node : nodes) {
if (node != null) {
System.out.print(node.element);
newNodes.add(node.left);
newNodes.add(node.right);
} else {
newNodes.add(null);
newNodes.add(null);
System.out.print(" ");
}
printWhitespaces(betweenSpaces);
}
System.out.println("");
for (int i = 1; i <= endgeLines; i++) {
for (int j = 0; j < nodes.size(); j++) {
printWhitespaces(firstSpaces - i);
if (nodes.get(j) == null) {
printWhitespaces(endgeLines + endgeLines + i + 1);
continue;
}
if (nodes.get(j).left != null)
System.out.print("/");
else
printWhitespaces(1);
printWhitespaces(i + i - 1);
if (nodes.get(j).right != null)
System.out.print("\\");
else
printWhitespaces(1);
printWhitespaces(endgeLines + endgeLines - i);
}
System.out.println("");
}
printInternal(newNodes, level + 1, maxLevel);
}
static public <T> void print(BinaryTree.Node<T> node){
int maxLevel = getMaxLevel(node);
java.util.LinkedList<BinaryTree.Node<T>> rootList = new java.util.LinkedList<BinaryTree.Node<T>>();
rootList.add(node);
printInternal(rootList,1,maxLevel);
}
static public <T> void printTest(BinaryTree.Node<T> node){
java.util.LinkedList<BinaryTree.Node<T>> curLevel = new java.util.LinkedList<BinaryTree.Node<T>>();
java.util.LinkedList<BinaryTree.Node<T>> nextLevel = curLevel;
StringBuilder sb = new StringBuilder();
curLevel.add(node);
sb.append(node.element + "\n");
while(nextLevel.size() > 0){
nextLevel = new java.util.LinkedList<BinaryTree.Node<T>>();
for (int i = 0; i < curLevel.size(); i++){
BinaryTree.Node<T> cur = curLevel.get(i);
if (cur.left != null) {
nextLevel.add(cur.left);
sb.append(cur.left.element + " ");
}
if (cur.right != null) {
nextLevel.add(cur.right);
sb.append(cur.right.element + " ");
}
}
if (nextLevel.size() > 0) {
sb.append("\n");
curLevel = nextLevel;
}
}
System.out.println(sb.toString());
}
private static <T> boolean isAllElementsNull(java.util.List<T> list) {
for (Object object : list) {
if (object != null)
return false;
}
return true;
}
private static <T> void printWhitespaces(int count) {
for (int i = 0; i < count; i++)
System.out.print(" ");
}
protected static <T> int getMaxLevel(BinaryTree.Node<T> node){
if(node == null)
return 0;
return java.lang.Math.max(getMaxLevel(node.left), getMaxLevel(node.right)) + 1;
}
}