-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapOper.cpp
74 lines (62 loc) · 1.17 KB
/
heapOper.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
/*
@chiranjeevi
Date: 04-June-2017
*/
#include <iostream>
using namespace std;
void exchange(int arr[], int i, int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
void swim(int arr[], int k){
while(k > 1 and arr[k] > arr[k/2]){
exchange(arr, k, k/2);
k/=2;
}
}
void sink(int arr[], int n, int k){
int j;
while(2*k <= n){
j=2*k;
if(j < n and arr[j] < arr[j+1])
j+=1;
if(arr[k] < arr[j]){
exchange(arr, k, j);
k=j;
}
else
break;
}
}
void insert(int arr[], int *np, int value){
arr[++(*np)]=value;
swim(arr, *np);
}
void delMax(int arr[], int *np){
int max=arr[1];
arr[1]=arr[*np];
(*np)--;
sink(arr, *np, 1);
}
int max(int arr[]){
return arr[1];
}
bool isEmpty(int n){
return n==0;
}
void print(int arr[], int n){
for(int i=1; i<=n; i++)
cout<<(char)arr[i]<<' ';
cout<<endl;
}
int main() {
int arr[100] = {0, 'T', 'P', 'R', 'N', 'H', 'O', 'A', 'E', 'I', 'G'};
int n = 10;
insert(arr, &n, 'S');
delMax(arr, &n);
delMax(arr, &n);
insert(arr, &n, 'S');
print(arr, n);
return 0;
}