-
Notifications
You must be signed in to change notification settings - Fork 1
/
prioirty_heap.cpp
134 lines (99 loc) · 2.17 KB
/
prioirty_heap.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
using namespace std;
const int MAX=100;
struct data
{
int val,p; /* val stores the value and p stores the respective priority*/
}d[MAX];
int heapsize=0,length=0;
data temp;
int parent(int i)
{return (i/2);}
int left(int i)
{return 2*i;}
int right(int i)
{return (2*i+1);}
int minheapify(data a[],int i) /* min heapify(min value at first) using the key of the data */
{
int l=left(i);
int smallest;
int r=right(i);
if (l<=heapsize && a[l].p<a[i].p)
smallest=l;
else smallest=i;
if (r<=heapsize && a[r].p<a[smallest].p)
smallest=r;
if (!(smallest==i))
{temp=a[i];
a[i]=a[smallest];
a[smallest]=temp;
minheapify(a,smallest); }
}
int heap_min(data a[])
{
return a[1].p;
}
int buildminheap(data a[],int length)
{
heapsize=length;
for(int l=(length/2);l>1;l--)
minheapify(a,l);
}
int heapincp(data a[],int i,int ps)
{
if (ps>a[i].p)
cout<<"ERROR";
a[i].p=ps;
while((i>1) && (a[parent(i)].p>a[i].p))
{
temp=a[i];
a[i]=a[parent(i)];
a[parent(i)]=temp;
i=parent(i);
}
}
int heap_extract_min(data a[])
{
if (heapsize<1)
cout<<"ERROR UNDERFLOW";
int minimum = a[1].val;
a[1]= a[heapsize];
heapsize--;
minheapify(a,1);
return minimum;
}
int min_heap_insert(data a[],int p)
{
heapsize+=1;
a[heapsize].p = 100000000;
heapincp(a,heapsize,p);
}
int main()
{
char op;
do
{
int ch;
cout<<"----------Menu-------------\n\n";
cout<<"\t1.Insertion\n\t2.Extract";
cout<<"\n\n\tEnter your Choice<1,2> ?";
cin>>ch;
switch(ch)
{
case 1 : length++;
cout<<"Enter Value ?";
cin>>d[length].val;
cout<<"Enter Priority (KEY) ?";
cin>>d[length].p;
min_heap_insert(d,d[length].p);
break;
case 2 : cout<<"\nPRIORITY QUEUE\n";
cout<< heap_extract_min(d)<<"\t";
length--;
break;
}
cout<<"\n\nDo You Want to Continue <Y/N> ?";
cin>>op;
}while(op=='Y' || op=='y');
return 0;
}