-
Notifications
You must be signed in to change notification settings - Fork 0
/
PQHeap.java
104 lines (91 loc) · 1.96 KB
/
PQHeap.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
public class PQHeap implements PriorityQueue{
private Integer[] data;
private int numElts;
public void add(Integer toAdd){
if((numElts+1) == data.length) resize();
data[numElts+1]= toAdd;
numElts++;
siftUp(numElts);
}
public Integer remove(){
if(numElts == 0) return null;
Integer toReturn = data[1];
data[1]=data[numElts];
numElts--;
siftDown();
return toReturn;
}
public void siftDown(){
int i = 1;
int big;
int holder;
// while there is at least one child
while(numElts>=(2*i)){
//checks to see if there are two children
if(numElts>=(2*i+1)){
// checks to see if the parent is larger than children
if(data[i]>=data[2*i] && data[i]>=data[2*i+1]){
break;
}
//finds the larger child and marks its pos
if(data[2*i]>data[2*i+1]){big = 2*i;}
else{big = 2*i+1;}
// does the switcheroo
holder = data[i];
data[i] = data[big];
data[big] = holder;
//changes the index to the sifted down number
i = big;
}
// the corner case of one child
else{
if(data[i]<data[i*2]){
holder = data[i];
data[i] = data[i*2];
data[i*2] = holder;
break;
}
else{break;}
}
}
}
public void resize(){
Integer[] temp = new Integer[data.length * 2];
for(int i = 1; i <= numElts; i++) {
temp[i] = data[i];
}
data = temp;
}
public void siftUp(int pos){
int temp;
// if the element is not at the top
if(pos>1) {
// if the element is greater than the one above it
if(data[pos]>data[pos/2]) {
temp = data[pos/2];
data[pos/2] = data[pos];
data[pos] = temp;
// call again on the new position
siftUp(pos/2);
}
}
}
public int size(){
return numElts;
}
public boolean isEmpty(){
if(numElts == 0){return true;}
else{return false;}
}
public PQHeap(){
data = new Integer[2];
numElts = 0;
}
public String toString() {
String toReturn = "";
for(int i = 1; i<numElts;i++) {
toReturn += data[i] +" ";
}
return toReturn;
}
}