-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathIndexHeapReverse.java
More file actions
199 lines (155 loc) · 4.64 KB
/
IndexHeapReverse.java
File metadata and controls
199 lines (155 loc) · 4.64 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
189
190
191
192
193
194
195
196
197
198
199
public class IndexHeapReverse<Key extends Comparable<Key>> {
// 最大索引堆
/*
* indexes[i] = j
* reverse[j] = i
*
* indexes[reverse[i]] = i
* reverse[indexes[i]] = i
*
* */
private Key[] data;
private int capacity;
int[] indexes;
int[] reverse;
private int count;
public int size() {
return count;
}
public boolean isEmpty() {
return count == 0;
}
public IndexHeapReverse(int capacity) {
data = (Key[]) new Comparable[capacity + 1];
indexes = new int[capacity + 1];
reverse = new int[capacity + 1];
for (int i = 0; i <= capacity; i++) {
// 从 1 开始,因此 0 为非法索引
reverse[i] = 0;
}
count = 0;
this.capacity = capacity;
}
private boolean less(int i, int j) {
return data[i].compareTo(data[j]) < 0;
}
private void exch(int i, int j) {
Key tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
private void exchIndex(int i, int j) {
int tmp = indexes[i];
indexes[i] = indexes[j];
indexes[j] = tmp;
/*// 此时 indexes i和j 已经发生了交换,所以只需要如下编写即可
reverse[indexes[i]] = i;
reverse[indexes[j]] = j;*/
}
public void swin(int k) {
// 上浮
while (k > 1 && less(indexes[k / 2], indexes[k])) {
// exch(k / 2, k);
exchIndex(k / 2, k);
// reverse 维护
// 此时 indexes k和k/2 已经发生了 改变,所以只需要如下编写即可
reverse[indexes[k / 2]] = k / 2;
reverse[indexes[k]] = k;
k = k / 2;
}
}
public void sink(int k) {
// 下沉
while (k * 2 <= count) {
int j = k * 2;
if (j < count && less(indexes[j], indexes[j + 1])) j++;
if (!less(indexes[k], indexes[j])) break;
// exch(indexes[k], indexes[j]);
exchIndex(k, j);
// reverse 维护
reverse[indexes[k]] = k;
reverse[indexes[j]] = j;
k = j;
}
}
// 传入的i对用户而言,是从0索引的
public void insert(int i, Key key) {
assert (count + 1 <= capacity);
assert (i + 1 >= 1 && i + 1 <= capacity);
i += 1;
data[i] = key;
indexes[count + 1] = i;
// reverse 维护
reverse[i] = count + 1;
count++;
swin(count);
}
public Key delMax() {
assert (count > 0);
Key key = data[indexes[1]];
// exch(indexes[1], indexes[count]);
exchIndex(1, count);
// reverse 维护
reverse[indexes[1]] = 1;
reverse[indexes[count]] = 0; // 因为是删除操作
count--;
sink(1);
return key;
}
public int delMaxIndex() {
assert (count > 0);
int ret = indexes[1] - 1;
// exch(indexes[1], indexes[count]);
exchIndex(1, count);
// reverse 维护
reverse[indexes[1]] = 1;
reverse[indexes[count]] = 0; // 因为是删除操作
count--;
sink(1);
return ret;
}
// 从 1 开始,因此 0 为非法索引
private boolean contain(int i) {
if (!(i + 1 >= 1 && i + 1 <= capacity))
throw new RuntimeException("Error");
return reverse[i + 1] != 0;
}
public Key getKey(int i) {
// assert( contain(i) )
if (!contain(i))
throw new RuntimeException("Error");
return data[i + 1];
}
public void change(int i, Key newKey) {
// assert( contain(i) )
if (!contain(i))
throw new RuntimeException("Error");
i += 1;
data[i] = newKey;
// 找到 indexes[j] = i, j表示data[i]在堆中的位置
// 找到之后 swin(j),再 sink(j)
// for (int j = 1; j <= count; j++) {
// if (indexes[j] == i) {
// swin(j); // 上移
// sink(j); // 下沉
// return;
// }
// }
int j = reverse[i];
swin(j);
sink(j);
}
private void sort(Comparable a[]) {
}
public static void main(String[] args) {
int[] nums = {3, 6, 2, 4, 5, 1};
IndexHeapReverse<Integer> maxHeap = new IndexHeapReverse<>(nums.length);
for (int i = 0; i < nums.length; i++) {
maxHeap.insert(i, nums[i]);
}
for (int i = 0; i < nums.length; i++) {
System.out.println(maxHeap.delMax());
// System.out.println(maxHeap.delMaxIndex());
}
}
}