Advertisement
m1o2

DataStructure Summer 2011 DaryHeap m1o2

Oct 17th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.22 KB | None | 0 0
  1. /*
  2.  *  DArySimpleIntegerMaximumHeap.cs
  3.  *  04.09.11
  4.  *  21:11
  5.  *  
  6.  * Programmers  :
  7.  *                  Miky Lestat AKA m1o2
  8.          
  9.  * Course       :   Data Structure
  10.  * lecturer     :   Amir Robinshtain
  11.  *
  12.  * a simple implementaion of D-ary Heaps using int arrays.
  13.  *
  14.  */
  15. using System;
  16.  
  17. namespace DataStructure_Heaps_Project
  18. {
  19.     public class DArySimpleIntegerHeap : ICountableHeap
  20.     {
  21.         public int Root { get; set; }                                       // will holds the Root of the heap. for example, if the heap is a Max heap then, it will hold the max
  22.         public int Degree { get { return _degree; } }                       // a property holds the degree of the heap.
  23.         public int[] Heap { get; set; }                                     // an array that contain all of the heap elements.
  24.         protected int Top { get; set; }                                     // an indicator ( index ) for the index of the last elements ( leaf ) in the heap ( array )
  25.        
  26.         public int SwapsCount{ get; set; }                                  // will hold the number of swaps done in Build-Heap operation
  27.         public int ComparisonsCount{ get; set; }                            // will hold the number of comaprisons done in Build-Heap operation.
  28.         int _degree;                                                        // holds the degree
  29.  
  30.         // if top numbers.length-1 isn't the last element in the array, then, we give us the top.
  31.         public DArySimpleIntegerHeap( int[] numbers, int top, int degree = 2)
  32.         {
  33.             initialize(numbers, top, degree);
  34.         }
  35.  
  36.         public DArySimpleIntegerHeap(int[] numbers, int degree = 2)
  37.         {
  38.             initialize(numbers, numbers.Length, degree);
  39.         }
  40.  
  41.         // initialize all the variables of the Heap implementation and also CALL   build-heap
  42.         protected void initialize(int[] numbers, int top, int degree)
  43.         {
  44.  
  45.             if (degree < 2)                                                
  46.                 throw new DegreeSmallerThanTwoException();
  47.  
  48.             this._degree = degree;
  49.             Heap = (int[])numbers.Clone();                                  // shallow copy ( copies only the references )
  50.  
  51.             Top = top;                          
  52.  
  53.             int swaps = 0, comparisons = 0;                               // for counting the number of swapns and number of comprisons.
  54.  
  55.             Build_Heap( out comparisons, out swaps );                   // buildheap
  56.             ComparisonsCount = comparisons;        
  57.             SwapsCount = swaps;
  58.         }
  59.  
  60.         public void Build_Heap(out int comparisons, out int swaps)
  61.         {
  62.                                                                                 // initialize variables.
  63.             swaps = 0;
  64.             comparisons = 0;
  65.  
  66.            
  67.             /*
  68.              * we will start at the parent of the last leaf in the array. for example, in heap with degree 2 we will start at [n/2]
  69.              * */
  70.             for (int index = ParentIndex( Top-1 ); index >= 0; --index)            
  71.             {
  72.                
  73.                 int s = 0;
  74.                 int c = 0;
  75.  
  76.                 HeapifyDown(index, out c, out s );                                    // heapify-Down
  77.  
  78.                 comparisons += c;
  79.                 swaps += s;  
  80.             }
  81.         }
  82.  
  83.         // in this project we did not use this method.
  84.         public void HeapifyUp(int current, out int comparisons, out int swaps)
  85.         {
  86.            
  87.             swaps = 0;
  88.             comparisons = 0;
  89.            
  90.             if (0 == current)                                                           // we reached the root
  91.                 return;
  92.  
  93.             int father = ParentIndex(current);                                          
  94.  
  95.            
  96.             ++comparisons;
  97.             if (Heap[father] < Heap[current])                                       // if the child bigger than the father
  98.             {
  99.                 int s = 0;
  100.                 int c = 0;
  101.                
  102.                 //++swaps;
  103.                 swaps += 3;
  104.                 exchange(current, father);                                          // swap the values.
  105.  
  106.                 HeapifyUp(father, out c, out s);                                    // recursivly heapify up
  107.                
  108.                 comparisons += c;
  109.                 comparisons += s;
  110.             }
  111.         }
  112.  
  113.  
  114.         public void HeapifyDown(int current, out int comparisons, out int swaps)
  115.         {
  116.            
  117.             swaps = 0;
  118.             comparisons = 0;
  119.  
  120.             int max = current;
  121.             int child = 0;
  122.  
  123.             /*
  124.              * for every child, and while childs exists, check if the children is smaller than theire fathers.
  125.              * if a child isn't smaller or equal to his father, and he is the bigger child ( holds the bigger key value of his brothers ) then swap it with his father
  126.              * and then heapifyDown.
  127.              * */
  128.             for (int index = 0; child < Top && index < Degree; ++index)
  129.             {
  130.                 child = Ichild(current, index + 1);                         // we could iterate as child += 1, but I think this way is clearer.
  131.  
  132.                 if (child < Top)                                            // I don't like to use break. if there isn't any child anymore then don't do nothing and it also will be the last operation
  133.                 {
  134.                     ++comparisons;
  135.                     if (child < Top && Heap[child] > Heap[max])             // find the biggest child
  136.                         max = child;
  137.                 }
  138.             }
  139.  
  140.             if (max != current)                                              // if there is a child that is bigger than his father
  141.             {
  142.                 int c = 0;
  143.                 int s = 0;
  144.  
  145.                 //++swaps;
  146.                 swaps += 3;
  147.                 exchange(current, max);                                     // exchange ( swap ) the child with his father, then heapifyDown
  148.  
  149.                 HeapifyDown(max, out c, out s );
  150.  
  151.                 swaps += s;
  152.                 comparisons += c;
  153.             }
  154.         }
  155.         public int ParentIndex(int child)
  156.         {
  157.             return (int)Math.Floor((double)((child - 1) / Degree));                   // getting the parent index of the givern child.
  158.            
  159.         }
  160.         public int Ichild(int parent, int child)                                        // getting the I child of the fiver parent.
  161.         {
  162.             return (Degree * parent) + child;                                           //
  163.         }
  164.  
  165.  
  166.         public int ExtractRoot(out int comparisons, out int swaps )
  167.         {
  168.            
  169.             comparisons = 0;
  170.             swaps = 0;
  171.            
  172.             int root;
  173.  
  174.             if (0 == Top)
  175.                 throw new HeapIsEmptyException();
  176.  
  177.            
  178.             //+swaps;
  179.             swaps += 3;
  180.             exchange(0, Top-1);                                                     // exchange the max with the last leaf.
  181.  
  182.             int s = 0;
  183.             int c = 0;
  184.             --Top;
  185.             HeapifyDown(0, out c, out s );                                          // heapifyDown the root that now holds the key of the last leaf
  186.  
  187.        
  188.             comparisons += c;
  189.             swaps += s;
  190.  
  191.             root = Heap[Top];                                                       //
  192.  
  193.             return root;                                                            // return the deleted root
  194.         }
  195.  
  196.         protected void exchange(int a, int b)
  197.         {
  198.             int temp = Heap[a];
  199.             Heap[a] = Heap[b];
  200.             Heap[b] = temp;
  201.         }
  202.  
  203.         /**
  204.          * a sorting method using HeapSort.
  205.          * it takes a numbers array ( , 2 variables for counting comparisons and swaps ) and the degree of the heap.
  206.          * the method will sort the array using heap sort.
  207.          * first of all it will build a heap, then it will call extract max till we sort all the array.
  208.          * */
  209.         public static void Sort(int[] numbers, out int comparisons, out int swaps, int degree = 2)
  210.         {
  211.             int s = 0;
  212.             int c = 0;
  213.        
  214.             DArySimpleIntegerHeap heap = new DArySimpleIntegerHeap(numbers, degree);                     // building the heap.
  215.            
  216.             comparisons = heap.ComparisonsCount;                                                        // how many comparisons there was in Build-Heap operations
  217.             swaps = heap.SwapsCount;                                                                    // how many swaps there was in Build-Heap operations
  218.  
  219.             for (int index = 0; index < numbers.Length; ++index)                                        
  220.             {
  221.                 numbers[numbers.Length - 1 - index] = heap.ExtractRoot( out c, out s );                 // get the extracted max and put it in the end of the array
  222.                
  223.                 comparisons += c;
  224.                 swaps += s;
  225.             }
  226.  
  227.             //swaps *= 3;                                                                        
  228.         }
  229.     }
  230. }
  231.  
  232.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement