-
Notifications
You must be signed in to change notification settings - Fork 28
/
HeapTree.java
264 lines (211 loc) · 4.67 KB
/
HeapTree.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package ds_005_sorting;
public class HeapTree<T extends Comparable<T>> {
private Node<T> root;
private int size;
public HeapTree() {
this.root = null;
}
public int[] heapSort(int[] values) {
add(values);
int[] sorted = new int[values.length];
for(int i = sorted.length-1; i >= 0; i--) {
sorted[i] = (int) remove();
}
return sorted;
}
public void add(int[] values) {
for(int i = 0; i < values.length; i++) {
Node<T> node = add(values[i]);
}
}
private void handleParent(Node<T> node) {
if(node.parent == null) {
return;
} else if(node.parent.value.compareTo(node.value) > 0) {
return;
} else {
T temp = node.value;
node.value = node.parent.value;
node.parent.value = temp;
if(node.parent != null) {
handleParent(node.parent);
}
}
}
// addBottom
public Node<T> add(int value) {
Node<T> newNode = new Node(value);
if(root == null) {
newNode.parent = null;
root = newNode;
} else {
SQueue<Node<T>> queue = new SQueue<>();
Node<T> parent = null;
// init
queue.enqueue(root);
while(!queue.isEmpty() && newNode.parent == null) {
int tempSize = queue.size();
for(int i = 0; i < tempSize; i++) {
Node<T> temp = queue.dequeue();
if(temp.left == null) {
newNode.parent = temp;
temp.left = newNode;
break;
} else if(temp.right == null) {
newNode.parent = temp;
temp.right = newNode;
break;
} else {
queue.enqueue(temp.left);
queue.enqueue(temp.right);
}
}
}
}
size++;
handleParent(newNode);
return newNode;
}
private T getLast() {
if(size <= 0) {
return null;
}
boolean isLeftChild = false;
Node<T> last = root;
Node<T> parent = null;
while(true) {
if(last.right != null) {
parent = last;
last = last.right;
isLeftChild = false;
} else if(last.left != null){
parent = last;
last = last.left;
isLeftChild = true;
} else {
break;
}
}
T value = last.value;
if(parent != null) {
if(isLeftChild) {
parent.left = null;
} else {
parent.right = null;
}
} else {
// only root exist
}
return value;
}
public T remove() {
if(size == 0 || root == null) {
return null;
}
T rootValue = root.value;
T lastValue = getLast();
root.value = lastValue;
handleDown(root);
if(size == 1 && root.value == lastValue) {
root = null;
}
size--;
return rootValue;
}
private void handleDown(Node<T> node) {
if(node == null) {
return;
}
if(node.left != null && node.right != null) {
if(node.left.value.compareTo(node.right.value) > 0) {
if(node.left.value.compareTo(node.value) > 0) {
swap(node, node.left);
handleDown(node.left);
return;
}
} else {
if(node.right.value.compareTo(node.value) > 0) {
swap(node, node.right);
handleDown(node.right);
return;
}
}
} else if(node.left != null) {
if(node.left.value.compareTo(node.value) > 0) {
swap(node, node.left);
handleDown(node.left);
return;
}
} else if(node.right != null) {
if(node.right.value.compareTo(node.value) > 0) {
swap(node, node.right);
handleDown(node.right);
return;
}
}
}
private void swap(Node<T> node1, Node<T> node2) {
T temp = node1.value;
node1.value = node2.value;
node2.value = temp;
}
public void print() {
System.out.println("Heap;");
System.out.println("*****");
if(root == null) {
System.out.println("Empty\n");
} else {
printLevels(root);
}
}
public void printLevels(Node<T> node) {
SQueue<Node<T>> queue = new SQueue<>();
queue.enqueue(node);
while(!queue.isEmpty()) {
int tempSize = queue.size();
System.out.print("\t");
for(int i = 0; i < tempSize; i++) {
Node<T> temp = queue.dequeue();
System.out.printf("%s ", temp);
if(temp.left != null) {
queue.enqueue(temp.left);
}
if(temp.right != null) {
queue.enqueue(temp.right);
}
}
System.out.println();
}
}
private void printLMR(Node<T> node) {
if(node == null) {
return;
}
if(node.left != null) {
printLMR(node.left);
}
System.out.printf("%d ", node.value);
if(node.right != null) {
printLMR(node.right);
}
}
public boolean isEmpty() {
return size == 0 ? true : false;
}
private static class Node<G extends Comparable<G>> {
private G value;
private Node<G> parent;
private Node<G> left;
private Node<G> right;
public Node(G value) {
this.parent = null;
this.value = value;
this.left = null;
this.right = null;
}
@Override
public String toString() {
return "" + value;
}
}
}