-
Notifications
You must be signed in to change notification settings - Fork 1
/
Heap Implementation
163 lines (146 loc) · 3.34 KB
/
Heap Implementation
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
#include <iostream>
#include <queue>
using namespace std;
class heap{
public:
int arr[100];
int size;
heap(){
arr[0] = -1;
size = 0;
}
void insert(int val){
size = size+1;
int index = size;
arr[index] = val;
while(index>1){
int parent = index/2;
if(arr[index]>arr[parent]){
swap(arr[parent],arr[index]);
index = parent;
}
else{
return ;
}
}
}
void print(){
for(int i=1;i<=size;i++){
cout<<arr[i]<<" ";
}cout<<endl;
}
void deletion(){
if(size == 0){
cout<<"Nothing to delete"<<endl;
return;
}
arr[1] = arr[size];
size--;
//take root node to its correct position
int i = 1;
while(i<size){
int leftIndex = 2*i;
int rightIndex = 2*i+1;
if(leftIndex<size && arr[leftIndex]>arr[i]){
swap(arr[leftIndex],arr[i]);
i = leftIndex;
}
else if(rightIndex<size && arr[rightIndex]>arr[i]){
swap(arr[rightIndex],arr[i]);
i = rightIndex;
}
else{
return;
}
}
}
};
void heapify(int arr[],int n, int i){
int largest = i;
int left = 2*i;
int right = 2*i+1;
if(left<=n && arr[largest]<arr[left]){
largest = left;
}
if(right<=n && arr[largest]<arr[right]){
largest = right;
}
if(largest!=i){
swap(arr[largest], arr[i]);
heapify(arr,n,largest);
}
}
void heapSort(int arr[], int n){
int t = n;
while(t>1){
//step 1: swap
swap(arr[1], arr[t]);
t--;
heapify(arr,t,1);
}
}
int main(){
heap h;
h.insert(50);
h.insert(55);
h.insert(53);
h.insert(52);
h.insert(54);
h.print();
h.deletion();
h.print();
//heap creation
int arr[6] = {-1,54,53,55,52,50};
int n =5;
for(int i=n/2;i>0;i--){
heapify(arr,n,i);
}
cout<<"Printing the array after heapify: "<<endl;
for(int i=1;i<n;i++){
cout<<arr[i]<<" ";
}cout<<endl;
//heap sort
heapSort(arr,n);
cout<<"Printing heap sort"<<endl;
for(int i =1;i<=n;i++){
cout<<arr[i]<<" ";
}cout<<endl;
cout<<"Using Priority Queue here"<<endl;
//max heap
priority_queue<int> pq;
pq.push(4);
pq.push(2);
pq.push(5);
pq.push(3);
//max element
cout<<"Element at top: "<<pq.top()<<endl;
pq.pop();
cout<<"After pop: "<<endl;
cout<<"Element at top: "<<pq.top()<<endl;
cout<<"Size is: "<<pq.size()<<endl;
if(pq.empty()){
cout<<"heap is empty: "<<endl;
}
else{
cout<<"Heap is not empty: "<<endl;
}
//min heap
priority_queue<int, vector<int>, greater<int>> minheap;
minheap.push(4);
minheap.push(2);
minheap.push(5);
minheap.push(3);
//max element
cout<<"Element at top: "<<minheap.top()<<endl;
minheap.pop();
cout<<"After pop: "<<endl;
cout<<"Element at top: "<<minheap.top()<<endl;
cout<<"Size is: "<<minheap.size()<<endl;
if(minheap.empty()){
cout<<"heap is empty: "<<endl;
}
else{
cout<<"Heap is not empty: "<<endl;
}
return 0;
}