Advertisement
Guest User

Heap

a guest
Jun 26th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. public class Heap <T>{
  2.  
  3.     private int capacity = 0;
  4.     private T[] buffer;
  5.     private int size;
  6.     private Comparator<T> comparator;
  7.  
  8.     public Heap(int capacity, Comparator<T> comparator) {
  9.         this.comparator = comparator;
  10.         this.capacity = capacity;
  11.         this.buffer = (T[])new Object[this.capacity + 1]; // spare buffer[0]
  12.     }
  13.  
  14.     public Heap(T[] buffer, Comparator<T> comparator) {
  15.         this.comparator = comparator;
  16.         this.capacity = buffer.length * 2; // spare buffer[0]
  17.         this.size = buffer.length;
  18.         this.buffer = (T[])new Object[this.capacity + 1];
  19.         for(int i = 0; i < buffer.length; i ++)
  20.             this.buffer[i+1] = buffer[i]; // buffer[0] is not used
  21.  
  22.         int nonLeafNodeIndex = this.size / 2; // the last
  23.         for(int i = nonLeafNodeIndex; i >= 1; i --)
  24.             this.swim(i);
  25.     }
  26.  
  27.     public void insert(T value) {
  28.         if(this.size >= this.capacity) { // resize
  29.             this.capacity *= 2;
  30.             T[] buffer = (T[])new Object[this.capacity + 1];
  31.             for(int i = 1; i <= this.size; i ++) {
  32.                 buffer[i] = this.buffer[i];
  33.             }
  34.             this.buffer = buffer;
  35.         }
  36.         this.buffer[++size] = value;
  37.         this.swim(size/2);
  38.     }
  39.  
  40.     public T remove() {
  41.         T top = this.buffer[1];
  42.         this.buffer[1] = this.buffer[this.size--];
  43.         this.sink(1);
  44.         return top;
  45.     }
  46.  
  47.     public void sink(int index) {
  48.         while(index <= this.size/2) { // loop until last non leaf child
  49.             // Compare the left and right child
  50.             // to see which one should be exchanged with current node
  51.             int exchangeChildIndex = 2 * index;
  52.             T exchangeChild = this.buffer[2 * index];
  53.             if(2 * index + 1 <= this.size) {
  54.                 if(this.comparator.compare(exchangeChild, this.buffer[2 * index + 1]) < 0) {
  55.                     exchangeChildIndex = 2 * index + 1;
  56.                     exchangeChild = this.buffer[exchangeChildIndex];
  57.                 }
  58.             }
  59.  
  60.             if(this.comparator.compare(this.buffer[index], exchangeChild) >= 0)  // heap condition met
  61.                 break;
  62.             else {
  63.                 this.swap(this.buffer, index, exchangeChildIndex);
  64.                 index = exchangeChildIndex;
  65.             }
  66.         }
  67.     }
  68.  
  69.     public void swim(int index) {
  70.         while(index >= 1 && 2 * index <= this.size) { // loop until first node
  71.             // Compare the left and right child
  72.             // to see which one should be exchanged with current node
  73.             int exchangeChildIndex = 2 * index;
  74.             T exchangeChild = this.buffer[2 * index];
  75.             if(2 * index + 1 <= this.size) {
  76.                 if(this.comparator.compare(exchangeChild, this.buffer[2 * index + 1]) < 0) {
  77.                     exchangeChildIndex = 2 * index + 1;
  78.                     exchangeChild = this.buffer[exchangeChildIndex];
  79.                 }
  80.             }
  81.  
  82.             if(this.comparator.compare(this.buffer[index], exchangeChild) >= 0)  // heap condition met
  83.                 break;
  84.             else {
  85.                 this.swap(this.buffer, index, exchangeChildIndex);
  86.                 index /= 2;
  87.             }
  88.         }
  89.     }
  90.  
  91.     private void swap(T[] array, int i, int j) {
  92.         T tmp = array[i];
  93.         array[i] = array[j];
  94.         array[j] = tmp;
  95.     }
  96.  
  97.     public int size() {
  98.         return this.size;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement