Open In App

Fibonacci Heap – Insertion and Union

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Fibonacci Heap is a collection of trees with min-heap or max-heap property. In Fibonacci Heap, trees can have any shape even all trees can be single nodes (This is unlike Binomial Heap where every tree has to be a Binomial Tree). In this article, we will discuss Insertion and Union operation on Fibonacci Heap. 

Prerequisites: Fibonacci Heap (Introduction)

Insertion: To insert a node in a Fibonacci heap H, the following algorithm is followed: 

  1. Create a new node ‘x’.
  2. Check whether heap H is empty or not.
  3. If H is empty then: 
    • Make x as the only node in the root list.
    • Set H(min) pointer to x.
  4. Else: 
    • Insert x into root list and update H(min).

Example: 

insert a node in a Fibonacci heap

Union: Union of two Fibonacci heaps H1 and H2 can be accomplished as follows: 

  1. Join root lists of Fibonacci heaps H1 and H2 and make a single Fibonacci heap H.
  2. If H1(min) < H2(min) then: 
    • H(min) = H1(min).
  3. Else: 
    • H(min) = H2(min).

Example: 

Union of two Fibonacci heaps

Following is a program to demonstrate building and inserting in a Fibonacci heap:  

C++




// C++ program to demonstrate building
// and inserting in a Fibonacci heap
#include <cstdlib>
#include <iostream>
#include <malloc.h>
using namespace std;
 
struct node {
    node* parent;
    node* child;
    node* left;
    node* right;
    int key;
};
 
// Creating min pointer as "mini"
struct node* mini = NULL;
 
// Declare an integer for number of nodes in the heap
int no_of_nodes = 0;
 
// Function to insert a node in heap
void insertion(int val)
{
    struct node* new_node = (struct node*)malloc(sizeof(struct node));
    new_node->key = val;
    new_node->parent = NULL;
    new_node->child = NULL;
    new_node->left = new_node;
    new_node->right = new_node;
    if (mini != NULL) {
        (mini->left)->right = new_node;
        new_node->right = mini;
        new_node->left = mini->left;
        mini->left = new_node;
        if (new_node->key < mini->key)
            mini = new_node;
    }
    else {
        mini = new_node;
    }
}
 
// Function to display the heap
void display(struct node* mini)
{
    node* ptr = mini;
    if (ptr == NULL)
        cout << "The Heap is Empty" << endl;
 
    else {
        cout << "The root nodes of Heap are: " << endl;
        do {
            cout << ptr->key;
            ptr = ptr->right;
            if (ptr != mini) {
                cout << "-->";
            }
        } while (ptr != mini && ptr->right != NULL);
        cout << endl
             << "The heap has " << no_of_nodes << " nodes" << endl;
    }
}
// Function to find min node in the heap
void find_min(struct node* mini)
{
    cout << "min of heap is: " << mini->key << endl;
}
 
 
// Driver code
int main()
{
 
    no_of_nodes = 7;
    insertion(4);
    insertion(3);
    insertion(7);
    insertion(5);
    insertion(2);
    insertion(1);
    insertion(10);
 
    display(mini);
 
    find_min(mini);
 
    return 0;
}


Python3




# Python program to demonstrate building
# and inserting in a Fibonacci heap
 
 
class node:
    def __init__(self):
        self.parent = None  # Assign data
        self.child = None  # Initialize next as null
        self.left = None
        self.right = None
        self.key = -1
 
 
# Creating min pointer as "mini"
mini = node()
 
# Declare an integer for number of nodes in the heap
no_of_nodes = 0
 
# Function to insert a node in heap
 
 
def insertion(val):
    new_node = node()
    new_node.key = val
    new_node.parent = None
    new_node.child = None
    new_node.left = new_node
    new_node.right = new_node
 
    global mini
    if mini.key != -1:
        (mini.left).right = new_node
        new_node.right = mini
        new_node.left = mini.left
        mini.left = new_node
        if (new_node.key < mini.key):
            mini = new_node
    else:
        mini = new_node
 
# Function to display the heap
 
 
def display(mini):
    ptr = mini
    if (ptr == None):
        print("The Heap is Empty")
    else:
 
        print("The root nodes of Heap are: ")
        print(ptr.key, end="")
        ptr = ptr.right
        if(ptr != mini):
            print("-->", end="")
 
        while ptr != mini and ptr.right != None:
            print(ptr.key, end="")
            ptr = ptr.right
            if ptr != mini:
                print("-->", end="")
        print()
        print(f"The heap has {no_of_nodes} nodes")
 
# Function to find min node in the heap
 
 
def find_min(mini):
    print(f"min of heap is: {mini.key}")
 
 
# Driver code
no_of_nodes = 7
insertion(4)
insertion(3)
insertion(7)
insertion(5)
insertion(2)
insertion(1)
insertion(10)
 
display(mini)
 
find_min(mini)
 
# The code is contributed by Gautam goel (gautamgoel962)


C#




// C# program to demonstrate building
// and inserting in a Fibonacci heap
using System;
 
