堆及堆排序c++实现

———-

不眼高手低,基础的东西一定要弄懂弄透,各种边界情况都要考虑到,争取高效率0bug。最近看算法导论看到堆的部分了,今天再实现一遍。

#pragma once
#include<iostream>
/*最大堆实现*/
namespace myheap {
template<class T>
class Heap {
public:
  using node_type = T;
  using ptr_to_node = T*;
  using size_type = size_t;

public:
  /*capacity容量,guard哨兵节点*/
  Heap(size_type capacity, node_type guard) :heap_head_(nullptr), capacity_(capacity), node_count_(0) {
    heap_head_ = new node_type[capacity + 1];
    heap_head_[0] = guard;
  }
  /*这个构造函数接受一个数组,head首地址,node_count节点个数,guard哨兵节点*/
  Heap(ptr_to_node head, size_type node_count, node_type guard) :heap_head_(nullptr), capacity_(2 * node_count), node_count_(node_count) {
    heap_head_ = new node_type[node_count * 2 + 1];
    heap_head_[0] = guard;
    for (size_type i = 1; i <= node_count; ++i) {
      heap_head_[i] = head[i - 1];
    }
    for (size_type i = node_count / 2; i >= 1; --i) {
      _DownFilter(i, node_count);
    }
  }

  ~Heap() {
    delete[] heap_head_;
  }

  node_type GetHeadNode() const {
    return heap_head_[1];
  }
  /*删除首节点*/
  void Pop() {
    heap_head_[1] = heap_head_[node_count_];
    --node_count_;
    _DownFilter(1, node_count_);
  }

  bool InsertNode(node_type node) {
    if (node_count_ + 1 > capacity_)
      return false;
    ++node_count_;
    heap_head_[node_count_] = node;
    _UpFilter(node_count_);
    return true;
  }

  bool IsHeap() {
    if (heap_head_ == nullptr || node_count_ == 0)
      return false;
    for (size_type i = 1; i <= node_count_ / 2; ++i) {
      if (heap_head_[i] < heap_head_[i * 2]) {
        return false;
      }
      if (i * 2 + 1 <= node_count_ && heap_head_[i * 2 + 1] > heap_head_[i]) {
        return false;
      }
    }
    return true;
  }
  /*堆排序*/
  void Sort() {
    _HeapSort(node_count_);
  }

  void print() {
    for (size_type i = 1; i <= node_count_; ++i) {
      std::cout << i << " : " << heap_head_[i] << std::endl;
    }
  }

 private:
  /*下滤操作,root_index要下滤的节点索引,last_index下滤终止节点索引*/
  void _DownFilter(size_type root_index,size_type last_index) {
    if (root_index >= last_index)
      return;
    size_type child_index = 0;
    size_type parent_index = root_index;
    node_type node = heap_head_[root_index];
    for (; 2 * parent_index <= last_index; parent_index = child_index) {
      child_index = 2 * parent_index;
      if (child_index < last_index && heap_head_[child_index] < heap_head_[child_index + 1]) {
        ++child_index;
      }
      if (node < heap_head_[child_index]) {
        heap_head_[parent_index] = heap_head_[child_index];
      }
      else
        break;
    }
    heap_head_[parent_index] = node;
  }
  /*上滤操作,index要上滤的节点索引*/
  void _UpFilter(size_type index) {
    node_type node = heap_head_[index];
    size_type i = index;
    for (; i >= 1; i = i / 2) {
      if (node > heap_head_[i / 2]) {
        heap_head_[i] = heap_head_[i / 2];
      }
      else
        break;
    }
    heap_head_[i] = node;
  }
  /*堆排序实现*/
  void _HeapSort(size_type last_index) {
    for (size_type i = last_index; i >= 1; --i) {
      node_type tmp = heap_head_[1];
      heap_head_[1] = heap_head_[i];
      heap_head_[i] = tmp;
      _DownFilter(1, i - 1);
    }
  }

  ptr_to_node heap_head_; //首地址
  size_type capacity_;    //容量
  size_type node_count_;  //节点数量
};
}//namespace myheap
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值