Advertisement
Sheero

HeapPriorityQueueClass

Jun 21st, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. //Zehra Baig
  2. //CSC-236-C1
  3. //Lab 6-B
  4.  
  5. public class HeapPriorityQueueClass implements HeapPriorityQueue
  6. {
  7.     //Fields
  8.     private Comparable[] heapArray;
  9.     private int current;
  10.     private int last;
  11.  
  12.     //Default Constructor
  13.     public HeapPriorityQueueClass()
  14.     {
  15.         heapArray = new Comparable[250];
  16.         current = -1;
  17.         last = 250 - 1;
  18.     }
  19.  
  20.     //Checks if queue is empty
  21.     public boolean isEmpty()
  22.     {
  23.         return (current == -1);
  24.     }
  25.  
  26.     //Checks if queue is full
  27.     public boolean isFull()
  28.     {
  29.         return (current == last);
  30.     }
  31.  
  32.     //Adds item to back of queue
  33.     public void enqueue(Comparable value) throws HeapOverflowException
  34.     {
  35.         if(current == last)
  36.         {
  37.             throw new HeapOverflowException("Heap overflow");
  38.         }
  39.  
  40.         current++;
  41.         reheapifyUpward(value);      
  42.     }
  43.  
  44.     //Removes item from front of queue
  45.     public Comparable dequeue() throws HeapUnderflowException
  46.     {
  47.         if(current == -1)
  48.         {
  49.             throw new HeapUnderflowException("Heap underflow");
  50.         }
  51.  
  52.         Comparable toRemove = heapArray[0];
  53.         Comparable temp = heapArray[current];
  54.         current--;  
  55.         reheapifyDownward(temp);
  56.  
  57.         return toRemove;
  58.     }
  59.  
  60.     //Raise item upward until it has a parent with higher priority
  61.     public void reheapifyUpward(Comparable val)
  62.     {
  63.         int position = current;
  64.  
  65.         while(position > 0 && val.compareTo(heapArray[(position - 1) / 2]) > 0)
  66.         {
  67.             heapArray[position] = heapArray[(position - 1) / 2];                                                          
  68.             position = (position - 1) / 2;
  69.         }
  70.  
  71.         heapArray[position] = val;
  72.     }
  73.  
  74.     //Sink item downward until it no longer has a child with higher priority
  75.     public void reheapifyDownward(Comparable val)
  76.     {
  77.         int position = 0;
  78.         int move = reposition(position, val);
  79.         while(move != position)
  80.         {
  81.             heapArray[position] = heapArray[move];
  82.             position = move;
  83.             move = reposition(position, val);
  84.         }
  85.  
  86.         heapArray[position] = val;
  87.     }
  88.  
  89.     //Reposition the items as needed by reheapify methods
  90.     public int reposition(int pos, Comparable val)
  91.     {
  92.         int leftPosition = (pos * 2) + 1;
  93.         int rightPosition = (pos * 2) + 2;
  94.  
  95.         if(leftPosition > current)
  96.         {
  97.             return pos;
  98.         }
  99.         else if(leftPosition == current)
  100.         {
  101.             if(val.compareTo(heapArray[leftPosition]) < 0)
  102.             {
  103.                 return leftPosition;
  104.             }
  105.             else
  106.             {
  107.                 return current;
  108.             }
  109.         }
  110.         else if(heapArray[leftPosition].compareTo(heapArray[rightPosition]) < 0)
  111.         {
  112.             if(heapArray[rightPosition].compareTo(val) <= 0)  
  113.             {
  114.                 return current;
  115.             }
  116.             else
  117.             {
  118.                 return rightPosition;
  119.             }
  120.         }
  121.         else
  122.         {
  123.             if(heapArray[leftPosition].compareTo(val) <= 0)
  124.             {
  125.                 return current;
  126.             }
  127.             else
  128.             {
  129.                 return leftPosition;
  130.             }
  131.         }      
  132.     }
  133.  
  134.     //Overridden toString method
  135.     public String toString()
  136.     {
  137.         StringBuilder strBuild = new StringBuilder();
  138.         int level = 1;
  139.         int count = 1;      
  140.         int i = 0;
  141.  
  142.         while(i <= current)
  143.         {      
  144.             for(int j = 0; j < count && i <= current; j++)
  145.             {
  146.                 strBuild.append(heapArray[i] + " at level " + level + "\n");          
  147.                 i++;
  148.             }
  149.  
  150.             level++;
  151.             count *= 2;
  152.         }
  153.  
  154.         return strBuild.toString();
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement