-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryMaxHeap.java
126 lines (109 loc) · 3.26 KB
/
BinaryMaxHeap.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
import java.lang.reflect.Array;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
public class BinaryMaxHeap<T extends Comparable<T>> implements MyPriorityQueue<T> {
private int size;
private T[] arr;
private Map<T, Integer> itemToIndex;
public BinaryMaxHeap() {
arr = (T[]) Array.newInstance(Comparable.class, 10);
size = 0;
itemToIndex = new HashMap<>();
}
private void percolateUp(int i) {
while (i > 0 && arr[i].compareTo(arr[(i - 1) / 2]) > 0) {
swap(i, (i - 1) / 2);
i = (i - 1) / 2;
}
}
private void percolateDown(int i) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int largest = i;
if (left < size && arr[left].compareTo(arr[largest]) > 0) {
largest = left;
}
if (right < size && arr[right].compareTo(arr[largest]) > 0) {
largest = right;
}
if (largest != i) {
swap(i, largest);
percolateDown(largest);
}
}
private void resize() {
T[] larger = (T[]) Array.newInstance(Comparable.class, arr.length * 2);
System.arraycopy(arr, 0, larger, 0, arr.length);
arr = larger;
}
private void swap(int i, int j) {
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
itemToIndex.put(arr[i], i);
itemToIndex.put(arr[j], j);
}
public void insert(T item) {
if (size == arr.length) {
resize();
}
arr[size] = item;
itemToIndex.put(item, size);
percolateUp(size);
size++;
}
public T extract() {
if (size == 0) throw new IllegalStateException("Heap is empty");
T max = arr[0];
arr[0] = arr[size - 1];
itemToIndex.put(arr[0], 0);
size--;
percolateDown(0);
itemToIndex.remove(max);
return max;
}
public void remove(T item) {
if (!itemToIndex.containsKey(item)) {
throw new IllegalArgumentException("Item not in heap");
}
remove(itemToIndex.get(item));
}
private T remove(int index) {
T removedItem = arr[index];
arr[index] = arr[size - 1];
itemToIndex.put(arr[index], index);
size--;
percolateDown(index);
itemToIndex.remove(removedItem);
return removedItem;
}
public void updatePriority(T item) {
if (!itemToIndex.containsKey(item)) {
throw new IllegalArgumentException("Item not found in the heap");
}
updatePriority(itemToIndex.get(item));
}
private void updatePriority(int index) {
percolateUp(index);
percolateDown(index);
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public T peek() {
if (size == 0) throw new IllegalStateException("Heap is empty");
return arr[0];
}
public List<T> toList() {
List<T> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add(arr[i]);
}
return list;
}
}