amdb

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

heap.hh

  1 #ifndef HEAP_HH
  2 #define HEAP_HH
  3 
  4 #include <iostream>
  5 #include <stdexcept>
  6 #include <cassert>
  7 
  8 using namespace std;
  9 
 10 template <typename T, int maxsize>class heap//模板类heap,类型T,常量maxsize
 11 {
 12 private:
 13     int num_values;//当前存储的元素的数量
 14     T values[maxsize];//存储元素的数组
 15     
 16     int parent(int index)
 17     {
 18         return (index - 1) / 2;
 19     }
 20 
 21     int left_child(int index)
 22     {
 23         return 2 * index + 1;        
 24     }
 25 
 26     int right_child(int index)
 27     {
 28         return 2 * index + 2;
 29     }
 30 
 31     void sift_down(int index)
 32     {
 33         assert(index < num_values);
 34         int left = left_child(index);
 35         int right = right_child(index);
 36         
 37         if(left >= num_values)
 38         {
 39             return;
 40         }
 41         if(right >= num_values)
 42         {
 43             if(values[left] < values[index])
 44             {
 45                 swap_values(index, left);
 46             }
 47         }
 48         else
 49         {
 50             T left_value = values[left];
 51             T right_value = values[right];
 52             int swap_child;
 53 
 54             if(left_value < values[index] || right_value < values[index])
 55             {
 56                 if(left_value < right_value)
 57                 {
 58                     swap_child = left;
 59                 }
 60                 else
 61                     swap_child = right;
 62                 swap_values(index, swap_child);
 63                 sift_down(swap_child);
 64             }
 65         }
 66     }
 67 
 68     void sift_up(int index)
 69     {
 70         int parent_index = parent(index);
 71         if(index == 0)
 72         {
 73             return;
 74         }
 75         assert(parent_index >= 0);
 76         assert(parent_index != index);
 77         if(values[index] < values[parent_index])
 78         {
 79             swap_values(index, parent_index);
 80             if(parent_index != 0)
 81             {
 82                 sift_up(parent_index);
 83             }
 84         }
 85     }
 86 
 87     void swap_values(int i, int j)
 88     {
 89         T tmp;
 90 
 91         assert(i >= 0 && i < num_values);
 92         assert(j >= 0 && j < num_values);
 93         assert(i != j);
 94 
 95         tmp = values[i];
 96         values[i] = values[j];
 97         values[j] = tmp;    
 98     }
 99 
100 
101 
102 public:
103     heap<T, maxsize>():num_values(0){cout<<"初始化啦"<<endl;}//初始化,在模板中的内容就无需再初始化了,只要初始化非模板内容就可以了。
104     
105     T get_first_value()
106     {
107         T result;
108         try
109         {    
110             if(num_values == 0)
111             {
112                 throw std::underflow_error("抛出异常:堆为空");
113                 result = values[0];
114                 --num_values;
115                 if(num_values != 0)
116                 {
117                     values[0] = values[num_values];
118                     sift_down(0);
119                 }
120                 return result;
121             }
122         }
123         catch (std::underflow_error &e)
124         {
125             cout << "捕捉异常:堆为空" << endl;
126         }
127     }
128 
129     void add_value(T value)
130     {
131         try
132         {
133             if(num_values >= maxsize)
134             {
135                 throw std::overflow_error("抛出异常:堆为满");
136             }
137             values[num_values] = value;
138             ++num_values;
139         }
140         catch (std::overflow_error &e)
141         {
142             cout << "捕捉异常:堆为满" << endl;
143         }
144         
145         if((num_values - 1)> 0)
146             sift_up(num_values - 1);
147     }
148     
149     T get_value(int index)
150     {
151         try
152         {    if(index >= maxsize)
153             {
154                 throw std::overflow_error("抛出异常:访问越界");
155             }    
156             return values[index];
157         }
158         catch (std::overflow_error &e)
159         {
160             cout << "捕捉异常:访问越界" << endl;
161         }
162     }
163 };
164 
165 #endif
View Code

xyz.cc

 1 #include <iostream>
 2 #include <string>
 3 #include "heap.hh"
 4 #include <stdlib.h>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     heap<int, 6>heapone;
10     
11     for(int i = 0; i < 6; i++)
12     {
13         heapone.add_value(i);
14     }
15 
16 //    int lastval = heapone.get_first_value();
17 //    cout<<"value0  = "<<lastval<<endl;;
18     
19     for(int i = 0; i < 6; i++)
20     {
21         cout<<"value"<<i<<" = "<<heapone.get_value(i)<<endl;        
22 
23 //        cout<<"value"<<i<<" = "<<heapone.get_first_value()<<endl;
24         
25     }
26 
27 
28     cout<<heapone.get_first_value()<<endl;
29 for(int i = 0; i < 6; i++)
30     {
31       cout<<"value"<<i<<" = "<<heapone.get_value(i)<<endl;        
32     
33 cout<<heapone.get_first_value()<<endl;
34  for(int i = 0; i < 6; i++)
35      {
36        cout<<"value"<<i<<" = "<<heapone.get_value(i)<<endl;         
37          
38  //        cout<<"value"<<i<<" = "<<heapone.get_first_value()<<endl;
39              
40       }
41 cout<<"value"<<i<<" = "<<heapone.get_first_value()<<endl;
42         
43      }
44 
45 
46 }
View Code

 

posted on 2015-04-30 10:30  amdb  阅读(182)  评论(0编辑  收藏  举报