-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjustHeap.java
More file actions
156 lines (134 loc) · 3.58 KB
/
AdjustHeap.java
File metadata and controls
156 lines (134 loc) · 3.58 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
import java.util.HashMap;
import java.util.Map;
public class AdjustHeap<T extends Comparable<? super T>> {
private Map<T, Integer> indexMap;
private Object[] array;
private int size;
public AdjustHeap(HashMap<T, Integer> indexMap) {
this(16, indexMap);
}
public AdjustHeap(int size, HashMap<T, Integer> indexMap) {
array = new Object[size + 1];
this.size = 0;
this.indexMap = indexMap;
}
public void offer(T t) {
if (size + 1 == array.length)// array full
expand();
int hole = ++size;
array[hole] = t;
percUp(hole);
}
private void percUp(int hole) {
array[0] = array[hole];
for (; compare(0, hole / 2) < 0; hole /= 2) {
array[hole] = array[hole / 2];
indexMap.put((T) array[hole], hole);
}
array[hole] = array[0];
indexMap.put((T) array[hole], hole);
}
private int compare(int o1, int o2) {
return ((T) array[o1]).compareTo((T) array[o2]);
}
private void expand() {
Object[] newArray = new Object[array.length * 2];
System.arraycopy(array, 1, newArray, 1, size);
array = newArray;
}
public T poll() {
if (size == 0)
return null;
T toReturn = (T) array[1];
array[1] = array[size];
size--;
percDown(1);
indexMap.remove(toReturn);
return toReturn;
}
private void percDown(int hole) {
array[0] = array[hole];
int child = hole;
for (; child * 2 <= size; hole = child) {
child = hole * 2;
if (child + 1 <= size && compare(child, child + 1) > 0)
child++;
if (compare(0, child) > 0) {
array[hole] = array[child];
indexMap.put((T) array[hole], hole);
} else {
break;
}
}
array[hole] = array[0];
indexMap.put((T) array[hole], hole);
}
public void adjust(T t) {
int index = indexMap.get(t);
if (index > 1 && compare(index, index / 2) < 0) {
percUp(index);
} else {
int child = index * 2;
if (child <= size) {
if (child + 1 <= size && compare(child, child + 1) > 0)
child++;
if (compare(index, child) > 0) {
percDown(index);
}
}
}
}
public boolean isEmpty() {
return size == 0;
}
public boolean contains(T t) {
return indexMap.containsKey(t);
}
//Test codes
public static void main(String[] args) {
AdjustHeap<Mutable> heap = new AdjustHeap<Mutable>(new HashMap<Mutable, Integer>());
heap.offer(new Mutable(0, 0));
System.out.println(heap.poll());
heap.offer(new Mutable(3, 1));
heap.offer(new Mutable(5, 2));
heap.offer(new Mutable(4, 3));
heap.offer(new Mutable(1, 4));
heap.offer(new Mutable(2, 5));
System.out.println(heap.poll());
System.out.println(heap.poll());
System.out.println(heap.poll());
Mutable m = new Mutable(6, 6);
heap.offer(m);
System.out.println(heap.poll());
heap.offer(new Mutable(1, 7));
heap.offer(new Mutable(2, 8));
System.out.println(heap.poll());
m.m = 0;
heap.adjust(m);
System.out.println(heap.poll());
System.out.println(heap.poll());
System.out.println(heap.poll());
System.out.println(heap.isEmpty());
}
static class Mutable implements Comparable<Mutable> {
int m;
int id;
public Mutable(int i, int id) {
m = i;
this.id = id;
}
@Override
public int compareTo(Mutable arg0) {
return m - arg0.m;
}
public boolean equals(Object o) {
return o instanceof Mutable && ((Mutable) o).id == id;
}
public int hashCode() {
return id;
}
public String toString() {
return String.valueOf(m);
}
}
}