-
Notifications
You must be signed in to change notification settings - Fork 22
/
Heap.java
112 lines (88 loc) · 2.5 KB
/
Heap.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
package com.company.amazon;
import com.geeksforgeeks.array.QuickSort;
import java.util.Arrays;
public class Heap {
int capacity = 10;
int[] items = new int[capacity];
int size = 0;
private int leftChildIndex(int parentIndex) {
return parentIndex * 2 + 1;
}
private int rightChildIndex(int parentIndex) {
return parentIndex * 2 + 2;
}
private int parentIndex(int childIndex) {
return (childIndex - 1) / 2;
}
private int parent(int childIndex) {
return items[parentIndex(childIndex)];
}
private int leftChild(int parentIndex) {
return items[leftChildIndex(parentIndex)];
}
private int rightChild(int parentIndex) {
return items[rightChildIndex(parentIndex)];
}
private boolean hasParent(int childIndex) {
return parentIndex(childIndex) >= 0;
}
private boolean hasLeftChild(int parentIndex) {
return leftChildIndex(parentIndex) < size;
}
private boolean hasRightChild(int parentIndex) {
return rightChildIndex(parentIndex) < size;
}
private void ensureExtraCapacity() {
if (size >= capacity) {
items = Arrays.copyOf(items, capacity * 2);
capacity *= 2;
}
}
public void insert(int item) {
ensureExtraCapacity();
items[size++] = item;
heapifyUp();
}
public int extractMin() {
int min = items[0];
items[0] = items[size - 1];
size--;
heapifyDown();
return min;
}
public void heapifyDown() {
int index = 0;
while (hasLeftChild(index)) {
int minChildIndex = leftChildIndex(index);
if (hasRightChild(index) && rightChild(index) < leftChild(index)) {
minChildIndex = rightChildIndex(index);
}
if (items[minChildIndex] < items[index]) {
QuickSort.swap(items, index, minChildIndex);
index = minChildIndex;
} else {
break;
}
}
}
public void heapifyUp() {
int index = size - 1; // Currently newly added item is at this index
while (hasParent(index) && items[index] < parent(index)) {
QuickSort.swap(items, index, parentIndex(index));
index = parentIndex(index);
}
}
public static void main(String[] args) {
Heap minHeap = new Heap();
minHeap.insert(10);
minHeap.insert(20);
minHeap.insert(30);
minHeap.insert(100);
minHeap.insert(5);
System.out.println(minHeap.extractMin());
System.out.println(minHeap.extractMin());
System.out.println(minHeap.extractMin());
System.out.println(minHeap.extractMin());
System.out.println(minHeap.extractMin());
}
}