-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTreeHeap.java
More file actions
188 lines (160 loc) · 5.03 KB
/
TreeHeap.java
File metadata and controls
188 lines (160 loc) · 5.03 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package Heap;
public class TreeHeap {
public static class Node<T extends Comparable<? super T>> {
public Node<T> left;
public Node<T> right;
public T data;
public int rank;
public Node(T data) {
this.data = data;
rank = 1;
}
public void insert(T data) {
if (rank == 1) {
if (left == null) {
left = new Node<>(data);
} else {
right = new Node<>(data);
this.rank++;
}
} else {
if (this.left.rank > this.right.rank) {
right.insert(data);
} else {
left.insert(data);
}
rank++;
}
}
public void insertSorted(T data) {
this.insert(data);
this.shiftUp();
}
public void shiftUp() {
if (left != null) {
left.shiftUp();
}
if (right != null) {
right.shiftUp();
}
if (!isLeaf()) {
Node<T> min;
if (right == null) {
min = left;
} else {
min = (left.data.compareTo(right.data) < 0) ? left : right;
}
if (min.data.compareTo(data) < 0) {
T tmp = data;
data = min.data;
min.data = tmp;
}
}
}
public boolean isLeaf() {
return right == null && left == null;
}
}
public static <T extends Comparable<? super T>> Node<T> merge(Node<T> x, Node<T> y) {
if (x == null) {
return y;
}
if (y == null) {
return x;
}
if (x.data.compareTo(y.data) > 0) {
Node temp = x;
x = y;
y = temp;
}
x.right = merge(x.right, y);
if (x.left == null) {
x.left = x.right;
x.right = null;
} else {
if (x.left.rank < x.right.rank) {
Node temp = x.left;
x.left = x.right;
x.right = temp;
}
x.rank = x.right.rank + 1;
}
return x;
}
public static <T extends Comparable<? super T>> T min(Node<T> root) {
if (root == null) {
return null;
}
T tmp = root.data;
return tmp;
}
public static <T extends Comparable<? super T>> Node<T> delete(Node<T> root) {
if (root == null) {
return null;
}
return merge(root.left, root.right);
}
public static <T extends Comparable<? super T>> Node<T> delete(Node<T> root, T data) {
if (root == null) {
return null;
}
Node left = delete(root.left, data);
Node right = delete(root.right, data);
if (data.compareTo(root.data) == 0) {
root = merge(left, right);
}
return root;
}
public static <T extends Comparable<? super T>> Node<T> insert(T... a) {
Node<T> root = new Node<>(a[0]);
for (int i = 1; i < a.length; i++) {
root = merge(root, new Node<>(a[i]));
}
return root;
}
public static void printNice(Node root, int level) {
if (root == null) {
return;
}
printNice(root.right, level + 1);
if (root.rank != 0) {
for (int i = 0; i < level - 1; i++) {
System.out.print("| ");
}
System.out.println("|---" + root.data);
} else {
System.out.println(root.data);
}
printNice(root.left, level + 1);
}
public static void main(String[] args) {
Node<Integer> heap1, heap2, heap3, heap4, heap5;
heap1 = insert(3, 1, 9, 10);
System.out.println("------------ heap1 ------------");
printNice(heap1, 1);
heap2 = insert(4, 8, 5, 6, 11);
System.out.println("------------ heap2 ------------");
printNice(heap2, 1);
heap3 = merge(heap1, heap2);
System.out.println("------------ heap3 = heap1 + heap2 ------------");
printNice(heap3, 1);
System.out.println("------------- Min(heap3) ------------");
System.out.println(min(heap3));
heap3 = delete(heap3);
System.out.println("------------ deleter(heap3) ------------");
printNice(heap3, 1);
heap4 = insert(3, 1, 9, 10, 1);
System.out.println("------------ heap4 ------------");
printNice(heap4, 1);
heap4 = delete(heap4, 1);
System.out.println("------------ delete(heap4, 1) ------------");
printNice(heap4, 1);
System.out.println("------------ heap5 ------------");
heap5 = insert(5);
heap5.insertSorted(7);
heap5.insertSorted(10);
heap5.insertSorted(1);
heap5.insertSorted(2);
printNice(heap5, 1);
}
}