Table of contents
1.
Introduction
2.
Types of Binary Heaps
2.1.
1. Min Heap
2.2.
2. Max Heap
3.
Binary Heap Operations
3.1.
1. Insertion
3.2.
2. Extract Min (or Max)
3.3.
3. Heapify
3.4.
4. Heap Sort
4.
Implementation of Binary Heap in Go
4.1.
Example
4.2.
Go
5.
Time Complexity Analysis
6.
Use Cases and Applications
6.1.
Priority Queues
6.2.
Dijkstra's Shortest Path Algorithm
6.3.
Huffman Coding
7.
Frequently Asked Questions
7.1.
Can I use a Binary Heap to sort an array? 
7.2.
Can I implement both Min Heap and Max Heap in the same program? 
7.3.
When should I use a Min Heap versus a Max Heap?
8.
Conclusion
Last Updated: Mar 27, 2024
Hard

Golang Program to Represent a Binary Heap

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Binary Heap is a special type of tree where each parent node has values smaller (or larger) than its child nodes. In a Golang program, we can create and manipulate a Binary Heap. By doing so, we can easily find the smallest (or largest) value in constant time, add new values while maintaining the heap property, and even sort elements quickly. 

Golang program to represent a Binary Heap

This program will guide you through understanding how to build and utilize a Binary Heap using simple yet powerful code examples.

Types of Binary Heaps

There are two types of Binary Heaps that are mentioned below: 

1. Min Heap

In a Min Heap, the parent nodes have values that are smaller than or equal to their child nodes. This means that the root node contains the smallest value in the heap. Each node's value is less than or equal to the values of its children. Min Heaps are used to efficiently retrieve the smallest element from a collection, making them ideal for priority queues and implementing algorithms like Dijkstra's shortest path algorithm.

2Max Heap

In a Max Heap, parent nodes have values that are greater than or equal to their child nodes. This results in the root node containing the largest value in the heap. Every node's value is greater than or equal to the values of its children. Max Heaps have applications in scenarios where you need to quickly access the highest-priority element, such as in job scheduling or implementing the Huffman coding algorithm for data compression.

 

To sum it up, Min Heaps focus on quickly retrieving the smallest element, while Max Heaps excel at retrieving the largest element, each offering unique advantages in different situations.

Binary Heap Operations

Let’s discuss the operations of Binary Heap.

1. Insertion

Insertion in a Binary Heap is the process of adding a new value to the heap while maintaining its unique structure. This structure ensures that each parent node's value is either smaller or larger than its child nodes, depending on whether it's a min heap or a max heap.

Syntax

To insert a value into the binary heap, you can use the Insert function. Its syntax looks like this:

f(h *MinHeap) Insert(value int)

 

2. Extract Min (or Max)

Extract Min (or Max) in a Binary Heap is the operation of removing and retrieving the smallest (or largest) value from the heap while preserving its hierarchical arrangement where parent nodes have values smaller (or larger) than their children, based on the heap type.

To perform the operation, the root node, which contains the minimum (or maximum) value, is extracted from the heap. This removal might temporarily disrupt the heap's structure. To restore the structure, the last leaf node is moved to the root position. Then, a process called "heapify" is initiated. In Heapify, the value at the root is compared with its child nodes. If the value is larger (for a max heap) or smaller (for a min-heap) than its children, they are swapped. This swapping is repeated down the tree until the heap property is satisfied.

Example: Suppose you have a min heap with values [5, 10, 12, 15], and you want to extract the minimum value. The root node contains 5, the smallest value. When you extract 5, you replace it with the last leaf node (15) and then initiate heapify. Comparing 15 with its children, you find that 15 should be swapped with 10 to satisfy the heap property. The heap becomes [10, 15, 12], with 5 successfully extracted.

Syntax 

The ExtractMin (or ExtractMax for max heaps) function is used to remove and retrieve the smallest (or largest) value from the heap. Its syntax looks like this:

func (h *MinHeap) ExtractMin() int

 

3. Heapify

Heapify in Binary Heap Operations is the process of restoring the correct hierarchical order within a heap after a modification, such as insertion or extraction, temporarily disrupts the structure. It ensures that the heap property is maintained, where parent nodes are smaller (or larger) than their child nodes based on the heap type.

When a value is added or removed in a heap, it can momentarily violate the structure. Heapify is responsible for fixing this. The process begins at a specific node and compares its value with the values of its children. If the value doesn't satisfy the heap property (smaller for a min heap, larger for a max heap), a swap is performed with the smaller (or larger) child. This swapping continues recursively down the tree until the heap property is restored.

Example: Imagine a min heap with the values [8, 10, 12, 15]. You've just inserted a new value, 5, which has disrupted the order. Starting from the position of the inserted value, heapify would compare 5 with its children (8 and 10). Since 5 is smaller, it remains in place. However, heapify then moves to the next level (comparing 5 with 12 and 15) and swaps 5 with 12 to satisfy the heap property. Now, the heap becomes [5, 8, 12, 15], with the structure restored.

4. Heap Sort

Heap Sort in Binary Heap Operations is a sorting algorithm that capitalizes on the unique structure of a heap. It involves transforming an unsorted array into a heap, then repeatedly extracting the smallest (or largest) element from the heap and placing it in the sorted portion of the array.

Heap Sort begins by creating a heap from the given array. This involves rearranging the array's elements so that they satisfy the heap property. Once the heap is built, the smallest value (in a min-heap) or the largest value (in a max heap) is extracted from the root and placed in the sorted portion of the array. The root is then replaced with the last element of the heap, and heapify is performed to restore the heap property. This process is repeated until the entire array becomes sorted.

Example: Consider an array [12, 5, 8, 15, 10]. Heap Sort would first transform this into a min-heap: [5, 10, 8, 15, 12]. Then, it would extract the minimum (5) and place it in the sorted portion. The heap would become [8, 10, 12, 15], and another extraction would result in [8, 10, 12], with 15 in the sorted portion. Repeating this process, the sorted array [5, 8, 10, 12, 15] is achieved.

Implementation of Binary Heap in Go

Implementing a Binary Heap in Go involves creating a data structure that maintains the hierarchical order of elements while allowing efficient access to the highest (or lowest) priority value.

  1. Structure of the MinHeap: To implement a Binary Heap, we create a structure that holds an array to represent the heap. This array stores the elements of the heap in a way that maintains the parent-child relationships.
     
  2. Initializing a MinHeap: To start, we create a function to initialize an empty MinHeap. This function initializes the array to hold elements and returns a MinHeap instance.
     
  3. Inserting Elements into the MinHeap: We define a method to insert elements into the MinHeap. When a new element is inserted, it's added to the end of the array. Then, we perform a process called "heapify-up" to ensure the heap property is satisfied. We compare the inserted value with its parent and swap them if necessary, repeating this process until the value finds its appropriate position.
     
  4. Extracting Minimum Element: We create a method to extract the minimum element from the MinHeap. The minimum element is always at the root of the heap. After extraction, we replace the root with the last element in the array and perform "heapify-down" to maintain the heap property. We compare the root with its children and swap with the smaller child if needed, continuing this process down the tree.
     
  5. Maintaining Heap Property: The "heapify-up" and "heapify-down" operations are essential for maintaining the heap property. "Heapify-up" ensures that the newly inserted value finds its proper place in the heap, and "heapify-down" restores the heap property after an extraction.
     

So, implementing a Binary Heap in Go involves creating a structured container for elements.

Example

Let’s discuss an example to implement Binary Heap in Golang.

Code

  • Go

Go

package main

import (
"fmt"
)

// Step 1: Define the MinHeap struct
type MinHeap struct {
nums []int
}

// Step 2: Initialize a new MinHeap
func NewMinHeap() *MinHeap {
return &MinHeap{
nums: []int{},
}
}

// Step 3: Implementing Insertion operation
func (x *MinHeap) Insert(value int) {
// Inserting value at the end
x.nums = append(x.nums, value)
i := len(x.nums) - 1
parent_node := (i - 1) / 2


// Perform heapify-up to maintain heap property
for i > 0 && x.nums[i] < x.nums[parent_node] {
x.nums[i], x.nums[parent_node] = x.nums[parent_node],x.nums[i]
i = parent_node
parent_node = (i - 1) / 2
}
}


// Step 4: Extract Min operation
func (h *MinHeap) FindMin() int {
if len(h.nums) == 0 {
return -1 // Heap is empty
}

mini := h.nums[0]
h.nums[0] = h.nums[len(h.nums)-1]
h.nums = h.nums[:len(h.nums)-1]

i := 0
child1Index := 2*i + 1
child2Index := 2*i + 2


// Perform heapify-down to maintain heap property
for child1Index < len(h.nums) {
smallestIndex := i


if h.nums[child1Index] < h.nums[smallestIndex] {
smallestIndex = child1Index
}


if child2Index < len(h.nums) && h.nums[child2Index] < h.nums[smallestIndex] {
smallestIndex = child2Index
}


if smallestIndex == i {
break
}


h.nums[i], h.nums[smallestIndex] = h.nums[smallestIndex], h.nums[i]
i = smallestIndex
child1Index = 2*i + 1
child2Index = 2*i + 2
}

return mini
}


func main() {
// Create a new MinHeap instance
obj := NewMinHeap()

// Insert values into the heap
obj.Insert(10)
obj.Insert(5)
obj.Insert(25)
obj.Insert(8)
obj.Insert(40)
obj.Insert(35)
obj.Insert(15)
obj.Insert(80)


// Extract and print the minimum values
fmt.Println("Extracted Min:", obj.FindMin())
fmt.Println("Extracted Min:", obj.FindMin())
}

 

Output

output


Explanation

This Go code demonstrates the implementation of a MinHeap, which is a specialized binary heap where parent nodes have smaller values than their child nodes. The code showcases how to insert elements into the heap and extract the minimum value efficiently.

Structure and Initialization 

The code begins by defining the MinHeap struct, which contains an array named nums to hold heap elements. The NewMinHeap() function initializes a new MinHeap instance with an empty array.

Insertion Operation

The Insert() method adds a value to the MinHeap while ensuring that the heap property is maintained. The value is initially added to the end of the array. Then, a process called "heapify-up" compares the value with its parent and swaps them if needed. This process continues up the tree until the heap property is satisfied.

Extract Min Operation

The FindMin() method retrieves and removes the minimum value from the MinHeap. The minimum value is at the root of the heap. After extraction, the last element replaces the root, and "heapify-down" is performed to restore the heap property. The value is compared with its children, and swapping ensures that the heap remains balanced.

Main Function

In the main() function, a MinHeap instance called obj is created. Values are inserted into the heap using the Insert() method. Then, the FindMin() method is used to extract and print the minimum values.

Overall, this code demonstrates how to use a MinHeap to efficiently manage data with priority. It showcases the insertion of values while maintaining the heap property and the extraction of the minimum value while preserving the heap's structure.

Time Complexity Analysis

Time Complexity Analysis in the context of a Golang program to represent a Binary Heap refers to the evaluation of how the execution time of the program grows in relation to the size of the input data (number of elements in the heap). It helps us understand how efficient our program is at handling larger amounts of data and provides insights into the performance characteristics of operations like insertion, extraction, and heapify.

Insertion Time Complexity: Inserting an element into a MinHeap involves two main steps. First, the element is added to the end of the heap, which takes constant time (O(1)) since arrays have fast append operations. 

However, after insertion, there's a process called "heapify-up" that ensures the heap property is maintained. In the worst case, this process might require traversing the height of the heap, which is logarithmic (O(log n)). So, the overall time complexity of insertion is O(log n).
 

