Advertisement
Gilgamesh858

Heap.cpp

Feb 9th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template<class T> class PriorityQueue
  6. {
  7.     public:
  8.         virtual T* extractMax() = 0;
  9.         virtual PriorityQueue* enqueue(T* x) = 0;
  10.         virtual int size() = 0;
  11. };
  12.  
  13. template<class H> class BinaryHeap
  14. {
  15.     private:
  16.         int length;
  17.         int heapsize;
  18.         H** A;
  19.        
  20.         int left(int i) { return i<<1; }
  21.         int right(int i) { return (i<<1)|1; }
  22.         int parent(int i) { return i>>1; }
  23.        
  24.         void swap(int i, int j)
  25.         {
  26.             H* temp = A[i];
  27.             A[i] = A[j];
  28.             A[j] = temp;
  29.         }
  30.        
  31.         void heapify(int i)
  32.         {
  33.             if( i > heapsize ) return;
  34.             int max = i;
  35.             int l = left(i);
  36.             int r = right(i);
  37.            
  38.             if( l < heapsize && compare(A[l], A[max]))
  39.                 max = l;
  40.             if( r < heapsize && compare(A[r], A[max]))
  41.                 max = r;
  42.             swap(i, max);
  43.             heapify(max);
  44.         }
  45.        
  46.     public:
  47.    
  48.         BinaryHeap(int size)
  49.         {
  50.             A = new H*[size];
  51.             length = size;
  52.             heapsize = 0;
  53.         }
  54.        
  55.         virtual bool compare(H* x, H* y) = 0;
  56.        
  57.         H* extractMax()
  58.         {
  59.             if( heapsize == 0 ) return NULL;
  60.             swap(1, heapsize);
  61.             heapsize--;
  62.             heapify(1);
  63.            
  64.             return A[heapsize+1];
  65.         }
  66.        
  67.         BinaryHeap<H>* enqueue(H x)
  68.         {
  69.             heapsize++;
  70.             A[heapsize] = new H(x);
  71.             int i = heapsize;
  72.             while( i > 1 && compare(A[i], A[parent(i)]))
  73.             {
  74.                 swap(i, parent(i));
  75.                 i = parent(i);
  76.             }
  77.             return this;
  78.         }
  79.        
  80.         int size()
  81.         {
  82.             return heapsize;
  83.         }
  84.        
  85.         BinaryHeap<H>* increaseKey(int i, H* k)
  86.         {
  87.             if( i > heapsize ) return this;
  88.             if( i < 1 ) return this;
  89.             if( *A[i] > *k ) return this;
  90.            
  91.             *A[i] = *k;
  92.            
  93.             while( i > 1 && compare(A[i], A[parent(i)]))
  94.             {
  95.                 swap(i, parent(i));
  96.                 i = parent(i);
  97.             }
  98.             return this;
  99.         }
  100.        
  101.         void print()
  102.         {
  103.             cout << "HEAP: ";
  104.             for( int i = 1; i <= heapsize; i++)
  105.                 cout << *A[i] << " ";
  106.             cout << endl;
  107.         }
  108. };
  109.  
  110. template<class H> class MaxBinaryHeap : public BinaryHeap<H>
  111. {
  112.     public:
  113.         bool compare( H* x, H* y) { return (*x > *y ? true : false); }
  114.        
  115.         MaxBinaryHeap(int size) : BinaryHeap<H>(size) {}
  116. };
  117.  
  118. template<class H> class MinBinaryHeap : public BinaryHeap<H>
  119. {
  120.     public:
  121.         bool compare( H* x, H* y) { return (*x < *y ? true : false); }
  122.        
  123.         MinBinaryHeap(int size) : BinaryHeap<H>(size) {}
  124. };
  125.  
  126. int main()
  127. {
  128.     MinBinaryHeap<int>* B = new MinBinaryHeap<int>(100);
  129.     B->enqueue(28)->enqueue(42)->enqueue(45)->enqueue(12)->enqueue(3)->enqueue(49)->enqueue(68)->enqueue(30)->enqueue(46);
  130.     B->print();
  131.    
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement