Java Data Structures Heap

Description

Java Data Structures Heap


class Node {
   private int iData; // data item (key)

   public Node(int key) {
      iData = key;//from www.j a  va2 s. co  m
   }

   public int getKey() {
      return iData;
   }

   public void setKey(int id) {
      iData = id;
   }
}

class Heap {
   private Node[] heapArray;
   private int maxSize;
   private int currentSize;

   public Heap(int mx) {
      maxSize = mx;
      currentSize = 0;
      heapArray = new Node[maxSize];
   }

   public boolean isEmpty() {
      return currentSize == 0;
   }

   public boolean insert(int key) {
      if (currentSize == maxSize)
         return false;
      Node newNode = new Node(key);
      heapArray[currentSize] = newNode;
      trickleUp(currentSize++);
      return true;
   } // end insert()

   public void trickleUp(int index) {
      int parent = (index - 1) / 2;
      Node bottom = heapArray[index];

      while (index > 0 && heapArray[parent].getKey() < bottom.getKey()) {
         heapArray[index] = heapArray[parent]; // move it down
         index = parent;
         parent = (parent - 1) / 2;
      }
      heapArray[index] = bottom;
   }

   public Node remove() {
      Node root = heapArray[0];
      heapArray[0] = heapArray[--currentSize];
      trickleDown(0);
      return root;
   }

   public void trickleDown(int index) {
      int largerChild;
      Node top = heapArray[index];
      while (index < currentSize / 2) {
         int leftChild = 2 * index + 1;
         int rightChild = leftChild + 1;
         if (rightChild < currentSize && // (rightChild exists?)
               heapArray[leftChild].getKey() < heapArray[rightChild].getKey())
            largerChild = rightChild;
         else
            largerChild = leftChild;
         if (top.getKey() >= heapArray[largerChild].getKey())
            break;
         // shift child up
         heapArray[index] = heapArray[largerChild];
         index = largerChild; // go down
      } // end while
      heapArray[index] = top; // root to index
   } // end trickleDown()

   public boolean change(int index, int newValue) {
      if (index < 0 || index >= currentSize)
         return false;
      int oldValue = heapArray[index].getKey(); // remember old
      heapArray[index].setKey(newValue); // change to new

      if (oldValue < newValue) // if raised,
         trickleUp(index); // trickle it up
      else // if lowered,
         trickleDown(index); // trickle it down
      return true;
   }

   public void displayHeap() {
      System.out.print("heapArray: "); 
      for (int m = 0; m < currentSize; m++)
         if (heapArray[m] != null)
            System.out.print(heapArray[m].getKey() + " ");
         else
            System.out.print("-- ");
      System.out.println();
      // heap format
      int nBlanks = 32;
      int itemsPerRow = 1;
      int column = 0;
      int j = 0; // current item

      System.out.println();

      while (currentSize > 0) {
         if (column == 0) // first item in row?
            for (int k = 0; k < nBlanks; k++) // preceding blanks
               System.out.print(' ');
         // display item
         System.out.print(heapArray[j].getKey());

         if (++j == currentSize) // done?
            break;

         if (++column == itemsPerRow) // end of row?
         {
            nBlanks /= 2; // half the blanks
            itemsPerRow *= 2; // twice the items
            column = 0; // start over on
            System.out.println(); // new row
         } else // next item on row
            for (int k = 0; k < nBlanks * 2 - 2; k++)
               System.out.print(' '); // interim blanks
      }
      System.out.println("\n"); 
   }
}

public class Main {
   public static void main(String[] args) {
      Heap theHeap = new Heap(31); // make a Heap; max size 31

      theHeap.insert(70); // insert 10 items
      theHeap.insert(40);
      theHeap.insert(50);
      theHeap.insert(20);
      theHeap.insert(60);
      theHeap.insert(100);
      theHeap.insert(80);
      theHeap.insert(30);
      theHeap.insert(10);
      theHeap.insert(90);

      theHeap.displayHeap();
      boolean success = theHeap.insert(123);
      theHeap.displayHeap();
      theHeap.remove();
      theHeap.displayHeap();
      success = theHeap.change(10, 20);
      theHeap.displayHeap();

   }

}



PreviousNext

Related