Java Program to Implement Skew Heap

This is a Java Program to implement Skew Heap. A skew heap (or self-adjusting heap) is a heap data structure implemented as a binary tree. Skew heaps are advantageous because of their ability to merge more quickly than binary heaps. In contrast with binary heaps, there are no structural constraints, so there is no guarantee that the height of the tree is logarithmic.

Here is the source code of the Java program to implement Skew 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 Skew Heap
  3.  **/
  4.  
  5. import java.util.*;
  6.  
  7. /** Class SkewNode **/
  8. class SkewNode
  9. {
  10.     int element;
  11.     SkewNode left, right;
  12.  
  13.     /** Constructor **/
  14.     public SkewNode(int val)
  15.     {
  16.         this.element = val;
  17.         this.left = null;
  18.         this.right = null;
  19.     }
  20. }
  21.  
  22. /** Class SkewHeap **/
  23. class SkewHeap
  24. {
  25.     private SkewNode root;
  26.     private int size;
  27.  
  28.     /** Constructor **/
  29.     public SkewHeap()
  30.     {
  31.         root = null;
  32.         size = 0;    
  33.     }
  34.  
  35.     /** Check if heap is empty **/
  36.     public boolean isEmpty()
  37.     {
  38.         return root == null;
  39.     }
  40.  
  41.     /** clear heap **/
  42.     public void clear()
  43.     {
  44.         root = null;
  45.         size = 0;
  46.     }
  47.  
  48.     /** Function to get size **/
  49.     public int getSize()
  50.     {
  51.         return size;
  52.     }
  53.  
  54.     /** Function to insert **/
  55.     public void insert(int val)
  56.     {
  57.         root = merge(root, new SkewNode(val)); 
  58.         size++ ;
  59.     }
  60.  
  61.     /** Function to remove element **/
  62.     public void remove() 
  63.     {
  64.         if (root == null)
  65.             throw new NoSuchElementException("Element not found");
  66.         root = merge(root.left, root.right);
  67.         size--;
  68.     }
  69.  
  70.     /** Function merge **/
  71.     private SkewNode merge(SkewNode x, SkewNode y) 
  72.     {
  73.         if (x == null) 
  74.             return y;
  75.         if (y == null) 
  76.             return x;
  77.  
  78.         if (x.element < y.element) 
  79.         {
  80.             SkewNode temp = x.left;
  81.             x.left = merge(x.right, y);
  82.             x.right = temp;
  83.             return x;
  84.         }
  85.         else 
  86.         {
  87.             SkewNode temp = y.right;
  88.             y.right = merge(y.left, x);
  89.             y.left = temp;
  90.             return y;
  91.         }
  92.     }
  93.  
  94.     /** Function to display heap **/
  95.     public void displayHeap()
  96.     {
  97.         System.out.print("\nHeap : ");
  98.         displayHeap(root);
  99.         System.out.println("\n");
  100.     }
  101.     private void displayHeap(SkewNode r)
  102.     {
  103.         if (r != null)
  104.         {
  105.             displayHeap(r.left);
  106.             System.out.print(r.element +" ");
  107.             displayHeap(r.right);
  108.         }
  109.     }    
  110. }
  111.  
  112. /** Class SkewHeapTest **/
  113. public class SkewHeapTest
  114. {
  115.     public static void main(String[] args)
  116.     {
  117.         Scanner scan = new Scanner(System.in);
  118.         System.out.println("Skew Heap Test\n\n");
  119.  
  120.         /** Make object of SkewHeap **/
  121.         SkewHeap sh = new SkewHeap( );
  122.  
  123.         char ch;
  124.         /**  Perform SkewHeap operations  **/
  125.         do    
  126.         {
  127.             System.out.println("\nSkewHeap Operations\n");
  128.             System.out.println("1. insert ");
  129.             System.out.println("2. delete ");
  130.             System.out.println("3. size");            
  131.             System.out.println("4. check empty");
  132.             System.out.println("5. clear");
  133.  
  134.             int choice = scan.nextInt();            
  135.             switch (choice)
  136.             {
  137.             case 1 : 
  138.                 System.out.println("Enter integer element to insert");
  139.                 sh.insert( scan.nextInt() ); 
  140.                 break;                          
  141.             case 2 :                 
  142.                 sh.remove();   
  143.                 break;                         
  144.             case 3 : 
  145.                 System.out.println("Size = "+ sh.getSize());
  146.                 break;                                   
  147.             case 4 : 
  148.                 System.out.println("Empty status = "+ sh.isEmpty());
  149.                 break; 
  150.             case 5 : 
  151.                 sh.clear();
  152.                 System.out.println("Heap Cleared\n");
  153.                 break;         
  154.             default : 
  155.                 System.out.println("Wrong Entry \n ");
  156.                 break;   
  157.             }
  158.             /** Display heap **/
  159.             sh.displayHeap();   
  160.  
  161.             System.out.println("\nDo you want to continue (Type y or n) \n");
  162.             ch = scan.next().charAt(0);                        
  163.         } while (ch == 'Y'|| ch == 'y');  
  164.     }
  165. }

Skew Heap Test
 
 
 
SkewHeap Operations
 
1. insert
2. delete
3. size
4. check empty
5. clear
4
Empty status = true
 
Heap :
 
 
Do you want to continue (Type y or n)
 
y
 
SkewHeap Operations
 
1. insert
2. delete
3. size
4. check empty
5. clear
1
Enter integer element to insert
1
 
Heap : 1
 
 
Do you want to continue (Type y or n)
 
y
 
SkewHeap Operations
 
1. insert
2. delete
3. size
4. check empty
5. clear
1
Enter integer element to insert
5
 
Heap : 5 1
 
 
Do you want to continue (Type y or n)
 
y
 
SkewHeap Operations
 
1. insert
2. delete
3. size
4. check empty
5. clear
1
Enter integer element to insert
7
 
Heap : 7 1 5
 
 
Do you want to continue (Type y or n)
 
y
 
SkewHeap Operations
 
1. insert
2. delete
3. size
4. check empty
5. clear
1
Enter integer element to insert
14
 
Heap : 14 5 1 7
 
 
Do you want to continue (Type y or n)
 
y
 
SkewHeap Operations
 
1. insert
2. delete
3. size
4. check empty
5. clear
1
Enter integer element to insert
70
 
Heap : 70 7 1 14 5
 
 
Do you want to continue (Type y or n)
 
y
 
SkewHeap Operations
 
1. insert
2. delete
3. size
4. check empty
5. clear
1
Enter integer element to insert
91
 
Heap : 91 5 14 1 70 7
 
 
Do you want to continue (Type y or n)
 
y
 
SkewHeap Operations
 
1. insert
2. delete
3. size
4. check empty
5. clear
2
 
Heap : 7 14 70 5 91
 
 
Do you want to continue (Type y or n)
 
y
 
SkewHeap Operations
 
1. insert
2. delete
3. size
4. check empty
5. clear
2
 
Heap : 91 70 14 7
 
 
Do you want to continue (Type y or n)
 
y
 
SkewHeap Operations
 
1. insert
2. delete
3. size
4. check empty
5. clear
2
 
Heap : 91 70 14
 
 
Do you want to continue (Type y or n)
 
y
 
SkewHeap Operations
 
1. insert
2. delete
3. size
4. check empty
5. clear
2
 
Heap : 91 70
 
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.