-
Notifications
You must be signed in to change notification settings - Fork 0
/
su.cpp
87 lines (74 loc) · 1.4 KB
/
su.cpp
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
#include<iostream>
using namespace std;
int z=-32000;
class heap{
public:
int a[100];
int cmax;
heap(){cmax=0;}
int parent(int i){return (i-1)/2;}
int right(int i){return 2*i+2;}
int left(int i){return 2*i+1;}
void swap(int *c,int *b)
{int temp=*c;
*c=*b;
*b=temp;}
void insert(int val){
int i=cmax++;
a[i]=val;
while((a[(i-1)/2]>a[i])&&(i!=0))
{swap(&a[i],&a[(i-1)/2]);
i=(i-1)/2;
}
}
int getmin(){return a[0];}
void display(){
for(int i=0;i<cmax;i++)
{cout<<a[i]<<",";}
}
void dk(int i, int new_val)
{
a[i] = new_val;
while (i != 0 && a[parent(i)] > a[i])
{
swap(&a[i], &a[parent(i)]);
i = parent(i);
}
}
int extractmin(){
if(cmax==0){return a[0];}
int root=a[0];
a[0]=a[cmax-1];
cmax--;
heapify(0);
return root;
}
void heapify(int i){
int small=i;
if((left(i)<cmax)&&(a[left(i)]<a[i]))
{small=left(i);}
if((right(i)<cmax)&&(a[right(i)]<a[small]))
{small=right(i);}
if(small!=i){
swap(&a[i],&a[small]);
heapify(small);}
}
void deleteat(int i){
dk(i,z);
extractmin();
}
};
int main(){
heap h;
h.insert(56);
h.insert(13);
h.insert(1);
h.insert(8);
h.insert(6);
h.insert(18);
h.display();
cout<<"=";
h.deleteat(2);
h.display();
return 232;
}