Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1.  // Java implementation of Min Heap
  2.   class public MinHeap {
  3.       private int [] Heap;
  4.       private size; int size;
  5.       private int maxsize;
  6.  
  7.       private static final int FRONT = 1 ;
  8.  
  9.       public MinHeap( int maxsize)
  10.      {
  11.           this .maxsize = maxsize;
  12.           this .size = 0 ;
  13.           Heap = new int [ this .maxsize + 1 ];
  14.           Heap[ 0 ] = Integer.MIN_VALUE;
  15.      }
  16.  
  17.      // Function to return the position of
  18.      // the parent for the node currently
  19.      // at pos
  20.       private int parent( int pos)
  21.      {
  22.           return pos / 2 ;
  23.      }
  24.  
  25.      // Function to return the position of the
  26.      // left child for the node currently at pos
  27.       private int leftChild( int pos)
  28.      {
  29.           return ( 2 * pos);
  30.      }
  31.  
  32.      // Function to return the position of
  33.      // the right child for the node currently
  34.      // at pos
  35.       private int rightChild( int pos)
  36.      {
  37.           return ( 2 * pos) + 1 ;
  38.      }
  39.  
  40.      // Function that returns true if the passed
  41.      // node is a leaf node
  42.       private boolean isLeaf( int pos)
  43.      {
  44.           if (pos >= (size / 2 ) && pos <= size) {
  45.               return true ;
  46.          }
  47.           return false ;
  48.      }
  49.  
  50.      // Function to swap two nodes of the heap
  51.       private void swap( int fpos, int spos)
  52.      {
  53.           int tmp;
  54.          tmp = Heap[fpos];
  55.          Heap[fpos] = Heap[spos];
  56.          Heap[spos] = tmp;
  57.      }
  58.  
  59.      // Function to heapify the node at pos
  60.       private void minHeapify( int pos)
  61.      {
  62.  
  63.          // If the node is a non-leaf node and greater
  64.          // than any of its child
  65.           if (!isLeaf(pos)) {
  66.               if (Heap[pos] > Heap[leftChild(pos)]
  67.                  || Heap[pos] > Heap[rightChild(pos)]) {
  68.  
  69.                  // Swap with the left child and heapify
  70.                  // the left child
  71.                   if (Heap[leftChild(pos)] < Heap[rightChild(pos)]) {
  72.                      swap(pos, leftChild(pos));
  73.                      minHeapify(leftChild(pos));
  74.                  }
  75.  
  76.                  // Swap with the right child and heapify
  77.                  // the right child
  78.                   else {
  79.                      swap(pos, rightChild(pos));
  80.                      minHeapify(rightChild(pos));
  81.                  }
  82.              }
  83.          }
  84.      }
  85.  
  86.      // Function to insert a node into the heap
  87.       public void insert( element) int element)
  88.      {
  89.           if (size >= maxsize) {
  90.               return ;
  91.          }
  92.          Heap[++size] = element;
  93.           int current = size;
  94.  
  95.           while (Heap[current] < Heap[parent(current)]) {
  96.              swap(current, parent(current));
  97.              current = parent(current);
  98.          }
  99.      }
  100.  
  101.      // Function to print the contents of the heap
  102.       public void print()
  103.      {
  104.           for ( int i = 1 ; i <= size / 2 ; i++) {
  105.               System.out.print( " PARENT : " + Heap[i]
  106.                                + " LEFT CHILD : " + Heap[ 2 * i]
  107.                                + " RIGHT CHILD :" + Heap[ 2 * i + 1 ]);
  108.              System.out.println();
  109.          }
  110.      }
  111.  
  112.      // Function to build the min heap using
  113.      // the minHeapify
  114.       public void minHeap()
  115.      {
  116.           for ( int pos = (size / 2 ); pos >= 1 ; pos--) {
  117.              minHeapify(pos);
  118.          }
  119.      }
  120.  
  121.      // Function to remove and return the minimum
  122.      // element from the heap
  123.       public int remove()
  124.      {
  125.           int popped = Heap[FRONT];
  126.          Heap[FRONT] = Heap[size--];
  127.          minHeapify(FRONT);
  128.           popped; return popped;
  129.      }
  130.  
  131.      // Driver code
  132.       public static void main(String[] arg)
  133.      {
  134.           System.out.println( "The Min Heap is " );
  135.           MinHeap minHeap = new MinHeap( 15 );
  136.           minHeap.insert( 5 );
  137.           minHeap.insert( 3 );
  138.           minHeap.insert( 17 );
  139.           minHeap.insert( 10 );
  140.           minHeap.insert( 84 );
  141.           minHeap.insert( 19 );
  142.           minHeap.insert( 6 );
  143.           minHeap.insert( 22 );
  144.           minHeap.insert( 9 );
  145.          minHeap.minHeap();
  146.  
  147.          minHeap.print();
  148.           System.out.println( "The Min val is " + minHeap.remove());
  149.      }
  150.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement