Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
MinHeap Class Reference

Public Member Functions

 MinHeap (int capacity)
 
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

Constructor & Destructor Documentation

◆ MinHeap()

MinHeap::MinHeap ( int  cap)

Constructor

Parameters
[in]capacityinitial heap capacity

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

50  {
51  heap_size = 0;
52  capacity = cap;
53  harr = new int[cap];
54 }

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].

78  {
79  harr[i] = new_val;
80  while (i != 0 && harr[parent(i)] > harr[i]) {
81  std::swap(harr[i], harr[parent(i)]);
82  i = parent(i);
83  }
84 }
Here is the call graph for this function:

◆ 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()

107  {
108  decreaseKey(i, INT_MIN);
109  extractMin();
110 }
Here is the call graph for this function:

◆ extractMin()

int MinHeap::extractMin ( )

to extract the root which is the minimum element

87  {
88  if (heap_size <= 0)
89  return INT_MAX;
90  if (heap_size == 1) {
91  heap_size--;
92  return harr[0];
93  }
94 
95  // Store the minimum value, and remove it from heap
96  int root = harr[0];
97  harr[0] = harr[heap_size - 1];
98  heap_size--;
99  MinHeapify(0);
100 
101  return root;
102 }
Here is the call graph for this function:

◆ getMin()

int MinHeap::getMin ( )
inline

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

40 { return harr[0]; }

◆ insertKey()

void MinHeap::insertKey ( int  k)

Inserts a new key 'k'

57  {
58  if (heap_size == capacity) {
59  std::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  std::swap(harr[i], harr[parent(i)]);
71  i = parent(i);
72  }
73 }
Here is the call graph for this function:

◆ left()

int MinHeap::left ( int  i)
inline

to get index of left child of node at index i

28 { 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

115  {
116  int l = left(i);
117  int r = right(i);
118  int smallest = i;
119  if (l < heap_size && harr[l] < harr[i])
120  smallest = l;
121  if (r < heap_size && harr[r] < harr[smallest])
122  smallest = r;
123  if (smallest != i) {
124  std::swap(harr[i], harr[smallest]);
125  MinHeapify(smallest);
126  }
127 }
Here is the call graph for this function:

◆ right()

int MinHeap::right ( int  i)
inline

to get index of right child of node at index i

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

The documentation for this class was generated from the following file:
MinHeap::heap_size
int heap_size
Current number of elements in min heap.
Definition: binaryheap.cpp:13
MinHeap::decreaseKey
void decreaseKey(int i, int new_val)
Definition: binaryheap.cpp:78
MinHeap::capacity
int capacity
maximum possible size of min heap
Definition: binaryheap.cpp:12
std::cout
k
ll k
Definition: matrix_exponentiation.cpp:48
MinHeap::harr
int * harr
pointer to array of elements in heap
Definition: binaryheap.cpp:11
MinHeap::MinHeapify
void MinHeapify(int)
Definition: binaryheap.cpp:115
MinHeap::left
int left(int i)
Definition: binaryheap.cpp:28
std::swap
T swap(T... args)
MinHeap::extractMin
int extractMin()
Definition: binaryheap.cpp:87
MinHeap::right
int right(int i)
Definition: binaryheap.cpp:31