-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBinaryHeap.java
More file actions
148 lines (123 loc) · 3.61 KB
/
BinaryHeap.java
File metadata and controls
148 lines (123 loc) · 3.61 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
package org.psjava.ds.heap;
import java.util.Comparator;
import org.psjava.ds.array.ArraySwapper;
import org.psjava.ds.array.DynamicArray;
import org.psjava.util.Assertion;
// TODO contains bug.
// TODO left should be (i << 1) + 1, right should be (i << 1) + 2, parent should be (i-1) >> 1
@Deprecated
public class BinaryHeap<T> implements Heap<T> {
private final DynamicArray<Node> array = DynamicArray.create();
private final Comparator<T> comparator;
public BinaryHeap(Iterable<T> initialItems, Comparator<T> comparator) {
this.comparator = comparator;
for (T v : initialItems)
array.addToLast(new Node(array.size(), v));
for (int i = array.size() / 2 - 1; i >= 0; i--)
heapify(i);
}
@Override
public HeapNode<T> insert(T v) {
Node node = new Node(array.size(), v);
array.addToLast(node);
decreaseKey(node.pos);
return node;
}
@Override
public T getMinimum() {
Assertion.ensure(!isEmpty(), "empty");
return array.get(0).key;
}
@Override
public T extractMinimum() {
Assertion.ensure(!isEmpty(), "heap is empty");
swapNode(0, array.size() - 1);
Node r = array.removeLast();
heapify(0);
r.pos = -1;
return r.key;
}
@Override
public boolean isEmpty() {
return array.isEmpty();
}
private void swapNode(int i, int j) {
ArraySwapper.swap(array, i, j);
array.get(i).pos = i;
array.get(j).pos = j;
}
private void heapify(int i) {
int left, right, smallest;
while (true) {
left = i << 1;
right = (i << 1) + 1;
smallest = i;
if (left < array.size() && compare(array.get(left), array.get(smallest)) < 0)
smallest = left;
if (right < array.size() && compare(array.get(right), array.get(smallest)) < 0)
smallest = right;
if (smallest == i)
break;
swapNode(smallest, i);
i = smallest;
}
}
private void decreaseKey(int pos) {
while (pos > 0 && compare(array.get(pos), array.get(pos >> 1)) < 0) {
swapNode(pos >> 1, pos);
pos >>= 1;
}
}
private void delete(int pos) {
forceToRoot(pos);
extractMinimum();
}
private void forceToRoot(int pos) {
while (pos > 0) {
swapNode(pos >> 1, pos);
pos >>= 1;
}
}
private int compare(Node v1, Node v2) {
return comparator.compare(v1.getKey(), v2.getKey());
}
private class Node implements HeapNode<T> {
int pos; // -1 if not in heap
T key;
Node(int pos, T key) {
this.pos = pos;
this.key = key;
}
@Override
public T getKey() {
assertNotDeleted();
return key;
}
@Override
public boolean isInHeap() {
return pos != -1;
}
@Override
public void decreaseKey(T key) {
assertNotDeleted();
this.key = key;
BinaryHeap.this.decreaseKey(pos);
}
@Override
public void delete() {
assertNotDeleted();
BinaryHeap.this.delete(pos);
}
private void assertNotDeleted() {
Assertion.ensure(pos != -1, "Node is not in heap (deleted)");
}
@Override
public String toString() {
return key.toString();
}
}
@Override
public String toString() {
return array.toString();
}
}