Java Program to Implement Fibonacci Heap

«
»
This is a Java Program to implement Fibonacci Heap. Fibonacci heap is a heap data structure consisting of a collection of trees. It has a better amortized running time than a binomial heap. The name of Fibonacci heap comes from Fibonacci numbers which are used in the running time analysis. Using Fibonacci heaps for priority queues improves the asymptotic running time of important algorithms, such as Dijkstra’s algorithm for computing the shortest path between two nodes in a graph.

Here is the source code of the Java program to implement Fibonacci Heap. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  *  Java Program to Implement FibonacciHeap
  3.  **/
  4.  
  5. import java.util.*;
  6.  
  7. /* Fibonacci Heap Node **/
  8. class FibonacciHeapNode
  9. {
  10.     FibonacciHeapNode child, left, right, parent;    
  11.     int element;
  12.  
  13.     /** Constructor **/
  14.     public FibonacciHeapNode(int element)
  15.     {
  16.         this.right = this;
  17.         this.left = this;
  18.         this.element = element;
  19.     }    
  20. }
  21.  
  22. /** Class FibonacciHeap **/
  23. class FibonacciHeap
  24. {
  25.     private FibonacciHeapNode root;
  26.     private int count;    
  27.  
  28.     /** Constructor **/
  29.     public FibonacciHeap()
  30.     {
  31.         root = null;
  32.         count = 0;
  33.     }
  34.  
  35.     /** Check if heap is empty **/
  36.     public boolean isEmpty()
  37.     {
  38.         return root == null;
  39.     }
  40.  
  41.     /** Make heap empty **/ 
  42.     public void clear()
  43.     {
  44.         root = null;
  45.         count = 0;
  46.     }
  47.  
  48.     /** Function to insert **/
  49.     public void insert(int element)
  50.     {
  51.         FibonacciHeapNode node = new FibonacciHeapNode(element);
  52.         node.element = element;
  53.  
  54.         if (root != null) 
  55.         {
  56.             node.left = root;
  57.             node.right = root.right;
  58.             root.right = node;
  59.             node.right.left = node;
  60.             if (element < root.element) 
  61.                 root = node;            
  62.         }
  63.         else 
  64.             root = node;
  65.         count++;
  66.     }   
  67.  
  68.     /** function to display **/
  69.     public void display()
  70.     {
  71.         System.out.print("\nHeap = ");
  72.         FibonacciHeapNode ptr = root;
  73.         if (ptr == null)
  74.         {
  75.             System.out.print("Empty\n");
  76.             return;
  77.         }        
  78.         do
  79.         {
  80.             System.out.print(ptr.element +" ");
  81.             ptr = ptr.right;
  82.         } while (ptr != root && ptr.right != null);
  83.         System.out.println();
  84.     } 
  85. }    
  86.  
  87. /** Class FibonacciHeapTest **/
  88. public class FibonacciHeapTest
  89. {
  90.     public static void main(String[] args)
  91.     {
  92.         Scanner scan = new Scanner(System.in);
  93.         System.out.println("FibonacciHeap Test\n\n");        
  94.         FibonacciHeap fh = new FibonacciHeap();
  95.  
  96.         char ch;
  97.         /**  Perform FibonacciHeap operations  **/
  98.         do    
  99.         {
  100.             System.out.println("\nFibonacciHeap Operations\n");
  101.             System.out.println("1. insert element ");
  102.             System.out.println("2. check empty");            
  103.             System.out.println("3. clear");
  104.  
  105.             int choice = scan.nextInt();            
  106.             switch (choice)
  107.             {
  108.             case 1 : 
  109.                 System.out.println("Enter element");
  110.                 fh.insert( scan.nextInt() );                                    
  111.                 break;                          
  112.             case 2 : 
  113.                 System.out.println("Empty status = "+ fh.isEmpty());
  114.                 break;   
  115.             case 3 : 
  116.                 fh.clear();
  117.                 break;           
  118.             default : 
  119.                 System.out.println("Wrong Entry \n ");
  120.                 break;   
  121.             }           
  122.             fh.display();
  123.  
  124.             System.out.println("\nDo you want to continue (Type y or n) \n");
  125.             ch = scan.next().charAt(0);                        
  126.         } while (ch == 'Y'|| ch == 'y');  
  127.     }
  128. }

advertisement
FibonacciHeap Test
 
 
 
FibonacciHeap Operations
 
1. insert element
2. check empty
3. clear
1
Enter element
24
 
Heap = 24
 
Do you want to continue (Type y or n)
 
y
 
FibonacciHeap Operations
 
1. insert element
2. check empty
3. clear
1
Enter element
6
 
Heap = 6 24
 
Do you want to continue (Type y or n)
 
y
 
FibonacciHeap Operations
 
1. insert element
2. check empty
3. clear
1
Enter element
28
 
Heap = 6 28 24
 
Do you want to continue (Type y or n)
 
y
 
FibonacciHeap Operations
 
1. insert element
2. check empty
3. clear
1
Enter element
14
 
Heap = 6 14 28 24
 
Do you want to continue (Type y or n)
 
y
 
FibonacciHeap Operations
 
1. insert element
2. check empty
3. clear
1
Enter element
63
 
Heap = 6 63 14 28 24
 
Do you want to continue (Type y or n)
 
y
 
FibonacciHeap Operations
 
1. insert element
2. check empty
3. clear
2
Empty status = false
 
Heap = 6 63 14 28 24
 
Do you want to continue (Type y or n)
 
y
 
FibonacciHeap Operations
 
1. insert element
2. check empty
3. clear
3
 
Heap = Empty
 
Do you want to continue (Type y or n)
 
y
 
FibonacciHeap Operations
 
1. insert element
2. check empty
3. clear
2
Empty status = true
 
Heap = Empty
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.