-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryHeap.cpp
134 lines (104 loc) · 2.85 KB
/
binaryHeap.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>
#include <vector>
using namespace std;
void heapify_Max(vector<int>& heap, const int& i) {
int largest = i;
int left = (2 * i) + 1;
int right = (2 * i) + 2;
int size = heap.size();
if (left < size && heap[left] > heap[largest]) {
largest = left;
}
if (right < size && heap[right] > heap[largest]) {
largest = right;
}
if (largest != i) {
swap(heap[largest], heap[i]);
heapify_Max(heap, largest);
}
}
void buildHeap_Max(vector<int>& heap) {
for (int i = (heap.size() / 2) - 1; i >= 0; i--) {
heapify_Max(heap, i);
}
}
void insertNode(vector<int>& heap, const int& data) {
heap.push_back(data);
int i = heap.size() - 1;
while (i != 0 && heap[(i - 1) / 2] < heap[i]) {
swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
void removeNode(vector<int>& heap) {
if (heap.size() == 0) {
cout << "Heap is empty." << endl;
return;
}
heap[0] = heap[heap.size() - 1];
heap.pop_back();
heapify_Max(heap, 0);
}
void printHeap(const vector<int>& heap) {
cout << "Heap : ";
for (int element : heap) {
cout << '[' << element << ']';
}
cout << endl << endl;
}
void mainMenu() {
cout << "Choose operation: " << endl;
cout << "[1] Build Heap." << endl;
cout << "[2] Insert an element in Heap." << endl;
cout << "[3] Remove an element in Heap." << endl;
cout << "[4] Print Heap." << endl;
cout << "[5] Exit." << endl;
cout << "Choose your option: ";
}
int main() {
int option, data;
vector<int> heap;
do {
mainMenu();
cin >> option;
switch (option) {
case 1:
int size;
cout << "Enter the number of elements in Heap: ";
cin >> size;
heap.resize(size);
for (int i = 0; i < size; i++) {
cout << "Enter element no. #" << (i + 1) << " : ";
cin >> heap[i];
}
buildHeap_Max(heap);
cout << endl << "Heap is built..." << endl << endl;
printHeap(heap);
break;
case 2:
cout << "Enter the value to be inserted: ";
cin >> data;
insertNode(heap, data);
cout << endl << "Element inserted..." << endl << endl;
printHeap(heap);
break;
case 3:
removeNode(heap);
cout << endl << "Element removed..." << endl << endl;
printHeap(heap);
break;
case 4:
cout << endl;
printHeap(heap);
cout << endl;
break;
case 5:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid option." << endl;
break;
}
} while (option != 5);
return 0;
}