-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxheap.java
79 lines (75 loc) · 2 KB
/
maxheap.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
public class maxheap {
qnode [] a=new qnode[2];
int last=0;
public int sizeh(){
return last;
}
public boolean isemptyh(){
return sizeh()>0;
}
public qnode findmax() throws EmptyheapException{
if(isemptyh()) throw new EmptyheapException("empty heap");
return a[1];
}
public qnode deletemax() throws EmptyheapException{
if(isemptyh()) throw new EmptyheapException("empty heap");
else{
qnode x=a[1];
a[1]=a[last];
last--;
percdown(1,a[1]);
return x;}
}
public void percdown(int i,qnode x){
if(2*i>last){
a[i]=x;
}
else if(2*i==last){
if(a[2*i].size>x.size) {
a[i]=a[2*i];
a[2*i]=x;
}
}
else{
int j;
if(a[2*i].size>a[2*i+1].size)
j=2*i;
else j=2*i+1;
if(a[j].size>x.size){
a[i]=a[j];
percdown(j, x);
}
else a[i]=x;
}
}
public void inserth(qnode x){
if(isemptyh()) {a[1]=x; last++;}
else{
if(last!=a.length-1){
a[last++]=x;
}
else{
qnode [] b=new qnode[2*a.length];
for(int i=1;i<=last;i++){
b[i]=a[i];
}
a=b;
a[last++]=x;
}
percup(last, x);
}
}
public void percup(int i,qnode x){
if(i==1) {a[i]=x;}
else if(a[i].size>a[i/2].size){
a[i]=a[i/2];
percup(i/2,x);
}
else {a[i]=x;}
}
public void buildminheap(qnode a[]){
for(int i=(last/2);i>=1;i++){
percdown(i, a[i]);
}
}
}