Advertisement
Guest User

Heap

a guest
Nov 22nd, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. package Lab07;
  2.  
  3. import java.io.*;
  4.  
  5. /*
  6.  * Parent Node = (k-1)/2
  7.  * Left Child = 2k + 1, Right Child = 2k + 2
  8.  *
  9.  * !!Need to write HeapSort!!  Is this needed?
  10.  * !!Heap Order Property!! - Checked
  11.  * !!Pop needs to return a Process!! - DONE
  12.  *
  13.  *  @Author Ryan DePrekel
  14.  */
  15.  
  16. public class PriorityHeap {
  17.    
  18.     private Process[] queue;
  19.     private int start_size = 16;
  20.     private int size;
  21.    
  22.     /*
  23.      ************************************************
  24.      * Constructors to create PriorityHeap Object
  25.      *   --Default: sets array length to start_size
  26.      *   --int s:   sets array length to s
  27.      ************************************************  
  28.      */
  29.     public PriorityHeap(){
  30.         size = 0;
  31.         queue = new Process[start_size];
  32.     }
  33.     public PriorityHeap(int s){
  34.         size = 0;
  35.         queue = new Process[s];
  36.     }
  37.    
  38.     public void print(){
  39.         int i;
  40.         for(i = 0; i < size; i++){
  41.             System.out.println(i + " " + queue[i].getPriority());
  42.         }
  43.     }
  44.     /*
  45.      ************************************************
  46.      * Push(): adds x to queue
  47.      *         Adjusts size and calls walkUp(int pos)
  48.      ************************************************
  49.      */
  50.     public void push(Process x){
  51.         queue[size] = x;
  52.         size++;
  53.         walkUp(size - 1);
  54.     }
  55.    
  56.     /*
  57.      ************************************************
  58.      * Pop() - Removes the head off the heap
  59.      *         Adjusts size and calls walkDown(int pos)
  60.      *        !!Needs to return a Process!!
  61.      ************************************************
  62.      */
  63.     public Process pop() throws IOException{
  64.         Process head = queue[0];
  65.         queue[0] = queue[size - 1];
  66.         size--;
  67.         walkDown(0);
  68.         return head;
  69.     }
  70.    
  71.     /*
  72.      * ***********************************************
  73.      *                     Heap Sort
  74.      * ***********************************************
  75.      */
  76.     /*public void sort(Process[] a){
  77.         int i;
  78.         queue = a;
  79.         buildHeap();
  80.         for(i = size;)
  81.     }*/
  82.    
  83.     //Swaps based on position
  84.     public void swap(int i, int j){
  85.         Process temp = queue[i];
  86.         queue[i] = queue[j];
  87.         queue[j] = temp;
  88.     }
  89.    
  90.     /*
  91.      ************************************************
  92.      *            --PRIVATE METHODS--
  93.      ************************************************
  94.      */
  95.    
  96.     // Returns true if queue is empty
  97.     private boolean isEmpty(){
  98.         return (size == 0);
  99.     }
  100.  
  101.     // Returns left child's index
  102.     private int getLeftChildIndex(int nodeIndex) {
  103.         return 2 * nodeIndex + 1;
  104.     }
  105.     // Return right child's index
  106.     private int getRightChildIndex(int nodeIndex) {
  107.         return 2 * nodeIndex + 2;
  108.     }
  109.     // Returns parent's index
  110.     private int getParentIndex(int nodeIndex) {
  111.         if(nodeIndex == 0){
  112.             return 0;
  113.         }
  114.         else
  115.             return (nodeIndex - 1) / 2;
  116.     }
  117.    
  118.     private void buildHeap(){
  119.         size = queue.length - 1;
  120.         for(int i = size/2; i > 0; i--)
  121.             walkDown(i);
  122.     }
  123.    
  124.     //Takes an element at position pos and walks it up the queue
  125.     // to maintain heap order upon insertion of an element
  126.     private void walkUp(int pos){
  127.         Process temp = queue[pos];
  128.         while((pos > 0) && temp.getPriority() < queue[getParentIndex(pos)].getPriority()){
  129.             queue[pos] = queue[getParentIndex(pos)];
  130.             pos = (pos - 1)/ 2;
  131.         }
  132.         queue[pos] = temp;
  133.     }
  134.    
  135.     //Takes an element and position pos and walks it down the queue
  136.     // to maintain heap order upon removal of an element
  137.     private void walkDown(int pos){
  138.         int sc = indexOfSmallest(pos);
  139.         Process temp = queue[pos];
  140.         while(getLeftChildIndex(pos) <= (size - 1) && temp.getPriority() > queue[sc].getPriority()){
  141.             queue[pos] = queue[sc];
  142.             pos = sc;
  143.             sc = indexOfSmallest(pos);
  144.         }
  145.         queue[pos] = temp;
  146.     }
  147.    
  148.     // Returns smallest child from parent at pos
  149.     private int indexOfSmallest(int pos){
  150.         int lc = getLeftChildIndex(pos);
  151.         int rc = getRightChildIndex(pos);
  152.         if(rc > (size - 1) || queue[lc].getPriority() < queue[rc].getPriority())
  153.             return lc;
  154.         else
  155.             return rc;
  156.     }
  157.    
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement