Open In App

C Program to Implement Binomial Heap

Last Updated : 09 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Binomial Heap is a specific type of heap data structure that finds its roots in the Binomial Tree. It’s a collection of binomial trees, each adhering to the min-heap property, where the parent node’s key is less than or equal to its children’s keys. This structure is particularly efficient for implementing priority queues due to its ability to perform union or merge operations faster.

In this article, we will learn the implementation of a Binomial Heap in C, understand the basic operations it supports, and its applications.

What is a Binomial Tree?

A Binomial Tree Bk​ of order k has several distinctive properties:

  • Recursive Definition:
    • A Binomial Tree of order 0, B0​, consists of a single node.
    • A Binomial Tree of order k is formed by linking two Binomial Trees of order k−1, where one tree becomes the leftmost child of the other.
  • Number of Nodes: It contains 2^k nodes.
  • Height: The height of the tree is k.
  • Children: The root node has exactly k children, each being the root of a Binomial Tree of orders 0 to k−1.

Implementation of Binomial Heap in C

A binomial heap consists of a collection of binomial trees, each adhering to certain properties:

  • The key of a parent node is less than or equal to the keys of its children (min-heap property).
  • There can be at most one binomial tree of any degree within the heap.
  • The degree of a binomial tree corresponds to the number of children it has.

Representation of Binomial Heap in C

A Binomial Heap can be visually represented as a collection of Binomial Trees of different orders.

BinomialHeap
Binomial Heap

To represent this in C, we use a structure to denote the nodes of the binomial tree and manage the heap using pointers.

struct BinomialNode {
int key;
int degree;
struct BinomialNode* parent;
struct BinomialNode* child;
struct BinomialNode* sibling;
} BinomialNode;

Here,

  • key: Holds the value stored in the node.
  • degree: Indicates the number of children of the node.
  • parent: Pointer to the parent node.
  • child: Pointer to the first child node.
  • sibling: Pointer to the next sibling node.

Binomial Heap Operations Implementation in C

Following are some basic operations that can be performed on a binomial heap:

Operation

Description

Time Complexity

Space Complexity

Insert

Inserts a new element into the binomial heap

O(log(n))

O(1)

Extract Min

Removes and returns the minimum element

O(log(n))

O(log(n))

Merge

Combines two binomial heaps into one

O(log(n))

O(1)

Decrease Key

Decreases the key value of a given node

O(log(n))

O(1)

Delete Node

Deletes a given node from the binomial heap

O(log(n))

O(1)

Insert Implementation in C++

To insert a new element into the binomial heap:

  • Create a new binomial heap with the new node.
  • Merge the new binomial heap with the existing heap by merging them based on their degrees..
  • Ensure there is at most one tree of each degree by combining trees of the same degree to maintain Binomial Heap properties.

Extract Minimum Implementation in C

To extract the minimum element:

  • Traverse the root nodes to find the minimum key.
  • Remove this minimum node and mark its children for merging.
  • Merge the remaining trees and the children.
  • After merging, ensure there is at most one tree of each degree by combining trees of the same degree to maintain Binomial Heap properties.

Merge Implementation in C

To merge two binomial heaps:

  • Start combining the root lists of both heaps in increasing order of their degrees.
  • Compare the degrees of the roots and link the trees with smaller degrees first.
  • After combining, check for trees of the same degree, if two trees of the same degree are found, link them to form a single tree of the next higher degree.
  • Continue this process until no two trees of the same degree remain.

Decrease Key Implementation in C

To decrease the key of a node:

  • Decrease the node's key value and update the node’s key.
  • If the new key value violates the heap property (i.e., it is smaller than the parent's key), swap the node with its parent until the heap property is restored.
  • Continue swapping until the heap property is restored.

Delete Node Implementation in C

To delete a node:

  • First, decrease the node’s key to a value less than any other key in the heap (typically negative infinity).
  • Use the extract minimum operation to remove the node from the heap , this process will ensure that the node is removed while maintaining the heap properties.

Applications of Binomial Heap

Binomial heaps have various practical applications, including:

  • Binomial heap is efficient for operations like insertion, deletion, and finding the minimum element.
  • It is used in Dijkstra’s shortest path and Prim’s minimum spanning tree algorithms.
  • It helps manage queues of tasks with different priorities.
  • It assists in maintaining efficient routing tables.
  • It is also utilized in Huffman coding.

Related Articles

You can also explore the following articles to enhance your understanding of Binomial Heaps:


Next Article
Article Tags :

Similar Reads