-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_heap.cpp
executable file
·199 lines (179 loc) · 3.5 KB
/
min_heap.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <bits/stdc++.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
class Heap
{
int *heaparr;
int heapSize;
int capacity;
public:
Heap(int);
int getSize();
void insertMinHeap(int);
int extractMin();
void minHeapify(int);
int linearSearch(int);
int height();
int parent(int);
int leftChild(int);
int rightChild(int);
void printHeap();
void decreaseKey(int, int);
void deleteKey(int i);
int *heapsort(int);
};
Heap::Heap(int size)
{
heapSize = 0;
capacity = size;
heaparr = new int[capacity];
}
int Heap::parent(int index)
{
return (index - 1) / 2;
}
int Heap::leftChild(int i)
{
return (i * 2) + 1;
}
int Heap::rightChild(int i)
{
return (i * 2) + 2;
}
void Heap::insertMinHeap(int val)
{
if (heapSize == capacity)
{
std::cout << "Heap is Full" << std::endl;
return;
}
heapSize++;
int i = heapSize - 1;
heaparr[i] = val;
while (heaparr[i] != 0 && heaparr[i] < heaparr[parent(i)])
{
swap(&heaparr[parent(i)], &heaparr[i]);
i = parent(i);
}
}
int Heap::linearSearch(int val)
{
for (int i = 0; i < heapSize; i++)
{
if (heaparr[i] == val)
{
return heaparr[i];
}
}
return -1;
}
int Heap::getSize()
{
return heapSize;
}
int Heap::height()
{
return ceil(log2(heapSize + 1)) - 1;
}
int Heap::extractMin()
{
if (heapSize <= 0)
{
return INT_MAX;
}
else if (heapSize == 1)
{
heapSize--;
return heaparr[0];
}
else
{
int root = heaparr[0];
heaparr[0] = heaparr[heapSize - 1];
heapSize--;
minHeapify(0);
return root;
}
}
void Heap::minHeapify(int i)
{
int smallest = i;
int l = leftChild(i);
int r = rightChild(i);
if (l < heapSize && heaparr[l] < heaparr[smallest])
{
smallest = l;
}
else if (r < heapSize && heaparr[r] < heaparr[smallest])
{
smallest = r;
}
if (smallest != i)
{
swap(&heaparr[smallest], &heaparr[i]);
minHeapify(smallest);
}
}
void Heap::decreaseKey(int i, int val)
{
heaparr[i] = val;
while (i != 0 && heaparr[i] < heaparr[parent(i)])
{
swap(&heaparr[i], &heaparr[parent(i)]);
i = parent(i);
}
}
int *Heap::heapsort(int size)
{
int i = 0;
int *temp = new int[size];
while (i < size)
{
// std::cout << size << std::endl;
temp[i] = extractMin();
i++;
}
return temp;
}
void Heap::deleteKey(int i)
{
decreaseKey(i, INT_MIN);
extractMin();
}
void Heap::printHeap()
{
for (int i = 0; i < heapSize; i++)
{
std::cout << heaparr[i] << " ";
}
}
void printarray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
int main()
{
Heap heap(10);
heap.insertMinHeap(5);
heap.insertMinHeap(2);
heap.insertMinHeap(10);
heap.insertMinHeap(1);
heap.printHeap();
std::cout << std::endl;
// heap.extractMin();
// heap.deleteKey(1);
// heap.printHeap();
int size = heap.getSize();
int *newarr = heap.heapsort(heap.getSize());
std::cout << size << std::endl;
printarray(newarr, size);
return 0;
}