Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268857
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: C/C++

2016-03-23 17:15:59


  1. #pragma once
  2. #include <vector>

  3. template<class T>
  4. class Heap
  5. {
  6. public:
  7.             Heap()
  8.             {}
  9.             Heap(const int *a,size_t size)
  10.             {
  11.                         for(int i = 0;i < size;++i)
  12.                         {
  13.                                     _array.push_back(a[i]);
  14.                         }
  15.                         int root = (_array.size() - 2)/2;//找第一个非叶子结点
  16.                         while (root>=0)
  17.                         {
  18.                                     AdjustDown(root--);
  19.                         }
  20.             }
  21.             bool IsEmpty()const
  22.             {
  23.                         return _array.size() == 0;
  24.             }
  25.             T& top()
  26.             {
  27.                         if(_array.size() != 0)
  28.                           return _array[0];
  29.             }
  30.             void pop()
  31.             {
  32.                         if(_array.size() == 0)
  33.                         {
  34.                                     return;
  35.                         }
  36.                         swap(_array[0],_array[_array.size() - 1]);
  37.                         _array.pop_back();
  38.                         if(_array.size() > 1)
  39.                                       AdjustDown(0);
  40.             }
  41.             void push(const T& x)
  42.             {
  43.                         _array.push_back(x);
  44.                         if(_array.size() > 1)
  45.                              AdjustUp(_array.size() - 1);
  46.             }
  47. protected:
  48.     void AdjustUp(int child)
  49.             {
  50.                         while (child >= 1)
  51.                         {
  52.                                     int root = (child - 1) / 2;
  53.                                     if (_array[root] > _array[child])
  54.                                     {
  55.                                                 swap(_array[root], _array[child]);
  56.                                     }
  57.                                     child = root;
  58.                         }
  59.             }
  60.             void AdjustDown(int root)
  61.             {
  62.                         while(root <= (_array.size() - 2) / 2)
  63.                         {
  64.                                     int child;
  65.                                     if(root*2 + 2 < _array.size())
  66.                                     {
  67.                                                 child = _array[root*2 +1] > _array[root*2 + 2]?root*2 +2:root*2 +1;
  68.                                     }
  69.                                     else
  70.                                     {
  71.                                                 child = root*2 +1;
  72.                                     }
  73.                                     if(_array[root] > _array[child])
  74.                                     {
  75.                                                 swap(_array[root],_array[child]);
  76.                                     }
  77.                                     root = child;
  78.                         }
  79.             }
  80. protected:
  81.             vector<T> _array;
  82. };


测试:

  1. #include <iostream>
  2. using namespace std;
  3. #include "Heap.h"

  4. void test()
  5. {
  6.     int arr[] = {80,40, 30, 60, 90, 70, 10, 50, 20 };
  7.      Heap<int>hp1(arr,9);
  8. //     cout << "Over!" << endl;

  9.     hp1.push(15);
  10.     while (!hp1.IsEmpty())
  11.     {
  12.         cout << hp1.top()<< " ";
  13.         hp1.pop();
  14.     }
  15. }
  16. int main()
  17. {
  18.     test();
  19.     return 0;

  20. }


阅读(1134) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~