-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinaryHeap.java
103 lines (91 loc) · 2 KB
/
BinaryHeap.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
package com.catherine.pq;
/**
* @author : Catherine
*/
public class BinaryHeap<T extends Comparable<? super T>> {
private int n; // size
private T[] pq;
public BinaryHeap(int capacity) {
pq = (T[]) new Object[capacity + 1]; // because root starts from pq[1]
}
public boolean isEmpty() {
return n == 0;
}
public T get(int i) {
return pq[i + 1];
}
/**
* Add a node at the end, then swim it up.
*
* @param node
*/
public void insert(T node) {
pq[n++] = node;
swim(n);
}
/**
* Exchange the root with the node at end, then sink it down.
*
* @return the deleted node
*/
public T delMax() {
T max = pq[1];
exch(1, n--);
sink(1);
pq[n + 1] = null; // prevent loitering
return max;
}
/**
* Check if the node is larger than its parent. If so, exchange the two node.
* Repeat the exchange up to the root
*
* @param k
*/
private void swim(int k) {
while (k > 1 && less(k / 2, k)) {
exch(k / 2, k);
k = k / 2;
}
}
/**
* Check if pq[k]'s one (or both) children are larger than pq[k]. If so, exchange pq[k] and its larger child.
* Repeat until the heap order resorted.
*
* @param k
*/
private void sink(int k) {
int j;
while (2 * k <= n) {
j = 2 * k;
if (j < n && less(j, j + 1)) {
j++;
}
if (less(j, k)) {
break;
}
exch(k, j);
k = j;
}
}
/**
* Array helper function
*
* @param i
* @param j
* @return
*/
private boolean less(int i, int j) {
return pq[i].compareTo(pq[j]) < 0;
}
/**
* Array helper function
*
* @param i
* @param j
*/
private void exch(int i, int j) {
T t = pq[i];
pq[i] = pq[j];
pq[j] = t;
}
}