# A Beginner’s Guide to Binary Heaps

## Heap Operations

### Element Class Definition and Helper Functions

class Element {
public:
int key;
int value;

inline Element() :
key(0),
value(0)
{}
};

size_t parent(size_t pos) {
return (i - 1) / 2;
}

size_t left(size_t pos) {
return 2 * i + 1;
}

size_t right(size_t pos) {
return 2 * i + 2;
}

void swap(Element& a, Element& b) {
Element& tmp = a;
a = b;
b = tmp;
}

### MinHeap Class Definition

class MinHeap {
std::vector<Element> arr;

Element getMin();
Element extractMin();
void decreaseKey(size_t index, int new_key);
void insert(Element e);
void remove(size_t index);
void buildHeap(std::vector<Element> elements);

};

### MaxHeap Class Definition

class MaxHeap {
std::vector<Element> arr;

Element getMax();
Element extractMax();
void increaseKey(size_t index, int new_key);
void insert(Element e);
void remove(size_t index);
void buildHeap(std::vector<Element> elements);
};

### Heap getMin() / getMax() Operation

The function, getMin() returns the minimum element in a min-heap. This is relatively simple to do, because, the minimum element in a min-heap is the root node. In the second section, we discussed how the root node is the first element in the array containing the heap. Thus, the getMin() function would be implemented as such:

Element MinHeap::getMin() {
if(arr.size() == 0)
return Element();
return arr;
}

Element MaxHeap::getMax() {
if(arr.size() == 0)
return Element();
return arr;
}

The time complexity of getMin()/getMax() is $$O(1)$$.

The space complexity of getMin()/getMax() is $$O(1)$$ because no allocations are made throughout the algorithm.

### Heap extractMin()/extractMax() Operation

The function, extractMin()/extractMax() returns the current minimum (or maximum) from the heap, and then removes it from the heap. The functions extractMin() and extractMax() can be implemented as such:

Element MinHeap::extractMin() {
if(arr.size() == 0)
return Element();

Element root = arr;
arr = arr[arr.size() - 1];

arr.pop_back();

minHeapify(0);

return root;
}

Element MaxHeap::extractMax() {
if(arr.size() == 0)
return Element();

Element root = arr;
arr = arr[arr.size() - 1];

arr.pop_back();

maxHeapify(0);

return root;
}

#### How does extractMin() / extractMax() work?

1. Check to see if the array is not empty. This would result in an error if true.
2. Store the root (0th element) in a temporary variable, $$root$$.
3. Overwrite the root with the last element in the heap, $$arr[arr.\text{size}() – 1]$$.
4. Reduce the size of the heap by one, by truncating the last value (arr.pop_back()).
5. Heapify the root (0th element).
6. Return original the root from the temporary variable.

The time complexity of these operations is $$O(h)=O(\lceil \log n\rceil)$$, where $$h$$ is the height of the tree

### Heap minHeapify()/maxHeapify Operation

minHeapify() and maxHeapify are operations which heapify an element into its right place. a subtree with the root index passed in. The heapify operation assumes that the subtrees of the subtree being heapified are already heapified (proper heaps).

Both operations take a maximum time of $$O(h)$$ where $$h$$ is the height of the tree. Thus, the maximum time complexity of heapify() is $$O(\log n)$$

void MinHeap::minHeapify(size_t pos) {
// Calculate the address of the left and right children
int l = left(pos);
int r = right(pos);
// Initialize smallest so it is pointing to the parent node
int smallest = pos;

// These if statements set smallest to l or r if l or r are smaller
// than the current smallest
if(l < arr.size() && arr[l] < arr[pos].key) {
smallest = l;
}
if(r < arr.size() && arr[r] < arr[smallest].key) {
smallest = r;
}

// This if statement checks to see if smallest changed through
// the two if statements. If it did, the smallest element is swapped
// with the parent. Heapify is thereafter called on the new location
// for the original root
if(smallest != pos) {
swap(arr[pos], arr[smallest]);
minHeapify(smallest);
}
}

