优先级队列  0.1
数据结构_第7章
BinaryHeap.h
Go to the documentation of this file.
1 
14 #ifndef INCLUDE_BINARYHEAP_H_
15 #define INCLUDE_BINARYHEAP_H_
16 #include "seqList.h"
17 
22 namespace Queue
23 {
39  template <typename Comparable, class Compare = std::less<Comparable>>
40  class BinaryHeap
41  {
42  // Member types
43  public:
44  typedef size_t size_type;
45  typedef Comparable value_type;
47  typedef const value_type &const_reference;
48 
49  // Data members
50  protected:
56 
63 
73  static constexpr Compare comp = Compare{};
74 
75  // Member functions
76  protected:
83  void buildHeap();
84 
90  void percolateDown(size_type hole);
91 
97  void percolateUp(size_type hole);
98 
99  public:
108  explicit BinaryHeap(size_type capacity = 100, const Compare &comp = Compare{});
109 
116  explicit BinaryHeap(const List::seqList<value_type> &items, const Compare &comp = Compare{});
117 
122  ~BinaryHeap() = default;
123 
130  bool empty() const;
131 
137  const_reference top() const;
138 
143  void pop();
144 
150  void pop(Comparable *minItem);
151 
157  void push(const_reference x);
158 
163  void clear();
164 
174  size_type size() const;
175 
181  void print_heap(std::ostream &out) const
182  {
183  for (size_type i = 1; i <= current_size_; ++i)
184  out << array_[i] << ", ";
185  out << std::endl;
186  }
187 
195  friend std::ostream &operator<<(std::ostream &out, const BinaryHeap &heap)
196  {
197  heap.print_heap(out);
198  return out;
199  }
200  };
201 
202  template <typename Comparable, class Compare>
204  : current_size_{0}, array_(capacity + 1)
205  {
206  }
207 
208  template <typename Comparable, class Compare>
210  {
211  return !current_size_;
212  }
213 
214  template <typename Comparable, class Compare>
216  {
217  return array_[1];
218  }
219 
220  template <typename Comparable, class Compare>
222  {
223  size_type hole = ++current_size_;
224  array_.push_back(Comparable{}); // 在堆尾建立hole结点, 作为初始候选位置。可能触发doubleSpace
225 
226  // 向上过滤
227  // 若x置于hole(候选位置)中会破坏堆性质,则hole上滤一层
228  while (hole > 1 && comp(array_[hole / 2], x))
229  {
230  array_[hole] = array_[hole / 2];
231  hole /= 2;
232  }
233  array_[hole] = x;
234  }
235 
236  template <typename Comparable, class Compare>
238  : current_size_{items.size()}, array_(items.size() + 11)
239  {
240  for (size_type i = 0; i < items.size(); ++i)
241  {
242  array_[i + 1] = items[i];
243  array_.resize(items.size() + 1);
244  }
245  buildHeap();
246  }
247 
248  template <typename Comparable, class Compare>
250  {
251  for (size_type i = current_size_ / 2; i > 0; --i)
252  {
253  percolateDown(i);
254  }
255  }
256 
257  template <typename Comparable, class Compare>
259  {
260  auto tmp = std::move(array_[hole]);
261  size_type child;
262 
263  for (; hole * 2 <= current_size_; hole = child)
264  {
265  // 选出候选位置的优先级最高的child
266  child = hole * 2;
267  if (child < current_size_ && comp(array_[child], array_[child + 1]))
268  {
269  ++child;
270  }
271  // 检查是否违反堆性质,若是则下滤一层
272  if (comp(tmp, array_[child]))
273  {
274  array_[hole] = std::move(array_[child]);
275  continue;
276  }
277  break;
278  }
279  array_[hole] = std::move(tmp);
280  }
281 
282  template <typename Comparable, class Compare>
284  {
285  value_type tmp = std::move(array_[hole]);
286 
287  // 向上过滤
288  // 若x置于hole(候选位置)中会破坏堆性质,则hole上滤一层
289  while (hole > 1 && comp(array_[hole / 2], tmp))
290  {
291  array_[hole] = std::move(array_[hole / 2]);
292  hole /= 2;
293  }
294  array_[hole] = std::move(tmp);
295  }
296 
297  template <typename Comparable, class Compare>
299  {
300  array_[1] = std::move(array_[current_size_--]);
301  array_.pop_back();
302  percolateDown(1);
303  }
304 
305  template <typename Comparable, class Compare>
306  void BinaryHeap<Comparable, Compare>::pop(Comparable *minItem)
307  {
308  *minItem = std::move(array_[1]);
309  array_[1] = std::move(array_[current_size_--]);
310  array_.pop_back();
311  percolateDown(1);
312  }
313 
314  template <typename Comparable, class Compare>
316  {
317  array_.clear();
318  current_size_ = 0;
319  }
320 
321  template <typename Comparable, class Compare>
323  {
324  return array_.size();
325  }
326 
327 } // namespace Queue
328 
329 #endif /* INCLUDE_BINARYHEAP_H_ */
Queue::BinaryHeap::current_size_
size_type current_size_
Number of elements in heap.
Definition: BinaryHeap.h:55
Queue::BinaryHeap::buildHeap
void buildHeap()
Establish heap order property from an arbitrary arrangement of items.
Definition: BinaryHeap.h:249
Queue
队列
Definition: BinaryHeap.h:22
Queue::BinaryHeap::value_type
Comparable value_type
Type of the elements.
Definition: BinaryHeap.h:45
Queue::BinaryHeap::~BinaryHeap
~BinaryHeap()=default
Destroy the Binary Heap object.
Queue::BinaryHeap::percolateDown
void percolateDown(size_type hole)
Internal method to percolate down in the heap.
Definition: BinaryHeap.h:258
Queue::BinaryHeap::print_heap
void print_heap(std::ostream &out) const
将二叉堆中元素顺序输出.
Definition: BinaryHeap.h:181
List::seqList::clear
virtual void clear()
Definition: seqList.h:298
Queue::BinaryHeap::size_type
size_t size_type
an unsigned integral type
Definition: BinaryHeap.h:44
Queue::BinaryHeap::const_reference
const typedef value_type & const_reference
Definition: BinaryHeap.h:47
seqList.h
顺序表类
Queue::BinaryHeap::size
size_type size() const
Return size.
Definition: BinaryHeap.h:322
Queue::BinaryHeap::empty
bool empty() const
判队空
Definition: BinaryHeap.h:209
Queue::BinaryHeap::reference
value_type & reference
Definition: BinaryHeap.h:46
List::seqList< value_type >
List::seqList::size
size_t size() const
返回线性表的当前元素个数,即长度
Definition: seqList.h:310
Queue::BinaryHeap::BinaryHeap
BinaryHeap(size_type capacity=100, const Compare &comp=Compare{})
Construct a new Binary Heap object.
Definition: BinaryHeap.h:203
List::seqList::pop_back
void pop_back()
删除表尾元素
Definition: seqList.h:455
List::seqList::resize
void resize(size_t n)
Change size Resizes the container so that it contains n elements.
Definition: seqList.h:485
Queue::BinaryHeap::clear
void clear()
清空队列
Definition: BinaryHeap.h:315
Queue::BinaryHeap::array_
List::seqList< value_type > array_
The heap array.
Definition: BinaryHeap.h:62
Queue::BinaryHeap::comp
static constexpr Compare comp
function object of type Compare
Definition: BinaryHeap.h:73
Queue::BinaryHeap::percolateUp
void percolateUp(size_type hole)
Internal method to percolate up in the heap.
Definition: BinaryHeap.h:283
List::seqList::push_back
void push_back(const T &x)
在表尾插入新元素
Definition: seqList.h:439
Queue::BinaryHeap
Priority queue.
Definition: BinaryHeap.h:40
Queue::BinaryHeap::push
void push(const_reference x)
将x入队
Definition: BinaryHeap.h:221
Queue::BinaryHeap::top
const_reference top() const
返回队头元素值
Definition: BinaryHeap.h:215
Queue::BinaryHeap::pop
void pop()
队头元素出队
Definition: BinaryHeap.h:298
Queue::BinaryHeap::operator<<
friend std::ostream & operator<<(std::ostream &out, const BinaryHeap &heap)
将二叉堆中元素顺序插到输出流对象out中.
Definition: BinaryHeap.h:195