个人学习笔记:堆的实现(c++数组实现)

本文介绍了如何使用C++自定义实现堆,包括小根堆和大根堆的头文件(.h)和源文件(.cpp)实现。同时,也讨论了C++ STL库中的堆操作,包括大根堆和小根堆的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、自定义实现:

使用时为防止出现不能运行的情况(最好.h和.cpp文件都导入,似乎使用模板类时有时会出现只导入.h文件会一直报错的情况)
(1)小根堆实现:
①MinHeap.h

#include <iostream>

/**
 * 小根堆
 */
template <class T>
class MinHeap{
public:
    MinHeap();//构造函数
    ~MinHeap(){delete []heap;};//析构函数
    bool isEmpty(){ return (heapSize==0);};//当堆为空时返回true
    int size(){ return heapSize;};//返回堆的大小
    T top();//返回堆的根节点
    T pop();//返回并删除堆的根节点
    void push(const T& element);//向堆中插入元素,当堆满时扩展堆的容量
private:
    int capacity;//堆容量
    int heapSize;//堆的元素数量
    T* heap;//保存堆数据的数组
};

②MinHeap.cpp


#include "MinHeap.h"

/**
 * 小根堆的具体实现
 */
template<class T>
MinHeap<T>::MinHeap() {//构造函数
    //this->capacity=capacity;
    capacity=16;
    heapSize=0;
    heap=new T[capacity+1];
}

template<class T>
T MinHeap<T>::top(){//返回堆的根节点
    return heap[1];
}

template<class T>
T MinHeap<T>::pop(){//返回并删除堆的根节点

    T result=heap[1];//保存需要返回的根节点
    int currentNode=1;
    int child=2;

    //获取到堆的最后一个元素
    T lastElement=heap[heapSize--];

    while(child<=heapSize){
        //获取较小的子节点位置
        if(child<heapSize&&heap[child]>heap[child+1])
            child++;

        if(heap[child]>lastElement)//找到合适的插入位置了
            break;

        heap[currentNode]=heap[child];
        currentNode=child;
        child*=2;
    }
    heap[currentNode]=lastElement;

    return result;
}

template<class T>
void MinHeap<T>::push(const T& element){//向堆中插入元素,当堆满时扩展堆的容量
    if(heapSize==capacity){//必要时扩展数组长度
        capacity*=2;
        T* temp=new T[capacity];
        for (int i = 1; i <=heapSize; ++i) {
            temp[i]=heap[i];
        }
        delete []heap;
        heap=temp;
    }

    //为元素element寻找插入位置
    //currentNode位置为新增叶节点位置
    int currentNode=++heapSize;
    while (currentNode!=1&&heap[currentNode/2]>element){
        heap[currentNode]=heap[currentNode/2];//把元素向下移动
        currentNode/=2;
    }
    //将元素插入
    heap[currentNode]=element;
}

(2)大根堆实现:
①MaxHeap.h

#include <iostream>

/**
 * 大根堆
 */
template <class T>
class MaxHeap{
public:
    MaxHeap(int capacity);//构造函数
    ~MaxHeap(){delete []heap;};//析构函数
    bool isEmpty(){ return (heapSize==0);};//当堆为空时返回true
    int size(){ return heapSize;};//返回堆的大小
    T top();//返回堆的根节点
    T pop();//返回并删除堆的根节点
    void push(const T &element);//向堆中插入元素,当堆满时扩展堆的容量
private:
    int capacity;//堆容量
    int heapSize;//堆的元素数量
    T* heap;//保存堆数据的数组
};

②MinHeap.cpp

/**
 * 大根堆的具体实现
 */
#include "MaxHeap.h"

template<class T>
MaxHeap<T>::MaxHeap(int capacity) {//构造函数
    this->capacity=capacity;
    heapSize=0;
    heap=new T[capacity+1];
}

template<class T>
T MaxHeap<T>::top(){//返回堆的根节点
    return heap[1];
}

template<class T>
T MaxHeap<T>::pop(){//返回并删除堆的根节点
    T result=heap[1];//保存需要返回的根节点
    int currentNode=1;
    int child=2;

    //获取到堆的最后一个元素
    T lastElement=heap[heapSize--];

    while(child<=heapSize){
        //获取较大的子节点位置
        if(child<heapSize&&heap[child]<heap[child+1])
            child++;

        if(heap[child]<lastElement)//找到合适的插入位置了
            break;

        heap[currentNode]=heap[child];
        currentNode=child;
        child*=2;
    }
    heap[currentNode]=lastElement;

    return result;
}

template<class T>
void MaxHeap<T>::push(const T &element){//向堆中插入元素,当堆满时扩展堆的容量
    if(heapSize==capacity){//必要时扩展数组长度
        capacity*=2;
        T* temp=new T[capacity];
        for (int i = 1; i <=heapSize; ++i) {
            temp[i]=heap[i];
        }
        delete []heap;
        heap=temp;
    }

    //为元素element寻找插入位置
    //currentNode位置为新增叶节点位置
    int currentNode=++heapSize;
    while (currentNode!=1&&heap[currentNode/2]<element){
        heap[currentNode]=heap[currentNode/2];//把元素向下移动
        currentNode/=2;
    }
    //将元素插入
    heap[currentNode]=element;
}

2、c++ STL库中的堆:

①大根堆:

#include< queue >
//创建一个大根堆
priority_queue<T> maxHeap;

②小根堆:

#include< queue >
//创建一个小根堆
priority_queue<T,vector<T>,greater<T> > minHeap;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值