Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1774490
  • 博文数量: 198
  • 博客积分: 4088
  • 博客等级: 上校
  • 技术积分: 2391
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-15 16:29
个人简介

游戏开发,系统架构; 博客迁移到:http://www.jianshu.com/u/3ac0504b3b8c

文章分类

全部博文(198)

文章存档

2017年(1)

2016年(12)

2015年(1)

2014年(3)

2013年(13)

2012年(18)

2011年(150)

分类: C/C++

2011-10-27 13:07:34

MinHeap.hpp
  1. #include <iostream>

  2. template <class type>
  3. class MinHeap
  4. {
  5. public:
  6.     MinHeap(int maxsize);
  7.     MinHeap(type* arr, int n);
  8.     ~MinHeap(){clear();}

  9.     int getHeapSize(){ return m_heapSize;}
  10.     type getHeapTop(){ return m_heapArr[0];}
  11.     void deleteTop();
  12.     void resize(int newsize);
  13.     bool isEmpty(){ return (m_heapSize == 0);}
  14.     bool isFull(){ return (m_heapSize == m_maxSize);}
  15.     void insertNode(const type& data);
  16.     void show();
  17. private:
  18.     int        m_heapSize;
  19.     int        m_maxSize;
  20.     type*    m_heapArr;
  21.     void filterDown(int p);
  22.     void filterUp(int p);
  23.     void clear(){ if(m_heapArr) delete []m_heapArr;}
  24. };


  25. template <class type>
  26. void swap(type* a, type* b)
  27. {
  28.     type t;
  29.     t = *a;
  30.     *a = *b;
  31.     *b = t;
  32. }

  33. template <class type>
  34. MinHeap<type>::MinHeap(int maxsize)
  35.     :m_maxSize(maxsize)
  36.     ,m_heapSize(0)
  37. {
  38.     m_heapArr = new type[m_maxSize];
  39. }

  40. template <class type>
  41. MinHeap<type>::MinHeap(type* arr, int n)
  42.     :m_maxSize(n)
  43.     ,m_heapSize(n)
  44. {
  45.     m_heapArr = new type[m_maxSize];
  46.     for(int i = 0; i < n; ++i)
  47.     {
  48.         m_heapArr[i] = arr[i];
  49.     }

  50.     int i = m_heapSize - 1;
  51.     while(i >= 0)
  52.     {
  53.         filterDown(i);
  54.         i--;
  55.     }
  56. }

  57. template <class type>
  58. void MinHeap<type>::resize(int newsize)
  59. {
  60.     if(newsize <= m_maxSize)
  61.     {
  62.         std::cout << "the new size is small than before, ignored!" << std::endl;
  63.         return;
  64.     }
  65.     m_maxSize = newsize;
  66.     type* temp = new type[m_maxSize];
  67.     for(int i = 0; i < m_heapSize; i++)
  68.     {
  69.         temp[i] = m_heapArr[i];
  70.     }

  71.     delete []m_heapArr;
  72.     m_heapArr = temp;
  73. }

  74. template <class type>
  75. void MinHeap<type>::filterDown(int p)
  76. {
  77.     int j, i = p;
  78.     j = i * 2 + 1;
  79.     
  80.     while(j < m_heapSize)
  81.     {
  82.         if( (j < m_heapSize -1) && (m_heapArr[j + 1] < m_heapArr[j]))
  83.             j++;
  84.         if(m_heapArr[i] <= m_heapArr[j]) break;
  85.         else
  86.         {
  87.             swap(&(m_heapArr[i]), &(m_heapArr[j]));
  88.             i = j;
  89.             j = j * 2 + 1;
  90.         }
  91.     }
  92. }

  93. template <class type>
  94. void MinHeap<type>::filterUp(int p)
  95. {
  96.     int i, j = p;
  97.     i = (j - 1)/2;

  98.     while( i >= 0)
  99.     {
  100.         if(m_heapArr[i] <= m_heapArr[j]) break;
  101.         else
  102.         {
  103.             swap(&(m_heapArr[i]), &(m_heapArr[j]));
  104.             j = i;
  105.             i = (i -1)/2;
  106.         }
  107.     }
  108. }

  109. template <class type>
  110. void MinHeap<type>::deleteTop()
  111. {
  112.     if(m_heapSize == 0)
  113.     {
  114.         std::cout << "the heap is empty, ignore this operation!" << std::endl;
  115.         return;
  116.     }

  117.     m_heapArr[0] = m_heapArr[m_heapSize - 1];
  118.     m_heapSize--;

  119.     filterDown(0);
  120. }

  121. template <class type>
  122. void MinHeap<type>::insertNode(const type& data)
  123. {
  124.     if(m_heapSize == m_maxSize)
  125.     {
  126.         std::cout << "the heap is already full, ignore this operation!" << std::endl;
  127.         return;
  128.     }

  129.     m_heapArr[m_heapSize] = data;
  130.     
  131.     filterUp(m_heapSize);

  132.     ++m_heapSize;
  133. }

  134. template <class type>
  135. void MinHeap<type>::show()
  136. {
  137.     for(int i = 0; i < m_heapSize; ++i)
  138.         std::cout << m_heapArr[i] << " ";
  139.     std::cout << std::endl;

  140. }
test.cpp
  1. #include "MinHeap.hpp"

  2. int main()
  3. {
  4.     int a[10] = {10, 1, 4, 2, 3, 5, 9, 8, 6, 7};

  5.     MinHeap<int> heap(a, 10);

  6.     heap.show();

  7.     heap.resize(15);

  8.     heap.insertNode(2);

  9.     heap.show();

  10.     heap.deleteTop();

  11.     heap.show();

  12.     system("pause");
  13.     return 0;
  14. }
阅读(2064) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~