TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
MinHeap Class Reference

Public Member Functions

 MinHeap (int cap)
 
void MinHeapify (int)
 
int parent (int i)
 
int left (int i)
 
int right (int i)
 
int extractMin ()
 
void decreaseKey (int i, int new_val)
 
int getMin ()
 
void deleteKey (int i)
 
void insertKey (int k)
 

Private Attributes

int * harr
 pointer to array of elements in heap
 
int capacity
 maximum possible size of min heap
 
int heap_size
 Current number of elements in min heap.
 

Detailed Description

A class for Min Heap

Definition at line 10 of file binaryheap.cpp.

Constructor & Destructor Documentation

◆ MinHeap()

MinHeap::MinHeap ( int cap)
inlineexplicit

Constructor: Builds a heap from a given array a[] of given size

Parameters
[in]capacityinitial heap capacity

Definition at line 19 of file binaryheap.cpp.

19 {
20 heap_size = 0;
21 capacity = cap;
22 harr = new int[cap];
23 }
int * harr
pointer to array of elements in heap
int capacity
maximum possible size of min heap
int heap_size
Current number of elements in min heap.

◆ ~MinHeap()

MinHeap::~MinHeap ( )
inline

Definition at line 51 of file binaryheap.cpp.

51{ delete[] harr; }

Member Function Documentation

◆ decreaseKey()

void MinHeap::decreaseKey ( int i,
int new_val )

Decreases key value of key at index i to new_val

Decreases value of key at index 'i' to new_val. It is assumed that new_val is smaller than harr[i].

Definition at line 76 of file binaryheap.cpp.

76 {
77 harr[i] = new_val;
78 while (i != 0 && harr[parent(i)] > harr[i]) {
79 std::swap(harr[i], harr[parent(i)]);
80 i = parent(i);
81 }
82}

◆ deleteKey()

void MinHeap::deleteKey ( int i)

Deletes a key stored at index i

This function deletes key at index i. It first reduced value to minus infinite, then calls extractMin()

Definition at line 105 of file binaryheap.cpp.

105 {
106 decreaseKey(i, INT_MIN);
107 extractMin();
108}
int extractMin()
void decreaseKey(int i, int new_val)

◆ extractMin()

int MinHeap::extractMin ( )

to extract the root which is the minimum element

Definition at line 85 of file binaryheap.cpp.

85 {
86 if (heap_size <= 0)
87 return INT_MAX;
88 if (heap_size == 1) {
89 heap_size--;
90 return harr[0];
91 }
92
93 // Store the minimum value, and remove it from heap
94 int root = harr[0];
95 harr[0] = harr[heap_size - 1];
96 heap_size--;
97 MinHeapify(0);
98
99 return root;
100}
void MinHeapify(int)

◆ getMin()

int MinHeap::getMin ( )
inline

Returns the minimum key (key at root) from min heap

Definition at line 43 of file binaryheap.cpp.

43{ return harr[0]; }

◆ insertKey()

void MinHeap::insertKey ( int k)

Inserts a new key 'k'

Definition at line 55 of file binaryheap.cpp.

55 {
56 if (heap_size == capacity) {
57 std::cout << "\nOverflow: Could not insertKey\n";
58 return;
59 }
60
61 // First insert the new key at the end
62 heap_size++;
63 int i = heap_size - 1;
64 harr[i] = k;
65
66 // Fix the min heap property if it is violated
67 while (i != 0 && harr[parent(i)] > harr[i]) {
68 std::swap(harr[i], harr[parent(i)]);
69 i = parent(i);
70 }
71}
double k(double x)
Another test function.

◆ left()

int MinHeap::left ( int i)
inline

to get index of left child of node at index i

Definition at line 31 of file binaryheap.cpp.

31{ return (2 * i + 1); }

◆ MinHeapify()

void MinHeap::MinHeapify ( int i)

to heapify a subtree with the root at given index

A recursive method to heapify a subtree with the root at given index This method assumes that the subtrees are already heapified

Definition at line 113 of file binaryheap.cpp.

113 {
114 int l = left(i);
115 int r = right(i);
116 int smallest = i;
117 if (l < heap_size && harr[l] < harr[i])
118 smallest = l;
119 if (r < heap_size && harr[r] < harr[smallest])
120 smallest = r;
121 if (smallest != i) {
122 std::swap(harr[i], harr[smallest]);
123 MinHeapify(smallest);
124 }
125}
int left(int i)
int right(int i)
double l(double x)
Another test function.

◆ parent()

int MinHeap::parent ( int i)
inline

Definition at line 28 of file binaryheap.cpp.

28{ return (i - 1) / 2; }

◆ right()

int MinHeap::right ( int i)
inline

to get index of right child of node at index i

Definition at line 34 of file binaryheap.cpp.

34{ return (2 * i + 2); }

Member Data Documentation

◆ capacity

int MinHeap::capacity
private

maximum possible size of min heap

Definition at line 12 of file binaryheap.cpp.

◆ harr

int* MinHeap::harr
private

pointer to array of elements in heap

Definition at line 11 of file binaryheap.cpp.

◆ heap_size

int MinHeap::heap_size
private

Current number of elements in min heap.

Definition at line 13 of file binaryheap.cpp.


The documentation for this class was generated from the following file: