-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpriorityqueue.cpp
More file actions
170 lines (138 loc) · 2.54 KB
/
priorityqueue.cpp
File metadata and controls
170 lines (138 loc) · 2.54 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Name :sharad Dixit
* Institute :IIIT -A
*/
#include <cstdlib>
#include <iostream>
using namespace std;
int length;
void heapify ( int arr[], int bound )
{
int root;
int lchild;
int mchild;
int rchild;
int i;
int temp;
for ( i = bound/2;i >=1;i--){
// root=a[i];
lchild=2*i;
rchild=2*i+1;
if ( lchild <= bound && rchild<=bound) {
if(arr[rchild] >= arr[lchild])
mchild = rchild;
else
mchild = lchild;
}
else{
mchild= lchild;
}
//printf("value of arr[mchild] %d",arr[mchild]);
//printf("value of arr[i] %d",arr[i]);
if (arr[i] < arr[mchild])
{
temp = arr[i];
arr[i] = arr[mchild];
arr[mchild] = temp;
}
}
/* temp = arr[1];
arr[1] = arr[bound];
arr[bound] = temp;*/
return;
}
void increase_key( int a[], int i, int key,int length ){
int j;
if ( i < 0) {
cout << "INDEX CAN'T BE LESS THAN ZERO" << endl;
}
a[i] = key;
for ( j = 0; j < length; j++) {
heapify( a, length);
}
}
void insert ( int a[], int num, int length )
{
length = length+1;
increase_key( a,length, num,length);
}
void max( int a[] )
{
cout << " MAX PRIORITY " << a[1] << endl;
}
void maxextract ( int a[] )
{
int temp = a[1];
a[1] = a[length];
a[length] = temp;
length = length - 1;
for ( int j = 0; j < length; j++) {
heapify( a, length);
}
}
void display ( int a[] )
{
cout << "length" << length << endl;
for ( int j = 1; j <=length; j++) {
cout << a[j] << " ";
}
}
int main()
{
int a [100];
int i=1,j;
int u;
int v;
int menu =1;
length =10;
while ( i <= 10){
a[i]= rand()%100;
i++;
}
for ( j = 0; j < length; j++) {
heapify( a, length);
}
cout << "INITIAL HEAP " << endl;
for ( j = 1; j <=length; j++) {
cout << a[j] << " ";
}
cout << endl;
cout << "1 -INSERT " << endl;
cout << "2 -MAX " << endl;
cout << "3 -MAX EXTRACT " << endl;
cout << "4 -INCREASE KEY " << endl;
cout << "5 -DISPLAY " << endl;
while ( menu == 1 ) {
int choice;
cout << "ENTER YOUR CHOICE " << endl;
cin >> choice;
switch (choice) {
case 1:
int num;
cout << "ENTER NUMBER" << endl;
cin >> num;
insert( a , num, length );
length = length ++;
break;
case 2:
max ( a );
break;
case 3:
maxextract(a);
// length = length --;
break;
case 4:
cout << endl <<"WHICH POSITION YOU WANT TO CHANGE " << endl;
cin >>u;
cout << "ENTER VALUE OF INDEX"<< endl;
cin >>v;
increase_key( a, u,v,length );
break;
case 5 :
display( a);
}
cout << "PRESS 1 TO PERFORM MORE" << endl;
cin >> menu;
}
return 0;
}