Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6.  
  7. template <typename T,
  8.                 typename CompareFunc = decltype(std::less<T>()),
  9.                         typename SwapFunc = decltype((void (*)(T&,T&))std::swap<T>) >
  10. class Heap {
  11. private:
  12.     std::vector<T> heap;
  13.     CompareFunc comparator;
  14.     SwapFunc swap;
  15.  
  16.     void heapShiftDown (int it){
  17.         int max = it;
  18.         if (it * 2 + 1 < heap.size() && comparator(heap[max], heap[it * 2 + 1]))
  19.             max = 2 * it + 1;
  20.         if (it * 2 + 2 < heap.size() && comparator(heap[max], heap[it * 2 + 2]))
  21.             max = 2 * it + 2;
  22.         if (max != it){
  23.             swap (heap[it], heap[max]);
  24.             heapShiftDown(max);
  25.         }
  26.     }
  27.  
  28.     void heapShiftUp (int it){
  29.         int parent = (it - 1) / 2;
  30.         if (it && comparator(heap[parent], heap[it])){
  31.             swap (heap[parent], heap[it]);
  32.             heapShiftUp (parent);
  33.         }
  34.     }
  35.  
  36. public:
  37.     Heap (CompareFunc cmp = std::less<T>(),
  38.             SwapFunc swap = (void (*)(T&,T&))std::swap<T>) :
  39.             comparator(cmp), swap(swap) {}
  40.  
  41.     void Insert (T value){
  42.         heap.push_back(value);
  43.         heapShiftUp(heap.size() - 1);
  44.     }
  45.  
  46.     void Erase(int it){
  47.         swap (heap[it], heap[heap.size() - 1]);
  48.         heap.pop_back();
  49.  
  50.         if (it != heap.size()){
  51.             heapShiftDown(it);
  52.             heapShiftUp(it);
  53.         }
  54.     }
  55.  
  56.     T& operator[] (int it){
  57.         return heap[it];
  58.     }
  59.  
  60.     void Erase(){
  61.         Erase(0);
  62.     }
  63.  
  64.     T GetMax (){
  65.         return heap[0];
  66.     }
  67.  
  68.     int GetSize() {
  69.         return heap.size();
  70.     }
  71. };
  72.  
  73. template <typename T, typename CompareFunc, typename SwapFunc>
  74. Heap<T, CompareFunc, SwapFunc> BuildSpecialHeap (CompareFunc cmp, SwapFunc swap){
  75.     return Heap<T,CompareFunc,SwapFunc>(cmp, swap);
  76. }
  77.  
  78. template <typename Iterator> void heapSortSwap (Iterator& a, Iterator& b){
  79.     std::swap (*a, *b);
  80. }
  81.  
  82. template <typename Iterator, typename CompareFunc>
  83. void heap_sort (Iterator begin, Iterator end, CompareFunc cmp){
  84.     auto heap = BuildSpecialHeap<Iterator>(
  85.             [&cmp] (Iterator a, Iterator b)  { return cmp (*a, *b); }
  86.             , heapSortSwap<Iterator>);
  87.     for (Iterator it = begin; it != end; it++)
  88.         heap.Insert(it);
  89.     while (heap.GetSize())
  90.         heap.Erase();
  91. }
  92.  
  93. int main(){
  94.     Heap<int> x;
  95.     x.Insert(5);
  96.     x.Insert(6);
  97.     x.Insert(3);
  98.     std::cout << x.GetMax() << std::endl;
  99.     x.Erase();
  100.     std::cout << x.GetMax() << std::endl;
  101.  
  102.     // ----------------------------------------------
  103.     std::cout << std::endl << std::endl;
  104.     // ----------------------------------------------
  105.  
  106.     std::vector<int> v;
  107.     v.push_back(1);
  108.     v.push_back(3);
  109.     v.push_back(2);
  110.     v.push_back(6);
  111.     v.push_back(7);
  112.     v.push_back(5);
  113.     heap_sort(v.begin(), v.end(), std::less<int>());
  114.     for (int i = 0; i < v.size(); i++)
  115.         std::cout << v[i] << " \n";
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement