-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHeap.java
130 lines (107 loc) · 3.39 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package data_structures;
// TODO(Jonathon): Should support min/max versions.
public class Heap {
private int[] heap;
private int size;
private int maxSize;
public Heap(int maxSize) {
this.maxSize = maxSize;
this.size = 0;
this.heap = new int[this.maxSize];
}
private int parent(int pos) {
return (pos -1) / 2;
}
private int kthChild(int i, int k){
return 2*i+k;
}
private void swap(int first, int second) {
int tmp;
tmp = this.heap[first];
this.heap[first] = this.heap[second];
this.heap[second] = tmp;
}
/**
* This will insert new element in to heap
* Complexity: O(log N)
* In the worst case scenario, it needs to traverse until the root.
*/
public void insert(int item) {
if (this.size == this.maxSize) {
throw new RuntimeException("Cannot insert. Heap is at max size.");
}
// Initially insert at end.
this.heap[this.size] = item;
for(int curr = this.size; this.heap[curr] > this.heap[parent(curr)]; curr = parent(curr)) {
swap(curr, parent(curr));
}
this.size++;
return;
}
private boolean isLeaf(int pos) {
if (pos > (size / 2) && pos <= size) {
return true;
}
return false;
}
public int pop() {
if (this.isEmpty()) {
throw new RuntimeException("Cannot pop from empty heap.");
}
int popped = this.heap[0];
this.heap[0] = this.heap[--this.size]; // Move last element to root.
// for (int curr = 0; this.heap[curr] < this.heap[kthChild(curr, 1)])
int curr = 0;
while (!isLeaf(curr)) {
if (
this.heap[curr] < this.heap[kthChild(curr, 1)]
|| this.heap[curr] < this.heap[kthChild(curr, 2)]
) {
if (this.heap[kthChild(curr, 1)] > this.heap[kthChild(curr, 2)]) {
swap(curr, kthChild(curr, 1));
curr = kthChild(curr, 1);
} else {
swap(curr, kthChild(curr, 2));
curr = kthChild(curr, 2);
}
} else {
break;
}
}
return popped;
}
public boolean isEmpty() {
return this.size == 0;
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++) {
sb.append(this.heap[i]);
sb.append(", ");
}
return sb.toString();
}
public static void main(String[] arg) {
// Display message for better readability
System.out.println("The Max Heap is ");
Heap maxHeap = new Heap(15);
maxHeap.insert(5);
maxHeap.insert(3);
maxHeap.insert(17);
maxHeap.insert(10);
maxHeap.insert(84);
maxHeap.insert(19);
maxHeap.insert(6);
maxHeap.insert(22);
maxHeap.insert(9);
System.out.println(maxHeap.toString());
StringBuilder sb = new StringBuilder();
while (!maxHeap.isEmpty()) {
sb.append(maxHeap.pop());
sb.append(", ");
}
System.out.println(sb.toString());
// Print and display the maximum value in heap
// System.out.println("The max val is " + maxHeap.extractMax());
}
}