Extraction Time Complexity: Extracting the minimum element from a MinHeap includes two significant steps. The first step is removing the root element, which takes constant time (O(1)). 

However, after removal, the last element is moved to the root position, and "heapify-down" is performed to restore the heap property. In the worst case, "heapify-down" might also traverse the height of the heap (O(log n)). Therefore, the overall time complexity of extraction is O(log n).
 

Heapify Time Complexity: Heapify is performed during insertion and extraction operations to maintain the heap property. "Heapify-up" and "heapify-down" both involve traversing the height of the heap, which is logarithmic (O(log n)). Since these operations are used as part of insertion and extraction, they contribute to the time complexity of those operations, as mentioned above.

So, inserting or extracting an element from a MinHeap takes more time as the heap grows larger, but the growth is not directly proportional. Instead, the time taken grows logarithmically with the number of elements. This efficient behaviour makes MinHeaps suitable for various applications where quick access to prioritized elements is required.

Use Cases and Applications

Let’s discuss some use cases and applications of Binary Heap.

Priority Queues

A Priority Queue is a data structure that allows efficient access to the highest-priority element. Binary Heaps, specifically Min Heaps, are excellent for implementing priority queues. Each element's priority is associated with its value, and the element with the highest priority (smallest value in a Min Heap) can be quickly extracted. Priority queues find applications in task scheduling, operating system scheduling algorithms, and network packet management.

Dijkstra's Shortest Path Algorithm

Dijkstra's algorithm is used to find the shortest path between nodes in a graph. Binary Heaps, particularly Min Heaps, play a crucial role in efficiently selecting the next node to explore. The algorithm involves choosing the node with the smallest distance value. The Min Heap ensures that this node can be quickly extracted, making Dijkstra's algorithm performant for finding shortest paths in networks, maps, or any situation involving distance calculations.

Huffman Coding

Huffman coding is a technique used in data compression, where characters are encoded with variable-length codes. Characters with higher frequencies get shorter codes, saving space. Binary Heaps, typically implemented as Min Heaps, are employed to construct Huffman trees efficiently. The process involves combining the two lowest-frequency nodes into a single node, and Min Heaps allow quick access to these low-frequency nodes. Huffman coding is used in data compression algorithms like JPEG and MP3.


So, Binary Heaps find applications in scenarios where priorities need to be managed efficiently, such as scheduling tasks in computing, finding the shortest routes, and optimizing data compression techniques. They help make these processes faster and more organized.

Frequently Asked Questions

Can I use a Binary Heap to sort an array? 

Yes, Binary Heaps can be used for a sorting technique called Heap Sort. It involves building a heap from the unsorted array and then repeatedly extracting the minimum (or maximum) element to create a sorted array. Heap Sort has a time complexity of O(n log n) and can be efficient for larger datasets.

Can I implement both Min Heap and Max Heap in the same program? 

Absolutely, you can implement both Min Heap and Max Heap in the same program. You would use the same structure but modify the comparison logic when inserting and extracting elements to either maintain the minimum or maximum heap property.

When should I use a Min Heap versus a Max Heap?

Use a Min Heap when you need quick access to the smallest element, like in priority queues or Dijkstra's algorithm. Use a Max Heap when you need quick access to the largest element, such as job scheduling or Huffman coding.

Conclusion

A Golang program to represent a Binary Heap offers a structured way to manage data with priority efficiently. Through insertion, extraction, and heapify operations, it ensures that elements are organized according to their importance, whether it's the smallest or largest value. Binary Heaps find applications in various domains, from optimizing task scheduling and shortest path algorithms to data compression techniques like Huffman coding. By understanding the basics of Binary Heaps and their practical uses, developers can make informed choices to enhance their programs' performance when dealing with prioritized data.

You can read these articles to learn more.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problemsinterview experiences, and interview bundles.

We wish you Good Luck! 

Happy Learning!

Live masterclass