-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinHeapImpl.java
119 lines (102 loc) · 1.93 KB
/
MinHeapImpl.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
import java.util.NoSuchElementException;
public class MinHeapImpl<Key extends Comparable<Key>> implements MinHeap<Key> {
private Key[] a; //an array that contains the Heap elements
private int N; //the size of the Heap
public MinHeapImpl(){
N = 0;
a = (Key[]) new Comparable[10];
}
public MinHeapImpl(Key[] keys){
N = keys.length;
a = (Key[]) new Comparable[2*N];
for(int i=0; i<N; i++){
a[i+1] = keys[i];
}
heapify();
}
@Override
public void insert(Key v) {
//check the size
if(N == a.length-1){
resize(2*a.length);
}
a[++N] = v;
swim(N);
}
@Override
public Key delMin() {
if(N == 0){
throw new NoSuchElementException("The Heap is empty");
}
Key min = a[1];
exchKeys(1, N--);
sink(1);
a[N+1] = null;
//check the size
if(N <= a.length/4){
resize(a.length/2);
}
return min;
}
@Override
public boolean isEmpty() {
if(N == 0) return true;
return false;
}
@Override
public Key min() {
if(N == 0){
throw new NoSuchElementException("The Heap is empty");
}
return a[1];
}
@Override
public int size() {
return N;
}
private void heapify(){
for(int k=N/2; k>=1; k--){
sink(k);
}
}
private void resize(int newSize){
Key[] aCopy = (Key[]) new Comparable[newSize];
for(int i=1; i<=N; i++){
aCopy[i] = a[i];
}
a = aCopy;
aCopy = null;
}
private void sink(int k){
int j;
while(k<=N/2){
j = 2*k;
if((j+1<=N) && isLess(j+1, j)) j++;
if(isLess(k, j)) break;
exchKeys(k, j);
k = j;
}
}
private void swim(int k){
while((k>1) && isLess(k, k/2)){
exchKeys(k, k/2);
k = k/2;
}
}
/**
*
* @param k
* @param j
* @return true if the key at the indice k is less than the key at indice j
*/
private boolean isLess(int k, int j){
int compareValue = a[k].compareTo(a[j]);
if(compareValue < 0) return true;
return false;
}
private void exchKeys(int i, int j){
Key k = a[i];
a[i] = a[j];
a[j] = k;
}
}