-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap.java
103 lines (88 loc) · 2.3 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
import java.util.Arrays;
// Heap using arrays
public class Heap{
private Integer[] heapData;
private int currentPosition = -1;
public Heap(int size) {
this.heapData = new Integer[size];
}
//Time Complexity O(log n)
public void insert(int item) {
if (isFull())
throw new RuntimeException("Heap is full");
this.heapData[++currentPosition] = item;
fixUp(currentPosition);
}
//Time Complexity O(log n)
public int deleteRoot() {
int result = heapData[0];
heapData[0] = heapData[currentPosition--];
heapData[currentPosition+1] = null;
fixDown(0, -1);
return result;
}
private void fixDown(int index, int upto) {
if (upto < 0) upto = currentPosition;
while (index <= upto) {
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;
if (leftChild <= upto) {
int childToSwap;
if (rightChild > upto)
childToSwap = leftChild;
else
childToSwap = (heapData[leftChild] > heapData[rightChild]) ? leftChild : rightChild;
if (heapData[index] < heapData[childToSwap]) {
int tmp = heapData[index];
heapData[index] = heapData[childToSwap];
heapData[childToSwap] = tmp;
} else
break;
index = childToSwap;
} else
break;
}
}
private void fixUp(int index) {
int i = (index-1)/2;
while (i >= 0 && heapData[i] < heapData[index]) {
int tmp = heapData[i];
heapData[i] = heapData[index];
heapData[index] = tmp;
index = i;
i = (index-1)/2;
}
}
private boolean isFull() {
return currentPosition == heapData.length-1;
}
//Time Complexity O(n log n)
public void heapSort() {
for (int i=0; i < currentPosition; i++) {
int tmp = heapData[0]; // max element
heapData[0] = heapData[currentPosition-i]; // bring last element to the root
heapData[currentPosition-i] = tmp; // put max at the last of unsorted part
fixDown(0, currentPosition-i-1);
}
}
@Override
public String toString() {
return Arrays.deepToString(this.heapData);
}
public static void main(String[] args) {
Heap heap = new Heap(10);
heap.insert(30);
heap.insert(55);
heap.insert(57);
heap.insert(78);
heap.insert(18);
heap.insert(3);
heap.insert(58);
heap.insert(24);
heap.heapSort();
System.out.println(heap);
System.out.println(heap.deleteRoot());
heap.heapSort();
System.out.println(heap);
}
}