-
Notifications
You must be signed in to change notification settings - Fork 0
/
q1.cpp
121 lines (97 loc) · 2.32 KB
/
q1.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
#include <iostream>
using namespace std;
class MaxHeap {
private:
int* heap;
int capacity;
int size;
void heapify(int i) {
int largest = i;
int l = 2*i +1;
int r = 2*i +2;
if (l < size && heap[l] > heap[largest]) {
largest = l;
}
if (r < size && heap[r] > heap[largest]) {
largest = r;
}
if (largest != i) {
swap(heap[i], heap[largest]);
heapify(largest);
}
}
public:
MaxHeap(int cap){
capacity = cap;
size = 0;
heap = new int[capacity];
}
void insert(int value) {
if (size == capacity) {
cout << "Heap is full\n";
return;
}
int i = size;
heap[i] = value;
size++;
int parent = (i-1) / 2;
while (i != 0 && heap[parent] < heap[i]) {
swap(heap[i], heap[parent]);
i = parent;
}
}
int getMax() {
if (size == 0) {
cout << "Heap is empty\n";
return -1;
}
return heap[0];
}
int extractMax() {
if (size == 0) {
cout << "Heap is empty\n";
return -1;
}
int root = heap[0];
heap[0] = heap[size - 1];
size--;
heapify(0);
return root;
}
void printHeap() {
for (int i = 0; i <= size; ++i) {
cout << heap[i] << " ";
}
cout <<endl;
}
};
int main() {
// MaxHeap maxHeap(10);
// // Inserting elements into the heap
// maxHeap.insert(3);
// maxHeap.insert(9);
// maxHeap.insert(4);
// maxHeap.insert(7);
// maxHeap.insert(5);
MaxHeap maxHeap(10);
int num;
cout << "Enter numbers to insert into the MaxHeap (enter -1 to stop):\n";
// Read numbers and insert into the heap
while (true) {
cin >> num;
// Break the loop if -1 is entered
if (num == -1) {
break;
}
maxHeap.insert(num);
}
cout << "Heap: ";
maxHeap.printHeap();
// Print max element
cout << "Max: " << maxHeap.getMax() << "\n";
// Extract max element
cout << "Extracted Max: " << maxHeap.extractMax() << "\n";
cout << "Heap after extracting max: ";
maxHeap.printHeap();
return 0;
}