class FibonacciHeap
{
class Node
{
public Node parent;
public Node child;
public Node left;
public Node right;
public int key;
};
  // Creating min pointer as "mini"
static Node mini = null;
 
// Declare an integer for number of nodes in the heap
static int no_of_nodes = 0;
 
// Function to insert a node in heap
static void Insertion(int val)
{
    Node new_node = new Node();
    new_node.key = val;
    new_node.parent = null;
    new_node.child = null;
    new_node.left = new_node;
    new_node.right = new_node;
 
    if (mini != null)
    {
        (mini.left).right = new_node;
        new_node.right = mini;
        new_node.left = mini.left;
        mini.left = new_node;
 
        if (new_node.key < mini.key)
            mini = new_node;
    }
    else
    {
        mini = new_node;
    }
}
 
// Function to display the heap
static void Display(Node mini)
{
    Node ptr = mini;
    if (ptr == null)
        Console.WriteLine("The Heap is Empty");
 
    else
    {
        Console.WriteLine("The root nodes of Heap are: ");
        do
        {
            Console.Write(ptr.key);
            ptr = ptr.right;
            if (ptr != mini)
            {
                Console.Write("-->");
            }
        } while (ptr != mini && ptr.right != null);
        Console.WriteLine();
        Console.WriteLine("The heap has " + no_of_nodes + " nodes");
    }
}
 
// Function to find min node in the heap
static void FindMin(Node mini)
{
    Console.WriteLine("min of heap is: " + mini.key);
}
 
 
// Driver code
static void Main()
{
    no_of_nodes = 7;
    Insertion(4);
    Insertion(3);
    Insertion(7);
    Insertion(5);
    Insertion(2);
    Insertion(1);
    Insertion(10);
 
    Display(mini);
 
    FindMin(mini);
}
}


Javascript




// JavaScript program to demonstrate building
// and inserting in a Fibonacci heap
 
class Node {
constructor() {
this.parent = null; // Assign data
this.child = null; // Initialize next as null
this.left = null;
this.right = null;
this.key = -1;
}
}
 
// Creating min pointer as "mini"
let mini = new Node();
 
// Declare an integer for number of nodes in the heap
let no_of_nodes = 0;
 
// Function to insert a node in heap
function insertion(val) {
let new_node = new Node();
new_node.key = val;
new_node.parent = null;
new_node.child = null;
new_node.left = new_node;
new_node.right = new_node;
 
if (mini.key != -1) {
mini.left.right = new_node;
new_node.right = mini;
new_node.left = mini.left;
mini.left = new_node;
if (new_node.key < mini.key) {
mini = new_node;
}
} else {
mini = new_node;
}
}
 
// Function to display the heap
function display(mini) {
let ptr = mini;
if (ptr === null) {
document.write("The Heap is Empty");
} else {
document.write("The root nodes of Heap are: ");
document.write(ptr.key.toString());
ptr = ptr.right;
if (ptr !== mini) {
document.write("-->");
}
 
 
while (ptr !== mini && ptr.right !== null) {
  document.write(ptr.key.toString());
  ptr = ptr.right;
  if (ptr !== mini) {
    document.write("-->");
  }
}
document.write("<br>");
document.write(`The heap has ${no_of_nodes} nodes<br>`);
}
}
 
// Function to find min node in the heap
function find_min(mini) {
document.write(`min of heap is: ${mini.key}<br>`);
}
 
// Driver code
no_of_nodes = 7;
insertion(4);
insertion(3);
insertion(7);
insertion(5);
insertion(2);
insertion(1);
insertion(10);
 
display(mini);
 
find_min(mini);


Java




// Java program to demonstrate building and inserting in a
// Fibonacci heap
 
import java.io.*;
 
class Node {
    Node parent;
    Node child;
    Node left;
    Node right;
    int key;
}
 
class GFG {
 
    // Creating min pointer as "mini"
    static Node mini = null;
 
    // Declare an integer for number of nodes in the heap
    static int no_of_nodes = 0;
 
    // Function to insert a node in heap
    static void Insertion(int val)
    {
        Node new_node = new Node();
        new_node.key = val;
        new_node.parent = null;
        new_node.child = null;
        new_node.left = new_node;
        new_node.right = new_node;
 
        if (mini != null) {
            (mini.left).right = new_node;
            new_node.right = mini;
            new_node.left = mini.left;
            mini.left = new_node;
 
            if (new_node.key < mini.key)
                mini = new_node;
        }
        else {
            mini = new_node;
        }
    }
 
    // Function to display the heap
    static void Display(Node mini)
    {
        Node ptr = mini;
        if (ptr == null)
            System.out.println("The Heap is Empty");
 
        else {
            System.out.println(
                "The root nodes of Heap are: ");
            do {
                System.out.print(ptr.key);
                ptr = ptr.right;
                if (ptr != mini) {
                    System.out.print("-->");
                }
            } while (ptr != mini && ptr.right != null);
            System.out.println();
            System.out.println("The heap has " + no_of_nodes
                               + " nodes");
        }
    }
 
    // Function to find min node in the heap
    static void FindMin(Node mini)
    {
        System.out.println("min of heap is: " + mini.key);
    }
 
    public static void main(String[] args)
    {
        no_of_nodes = 7;
        Insertion(4);
        Insertion(3);
        Insertion(7);
        Insertion(5);
        Insertion(2);
        Insertion(1);
        Insertion(10);
 
        Display(mini);
 
        FindMin(mini);
    }
}
 
// This code is contributed by karthik.


Output

The root nodes of Heap are: 
1-->2-->3-->4-->7-->5-->10
The heap has 7 nodes
min of heap is: 1


Last Updated : 03 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads