-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMaxHeap.java
81 lines (73 loc) · 2.32 KB
/
MaxHeap.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
package main.java.datastructure;
import java.util.Arrays;
/**
* Reference : https://github.com/midNight-jam/DataStructures_Algorithms_Java/blob/master/src/ADT/MaxHeap.java
*/
public class MaxHeap {
//Max heap
private int[] heap;
private int heapCount;
public MaxHeap(int size) {
heap = new int[size + 1];
heapCount = 0;
}
public void insert(int data) {
if (heapCount == heap.length - 1) {
doubleSize();
}
//insert the new item to the last of heap (end of the array) and SIFT UP
int pos = ++heapCount;
heap[heapCount] = data;
// checking for greater than beacuse its a MAX Heap
while ((pos > 1) && data > heap[pos / 2]) {
heap[pos] = heap[pos / 2]; // bringing the parent down to make room for new data
pos = pos / 2; // moving to another parent
}
heap[pos] = data; // final postion for new data
}
public int fetchTop() {
int res = heap[1];
int ptr, left, right;
ptr = 1;
left = 2 * ptr;
right = left + 1;
heap[1] = heap[heapCount]; // get the last element on top
int last = heap[heapCount]; // get the last element on top
// percolate down
while (right <= heapCount) {
// take the max among two childs & then SiftDown the parent
// SiftDown left subtree
if (heap[left] <= heap[right]) {
heap[ptr] = heap[right];
ptr = right;
}
// SiftDown right subtree
if (heap[right] <= heap[left]) {
heap[ptr] = heap[left];
ptr = left;
}
left = 2 * ptr;
right = left + 1;
}
heap[ptr] = last;
heapCount--; // drop the last element
return res;
}
public boolean isEmpty() {
return heapCount == 0;
}
private void doubleSize() {
heap = Arrays.copyOf(heap, heap.length * 2);
}
public static void main(String[] args) {
MaxHeap maxHeap = new MaxHeap(6);
maxHeap.insert(12);
maxHeap.insert(7);
maxHeap.insert(6);
maxHeap.insert(10);
maxHeap.insert(8);
maxHeap.insert(20);
// maxHeap.insert(23);
System.out.println(maxHeap.fetchTop());
}
}