forked from MakeContributions/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
min-heap.cpp
138 lines (112 loc) · 2.85 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
/*
Implementation of the min-heap using an array.
Min-Heap is a Binary tree where each child nodes value is greater
than that of their parent. It is used when you need quick access to
the smallest number in the array.
*/
#include <iostream>
class Heap{
public:
void insert(const int& data);
void removemin();
void print();
Heap();
~Heap();
private:
// size - number of elements in the array
// capacity - number of elements the array can store
int size, capacity;
int *items;
int parent(unsigned index) const;
int child(unsigned index) const;
void heapifyUP(unsigned index);
void heapifyDOWN(unsigned index);
void growArray();
};
Heap::~Heap(){
delete[] items;
items = nullptr;
}
Heap::Heap(): size(0), capacity(2){
items = new int[capacity + 1];
}
int Heap::parent(unsigned index) const{ return index/2; }
void Heap::growArray(){
/**
* Inorder to accommodate more numbers in the array
* Allocate memory in the heap
* Double the size of the old array up to (capacity*2) + 1
* Copy the elements of the previous array into the new array
*/
// Doubling the size of the array
int* new_Array = new int[(capacity * 2) + 1];
// Copying the elements of the old array to the new array
for(int i=1; i<=size; i++){ new_Array[i] = items[i]; }
// Doubling the capacity
capacity *= 2;
// delete the items inorder to avoid any memory leak
delete[] items;
// set items to point to new_Array
items = new_Array;
}
void Heap::insert(const int& data){
// if size == capacity it means the array is full and need to grow
if(size == capacity){ growArray(); }
items[++size] = data;
heapifyUP(size);
}
void Heap::heapifyUP(unsigned index){
if(index > 1){
if(items[index] < items[parent(index)]){
std::swap(items[index], items[parent(index)]);
heapifyUP(parent(index));
}
}
}
void Heap::removemin(){
std::swap(items[1], items[size--]);
heapifyDOWN(1);
}
int Heap::child(unsigned index) const{
unsigned left = index * 2;
unsigned right = (index * 2) + 1;
if(right > size){ return left; }
else if(items[left] <= items[right]){ return left; }
return right;
}
void Heap::heapifyDOWN(unsigned index){
int childindex = child(index);
if(index*2 <= size){
if(items[index] > items[childindex]){
std::swap(items[index], items[childindex]);
heapifyDOWN(childindex);
}
}
}
void Heap::print(){
for(int i=1; i<=size; i++){ std::cout << items[i] << " "; }
std::cout << std::endl;
}
int main(){
Heap heap;
heap.insert(4);
heap.insert(10);
heap.insert(2);
heap.insert(22);
heap.insert(45);
heap.insert(18);
heap.insert(-8);
heap.insert(95);
heap.insert(13);
heap.insert(42);
heap.removemin();
heap.removemin();
heap.print();
/*
Output: 4 10 18 13 42 22 45 95
Time complexity to build the heap: O(n)
Time complexity to remove min: O(log(n))
Time complexity to remove all elements: O(n*log(n))
*/
return 0;
}