-
Notifications
You must be signed in to change notification settings - Fork 1
/
MinHeap.java
63 lines (54 loc) · 1.81 KB
/
MinHeap.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
package datastructure.heap;
import java.util.Arrays;
public class MinHeap implements Heap {
private int[] items = null;
private int size;
private int capacity = 5;
public MinHeap() {
items = new int[capacity];
size = 0;
}
public void heapifyUp() {
int index = size - 1;
while (HeapUtility.hasParent(index) && HeapUtility.parent(index, items) > items[index]) {
HeapUtility.swap(HeapUtility.getParentIndex(index), index, items);
index = HeapUtility.getParentIndex(index);
}
}
public void heapifyDown() {
int index = 0;
while (HeapUtility.hasLeftChild(index, size)) {
int smallerChildIndex = HeapUtility.getLeftChildIndex(index);
if (items[index] < items[smallerChildIndex])
break;
if (HeapUtility.hasRightChild(index, size) && HeapUtility.rightChild(index, items) < HeapUtility.leftChild(index, items))
smallerChildIndex = HeapUtility.getRightChildIndex(index);
else
HeapUtility.swap(index, smallerChildIndex, items);
index = smallerChildIndex;
}
}
@Override
public void insert(int item) {
items = HeapUtility.ensureExtraCapacity(items, size, capacity);
if (size == capacity)
capacity *= 2;
items[size] = item;
size++;
heapifyUp();
}
@Override
public int pop() {
if (size == 0)
throw new IllegalStateException();
int item = items[0];
items[0] = items[size - 1];
size--;
heapifyDown();
return item;
}
@Override
public String toString() {
return "MinHeap [items=" + Arrays.toString(items) + "]";
}
}