Advertisement
Aldin-SXR

BinaryHeap.java

May 5th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package ds.binary.heap;
  2.  
  3. public class BinaryHeap<Item extends Comparable<Item>> {
  4.    
  5.     @SuppressWarnings({ "unchecked" })
  6.     public Item[] pq = (Item[]) new Comparable[2];
  7.     public int length = 0;
  8.    
  9.     /* Insert a new element into the priority queue */
  10.     public void insert(Item data) {
  11.         if (pq.length == length + 1) {          // 1
  12.             resize(2 * pq.length);              // 1
  13.         }
  14.         pq[++length] = data;                    // 2
  15.         swim(length);                           // 3
  16.     }
  17.    
  18.     /* Node promotion:
  19.      * swim up a node to its correct position */
  20.     private void swim(int k) {
  21.         while (k > 1 && less(k/2, k)) {     // 1
  22.             swap(k, k/2);                   // 2
  23.             k = k/2;                        // 3
  24.         }
  25.     }
  26.    
  27.     /* Remove the maximum (max. priority) item  */
  28.     public Item delMax() {
  29.         Item max = pq[1];                               // 1
  30.         swap(1, length--);                              // 2
  31.         pq[length + 1] = null;                          // 3
  32.          
  33.         if (length > 0 && length == pq.length / 4) {    // 4
  34.             resize(pq.length / 2);                      // 4
  35.         }
  36.        
  37.         sink(1);                                        // 5
  38.         return max;                                     // 6
  39.     }
  40.    
  41.     /* Node demotion:
  42.      * sink down a node to its correct position */
  43.     private void sink(int k) {
  44.         while (2*k <= length) {                     // 1
  45.             int j = 2 * k;                          // 2
  46.             if (j < length && less(j, j + 1)) {     // 2
  47.                 j++;                                // 2
  48.             }
  49.            
  50.             if (!less(k, j)) {                      // 3
  51.                 break;                              // 3
  52.             }
  53.            
  54.             swap(k, j);                             // 4
  55.             k = j;                                  // 5
  56.         }
  57.     }
  58.    
  59.     /* Check if the priority queue is empty */
  60.     public boolean isEmpty() {
  61.         return length == 0;
  62.     }
  63.    
  64.     /* Return the current size of the priority queue */
  65.     public int size() {
  66.         return length;
  67.     }
  68.    
  69.     /* Create a new internal array with a given capacity */
  70.     @SuppressWarnings("unchecked")
  71.     private void resize(int capacity) {
  72.         Item[] copy = (Item[]) new Comparable[capacity];        // 1
  73.         for (int i = 1; i <= length; i++) {                     // 2
  74.             copy[i] = pq[i];                                    // 2
  75.         }                                              
  76.         pq = copy;                                              // 3
  77.     }
  78.    
  79.     /* Check which of the two elements is smaller */
  80.     private boolean less(int a, int b) {
  81.         return pq[a].compareTo(pq[b]) < 0;
  82.     }
  83.    
  84.     /* Swap the array elements at provided indexes */
  85.     private void swap(int a, int b) {
  86.         Item temp = pq[a];
  87.         pq[a] = pq[b];
  88.         pq[b] = temp;
  89.     }
  90.    
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement