二叉堆(完全二叉树)

本文介绍了二叉堆中的一种特殊形式——最小堆,并提供了相应的实现代码,详细阐述了如何通过完全二叉树的特性来构建和操作最小堆。

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



最小堆的实现代码:

/* 
 * 最小堆是一颗完全二叉树(这里用数组实现完全二叉树)
 * index:  0   1 2 3 4 5 6
 * value: [空] a b c d 
 * 
 * 任意index,其父亲为index/2,左儿子为2*index,右儿子为2*index + 1 
 * 时间复杂度: O(logN)
 */
#include<iostream>
#include<vector>

using namespace std;

template<typename Comparable>
class BinaryHeap
{
        public:
                explicit BinaryHeap(int capacity)
                {
                        currentSize = 0;
                        array.resize(capacity);
                 };
                explicit BinaryHeap(const vector<Comparable> &items);

                bool isEmpty() const;
                const Comparable& findMin() const;
                void insert(const Comparable &x);
                void deleteMin();
                void deleteMin(Comparable &item);
                void makeEmpty();

        private:
                int currentSize;
                vector<Comparable> array;
                void buidHeap();
                void percolateDown(int hole);
};

template<typename Comparable>
void BinaryHeap<Comparable>::insert(const Comparable &x)
{       
        if(currentSize == array.size() - 1) 
                array.resize(array.size() * 2);
        
        //hole表示即将插入的空闲节点
        int hole = ++currentSize;
        
        //节点上虑,最终最小值为root节点
        for(;hole > 1 && x < array[hole/2]; hole /= 2)
                array[hole] = array[hole/2];

        
        array[hole] = x;
}

template<typename Comparable>
void BinaryHeap<Comparable>::deleteMin()
{
        if(isEmpty())
                throw 1;

        array[1] = array[currentSize--];
        percolateDown(1);
}

template<typename Comparable>
void BinaryHeap<Comparable>::deleteMin(Comparable& minItem)
{
        if(isEmpty())
                throw 1;

        minItem = array[1];
        array[1] = array[currentSize--]; //最后一个叶子节点暂时放入根节点(此时的空闲节点位置)
        percolateDown(1);       //空闲节点下虑,形成新的完全二叉树
}

/*节点下虑*/
template<typename Comparable>
void BinaryHeap<Comparable>::percolateDown(int hole)
{
        int child;
        Comparable tmp = array[hole];//保存临时节点

        for(; hole*2 <= currentSize; hole = child)
        {
                child = hole*2;
                if(child != currentSize && array[child+1] < array[child])
                        child++;

                if(tmp < array[child])
                        array[hole] = array[child];
                else
                        break;

        }

        array[hole] = tmp;
}

template<typename Comparable>
bool BinaryHeap<Comparable>::isEmpty() const
{
        return currentSize == 0? true : false;
}

int main()
{
        int num;
        int min;
        BinaryHeap<int> heap(100);//初始化(自动扩容)

        heap.insert(9);
        heap.insert(7);
        heap.insert(8);
        heap.insert(1);
        heap.insert(8);

        heap.deleteMin(min);
        cout << "min number:" << min << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值