编程练习——堆heap

 因为vs2005中c++的Heap构造有些问题,所以才重写了heap类。

不知道vs2008中这个是不是个bug。

 

  1. /* created by chico chen
  2. *  date 2008/10/25
  3. */
  4. template<class T,class Cmp>
  5. class Heap
  6. {
  7. private:
  8.     vector<T> heap;
  9.     void ShiftDown(int index)
  10.     {
  11.         while(!IsLeaf(index))
  12.         {
  13.             int l = Lchild(index);
  14.             int r = Rchild(index);
  15.             if(r != -1)
  16.             {
  17.                 if(Cmp::Up(heap[r],heap[l]))
  18.                 {
  19.                     // put the up node to the left
  20.                     swap(heap[l],heap[r]);
  21.                 }
  22.             }
  23.             if(Cmp::Up(heap[l],heap[index]))
  24.             {
  25.                 // up the left child 
  26.                 swap(heap[l],heap[index]);
  27.                 index = l;
  28.             }
  29.             else
  30.             {
  31.                 break;
  32.             }
  33.         }
  34.     }
  35.     void ShiftUp(int index)
  36.     {
  37.         int parent = -1;
  38.         while(index != 0)
  39.         {
  40.             parent = Parent(index);
  41.             if(Cmp::Up(heap[index],heap[parent]))
  42.             {
  43.                 swap(heap[index],heap[parent]);
  44.                 index = parent;
  45.             }
  46.             else
  47.             {
  48.                 break;
  49.             }
  50.         }
  51.         
  52.     }
  53.     void ReHeapAll()
  54.     {
  55.         for(int i=Parent(heap.size()-1); i>= 0; i--)
  56.         {
  57.             ShiftDown(i);
  58.         }
  59.     }
  60.     void ReHeapOne(int index)
  61.     // set one prior and re-position the index node
  62.     {
  63.         T data = heap[index];
  64.         ShiftDown(index);
  65.         if(Cmp::Eq(data,heap[index]))
  66.         {
  67.             ShiftUp(index);
  68.         }
  69.     }
  70.     int Parent(int index)
  71.     // parent node of the index node
  72.     {
  73.         return (index-1)/2;
  74.     }
  75.     int Lchild(int index)
  76.     // Left child of the index node
  77.     {
  78.         if(!IsLeaf(index))
  79.             return index*2+1;
  80.         return -1;
  81.     }
  82.     int Rchild(int index)
  83.     // Right child of the index node
  84.     {
  85.         if(!IsLeaf(index))
  86.         {
  87.             int r= index*2+2;
  88.             if(r<heap.size())
  89.                 return r;
  90.         }
  91.         return -1;
  92.     }
  93.     int IsLeaf(int index)
  94.     {
  95.         assert(index < heap.size());
  96.         if(heap.size() == 1)
  97.             return true;
  98.         int lastNode = Parent(heap.size()-1);
  99.         return lastNode < index;
  100.     }
  101.     int FindData(T& data)
  102.     {
  103.         int len = heap.size();
  104.         for(int i = 0; i < len; i++)
  105.         {
  106.             if(Cmp::Eq(data,heap[i]))
  107.             {
  108.                 return i;
  109.             }
  110.         }
  111.         return -1;// can not find the data
  112.     }
  113.     void printTree(int index,int count)
  114.     {
  115.         if(index < heap.size() && index >=0)
  116.         {
  117.             
  118.             printTree(Rchild(index),count+4);
  119.             for(int i = 0; i < count; i++)
  120.             {
  121.                 cout << " ";
  122.             }
  123.             cout <<heap[index]<<endl;
  124.             printTree(Lchild(index),count+4);
  125.         }
  126.     }
  127. public:
  128.     Heap()
  129.     {
  130.         heap = vector<T>();
  131.     }
  132.     ~Heap()
  133.     {
  134.         // heap destroyed
  135.     }
  136.     bool IsEmpty()
  137.     {
  138.         return heap.size() == 0;
  139.     }
  140.     void Insert(T& data)
  141.     {
  142.         heap.push_back(data);
  143.         ShiftUp(heap.size()-1);
  144.     }
  145.     void Delete(int index)
  146.     {
  147.         int last = heap.size()-1;
  148.         if(index>=0 && index <= last)
  149.         {
  150.             
  151.             swap(heap[index],heap[last]);
  152.             heap.pop_back();
  153.             ReHeapOne(index);
  154.         }
  155.     }
  156.     void Delete(T& data)
  157.     {
  158.         int index = FindData(data);
  159.         if(index != -1)
  160.             Delete(index);
  161.     }
  162.     void RemoveTop()
  163.     {
  164.         if(!IsEmpty()&&heap.size()>1)
  165.         {   
  166.             swap(heap[0],heap[heap.size()-1]);
  167.             heap.pop_back();
  168.             ShiftDown(0);
  169.         }
  170.         else if(heap.size() == 1)
  171.         {
  172.             heap.pop_back();
  173.         }
  174.         
  175.     }
  176.     T& Top()
  177.     {
  178.         return heap[0];
  179.     }
  180.     void Print()
  181.     {
  182.         printTree(0,1);
  183.     }
  184.     
  185. };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值