最大堆的建立/插入/删除元素

头文件://函数声明,结构体声明

//大顶堆
// 下面是头文件的保护
//

#ifndef HEAP_MAXHEAP_H
#define HEAP_MAXHEAP_H
template <class T>
class Maxheap{
public:
    Maxheap(int mx = 10);   //构造函数,默认堆的大小为10
    virtual ~Maxheap();     //析构函数
    bool IsEmpty();         //判断堆是否为空
    void Push(const T&);    // 往堆中增加数据
    void Pop();             //删除堆中根结点
    const T& Top() const;   //获取根中数据,但不删除
private:
    T* heapArray;  //定义数组
    int maxSize;  //数组最大存储量
    int currentSize;  //当前堆里面的数据个数
    void trickleUp(int index);  //向上调整
    void trickleDown(int index); //向下调整
};
template <class T>
Maxheap<T>::Maxheap(int mx ){   //构造函数初始化
    if(mx < 1)
        throw "max size must be >= 1."; //异常处理
    maxSize = mx;
    currentSize = 0;
    heapArray = new T[maxSize]; //用new创建数组

}
template <class T>
Maxheap<T> ::~Maxheap() {
    delete[] heapArray; //删除创建出来的数组
}
template <class T>
bool Maxheap<T>::IsEmpty() {
    return currentSize == 0; //堆为空时
}

template <class T>
void Maxheap<T> ::Push(const T& e){
    if(currentSize == maxSize) throw "the Maxheap is full."; //如果满了,则抛出异常

    heapArray[currentSize] = e; //把新的数据放在最后
    //向上调整
    trickleUp(currentSize++); //向上找到合适的位置插入,然后currentSize+1

}
template <class T>
void Maxheap<T>::trickleUp(int index) {
    //子结点-1 然后除以2 向下取整 找到它的父结点
    int parent = (index -1)/2; //找到父结点的下标
    T bottom = heapArray[index] ;  //把当前数值保存起来
    while(index >0 && heapArray[parent] < bottom){  //当父结点的数小于要插入的数时,不断向上
        heapArray[index] = heapArray[parent];
        index = parent; //index开始是子结点,然后让它指向父结点,只要大于0,就不断向上
        parent = (parent -1)/2; //父结点不断向上

    }
    heapArray[index] = bottom;  //把数值放在当前查找的位置

}
template <class T>
const T& Maxheap<T>::Top() const{  //显示最大数
    return heapArray[0];
}
template <class T>
void Maxheap<T> ::Pop(){  //删除根结点,把最后的数放到删除的位置
    heapArray[0] = heapArray[--currentSize];  //把最后一个子结点的数值拿来临时放到跟结点中
    trickleDown(0);  //向下调整或向下渗透

}
template <class T>
void Maxheap<T>::trickleDown(int index) {   //向下渗透或向下调整
    int largeChild; //找比较大的孩子
    T top = heapArray[index];  //把最后一个结点保存在临时变量top中,此函数上接Pop函数
    while(index < currentSize/2){  //循环向下渗透,到最后一层的上一层就可以了
        int leftChild = 2 * index + 1;   //找左孩子
        int rightChild = leftChild + 1; //会更快,也可以写 2*index + 1,找到右孩子
        if(rightChild < currentSize && heapArray[leftChild] < heapArray[rightChild]){ //判断有右孩子 而且左孩子的数小于右孩子的数
            largeChild = rightChild;  //让右孩子作为最大孩子
        }
        else
            largeChild = leftChild;  //否则左孩子作为最大孩子
        if(top >= heapArray[largeChild])
            break;  //停止循环
        heapArray[index] = heapArray[largeChild];
        index = largeChild; //继续向下渗透
    }
    heapArray[index] = top;


}


#endif //HEAP_MAXHEAP_H

 

.cpp文件

//堆的操作
//插入新结点:先放到最后一个结点的最后,再向上排
//删除新结点:删除后,把最后一个结点放到删除的结点的位置,再调整位置
//堆用于优先队列,堆排序,大顶堆,小顶堆

#include <iostream>
#include "Maxheap.h"
using namespace std;
int main() {
    Maxheap<int> h(100); //构造函数
    cout << h.IsEmpty() <<endl;
    h.Push(20);
    //h.Push(30);
    //h.Push(15);
    cout <<"最大堆为:"<< h.Top() <<endl;
    h.Push(90);
    h.Push(35);
    cout <<"最大堆为:"<< h.Top() <<endl;
    h.Pop(); //删除根结点
    cout <<"删除90后最大堆为:"<< h.Top() <<endl;
    cout << "OK!"<<endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值