void MaxHeap::maxHeapify(size_t pos) {
// Calculate the address of the left and right children
int l = left(pos);
int r = right(pos);
// Initialize the largest value to the root node
int largest = pos;

// These two if statements set largest to l or r respectively if
// l or r is smaller than smallest.
if(l < arr.size() && arr[l] > arr[pos].key) {
largest = l;
}
if(r < arr.size() && arr[r] > arr[pos].key) {

largest = r;
}

// This if statement checks to see if largest changed through
// the two if statements. If it did, the largest element is swapped
// with the parent. Heapify is thereafter called on the new location
// for the original root
if(largest != pos) {
swap(arr[pos], arr[largest];
maxHeapify(largest);
}
}

#### How minHeapify() and maxHeapify() work:

1. Addresses of the left and right children are calculated by the first two lines
2. The smallest of, left, right and the current node is found using the first two if statements.
3. The third if statement checks to see if smallest or largest no longer point to the node pointed by pos.
1. If smallest / largest does not equal pos, we swap the values of both, and then heapify the child pointed by smallest / largest.

### Heap decreaseKey() and increaseKey() Operation

To decrease the key of a value in a min-heap, you may use the decreaseKey() operation.

To increase the key of a value in a max-heap, you may use the increaseKey() operation.

Both operations take $$O(\log n)$$ time.

void MinHeap::decreaseKey(size_t position, int new_key) {
arr[position].key = new_key;

while(position != 0 && arr[parent(position)] > arr[position]) {
swap(arr[position], arr[parent(position)]);
position = parent(position);
}
}

void MaxHeap::increaseKey(size_t position, int new_key) {
arr[position].key = new_key;

while(position != 0 && arr[parent(position)] < arr[position]) {
swap(arr[position], arr[parent(position)]);
position = parent(position);
}
}

### Heap insert() Operation

The insert() operation of a heap inserts a value in its correct position. It takes up to $$O(h)=O(\log n)$$ time.

void MinHeap::insert(Element e) {
size_t pos = arr.size();
arr.push_back(e);

while(pos != 0 && arr[parent(pos)] > arr[pos]) {
swap(arr[pos], arr[parent(pos)]);
pos = parent(pos);
}
}

void MaxHeap::insert(Element e) {
size_t pos = arr.size();
arr.push_back(e);

while(pos != 0 && arr[parent(pos)] < arr[pos]) {
swap(arr[pos], arr[parent(pos)]);
pos = parent(pos);
}
}

To insert an element into a heap, the insert() operation inserts a key at the end of the array, and then moves it upwards in the tree until it is in the right spot.

### Heap remove() Operation

To delete an element in a min-heap, we need to make it the root element, and then extract it. In a min-heap, we can do this by setting its key to the current root – 1, and in a max-heap, we can do this by setting the its key to the current root + 1. To move the element to its new position, we call decreaseKey() or increaseKey() respectively. This operation takes up to $$O(h)=O(\log n)$$ time.

void MinHeap::remove(size_t index) {
decreaseKey(i, arr.key - 1);
extractMin();
}

void MaxHeap::remove(size_t index) {
increaseKey(i, arr.key + 1);
extractMin();
}

### Heap buildHeap() Operation

Here is a function which builds a heap from an array:

BuildHeap() has a time complexity of $$O(n)$$

void MaxHeap::buildMaxHeap(std::vector<Element> array) {
arr = array;
for(size_t i = arr.size() / 2; i != 0; i--) {
maxHeapify(i);
}
}

void MinHeap::buildMaxHeap(std::vector<Element> array) {
arr = array;
for(size_t i = arr.size() / 2; i != 0; i--) {
minHeapify(i);
}
}

## 3 thoughts on “A Beginner’s Guide to Binary Heaps”

1. Kaan says:

Very clear explanation!

2. BestMarcelino says:

I see you don’t monetize devyashis.me, don’t waste
1. yash101 says:
{"ft_lang_label":"__label__en","ft_lang_prob":0.5922037,"math_prob":0.9855427,"size":7846,"snap":"2019-51-2020-05","text_gpt3_token_len":2021,"char_repetition_ratio":0.14294823,"word_repetition_ratio":0.2249191,"special_character_ratio":0.28804487,"punctuation_ratio":0.16424802,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99657255,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-23T07:39:49Z\",\"WARC-Record-ID\":\"<urn:uuid:85af3b00-8ff4-4d43-818f-ff2f0357021c>\",\"Content-Length\":\"40825\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fb2f8919-5913-470f-a9cc-b4de4cc94a26>\",\"WARC-Concurrent-To\":\"<urn:uuid:01035e19-43e2-4f19-806d-59e741523704>\",\"WARC-IP-Address\":\"108.61.158.250\",\"WARC-Target-URI\":\"https://www.devyashis.me/computer-science/dsa/2018/-/a-proper-explanation-of-binary-heaps/3\",\"WARC-Payload-Digest\":\"sha1:264DJ3G7UYY6VA2O4M7B5AYFL46YAB5F\",\"WARC-Block-Digest\":\"sha1:XC6XH7CXMBNJ7CWLSWCGIO7MS6YDCMNL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250609478.50_warc_CC-MAIN-20200123071220-20200123100220-00144.warc.gz\"}"}