Skip to content

Commit c8ad735

Browse files
authored
Merge pull request #123 from ojas1234/master
Added Binary heap in data structures
2 parents e946cc8 + a9d7f92 commit c8ad735

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

Data Structure/Binaryheap.cpp

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// A C++ program to demonstrate common Binary Heap Operations
2+
#include<iostream>
3+
#include<climits>
4+
using namespace std;
5+
6+
// Prototype of a utility function to swap two integers
7+
void swap(int *x, int *y);
8+
9+
// A class for Min Heap
10+
class MinHeap
11+
{
12+
int *harr; // pointer to array of elements in heap
13+
int capacity; // maximum possible size of min heap
14+
int heap_size; // Current number of elements in min heap
15+
public:
16+
// Constructor
17+
MinHeap(int capacity);
18+
19+
// to heapify a subtree with the root at given index
20+
void MinHeapify(int );
21+
22+
int parent(int i) { return (i-1)/2; }
23+
24+
// to get index of left child of node at index i
25+
int left(int i) { return (2*i + 1); }
26+
27+
// to get index of right child of node at index i
28+
int right(int i) { return (2*i + 2); }
29+
30+
// to extract the root which is the minimum element
31+
int extractMin();
32+
33+
// Decreases key value of key at index i to new_val
34+
void decreaseKey(int i, int new_val);
35+
36+
// Returns the minimum key (key at root) from min heap
37+
int getMin() { return harr[0]; }
38+
39+
// Deletes a key stored at index i
40+
void deleteKey(int i);
41+
42+
// Inserts a new key 'k'
43+
void insertKey(int k);
44+
};
45+
46+
// Constructor: Builds a heap from a given array a[] of given size
47+
MinHeap::MinHeap(int cap)
48+
{
49+
heap_size = 0;
50+
capacity = cap;
51+
harr = new int[cap];
52+
}
53+
54+
// Inserts a new key 'k'
55+
void MinHeap::insertKey(int k)
56+
{
57+
if (heap_size == capacity)
58+
{
59+
cout << "\nOverflow: Could not insertKey\n";
60+
return;
61+
}
62+
63+
// First insert the new key at the end
64+
heap_size++;
65+
int i = heap_size - 1;
66+
harr[i] = k;
67+
68+
// Fix the min heap property if it is violated
69+
while (i != 0 && harr[parent(i)] > harr[i])
70+
{
71+
swap(&harr[i], &harr[parent(i)]);
72+
i = parent(i);
73+
}
74+
}
75+
76+
// Decreases value of key at index 'i' to new_val. It is assumed that
77+
// new_val is smaller than harr[i].
78+
void MinHeap::decreaseKey(int i, int new_val)
79+
{
80+
harr[i] = new_val;
81+
while (i != 0 && harr[parent(i)] > harr[i])
82+
{
83+
swap(&harr[i], &harr[parent(i)]);
84+
i = parent(i);
85+
}
86+
}
87+
88+
// Method to remove minimum element (or root) from min heap
89+
int MinHeap::extractMin()
90+
{
91+
if (heap_size <= 0)
92+
return INT_MAX;
93+
if (heap_size == 1)
94+
{
95+
heap_size--;
96+
return harr[0];
97+
}
98+
99+
// Store the minimum value, and remove it from heap
100+
int root = harr[0];
101+
harr[0] = harr[heap_size-1];
102+
heap_size--;
103+
MinHeapify(0);
104+
105+
return root;
106+
}
107+
108+
109+
// This function deletes key at index i. It first reduced value to minus
110+
// infinite, then calls extractMin()
111+
void MinHeap::deleteKey(int i)
112+
{
113+
decreaseKey(i, INT_MIN);
114+
extractMin();
115+
}
116+
117+
// A recursive method to heapify a subtree with the root at given index
118+
// This method assumes that the subtrees are already heapified
119+
void MinHeap::MinHeapify(int i)
120+
{
121+
int l = left(i);
122+
int r = right(i);
123+
int smallest = i;
124+
if (l < heap_size && harr[l] < harr[i])
125+
smallest = l;
126+
if (r < heap_size && harr[r] < harr[smallest])
127+
smallest = r;
128+
if (smallest != i)
129+
{
130+
swap(&harr[i], &harr[smallest]);
131+
MinHeapify(smallest);
132+
}
133+
}
134+
135+
// A utility function to swap two elements
136+
void swap(int *x, int *y)
137+
{
138+
int temp = *x;
139+
*x = *y;
140+
*y = temp;
141+
}
142+
143+
// Driver program to test above functions
144+
int main()
145+
{
146+
MinHeap h(11);
147+
h.insertKey(3);
148+
h.insertKey(2);
149+
h.deleteKey(1);
150+
h.insertKey(15);
151+
h.insertKey(5);
152+
h.insertKey(4);
153+
h.insertKey(45);
154+
cout << h.extractMin() << " ";
155+
cout << h.getMin() << " ";
156+
h.decreaseKey(2, 1);
157+
cout << h.getMin();
158+
return 0;
159+
}

0 commit comments

Comments
 (0)