最小堆MinHeap源码

最小堆MinHeap源码


MinHeap.h源码

#pragma  once
#include "stdafx.h"
#include <vector>
using namespace std;
class MinHeapNode
{
private:
    int _nodeIndex;
    float _nodeValue;
public:
    MinHeapNode(float nodeValue)
    {

        this->_nodeIndex = 0;
        this->_nodeValue = nodeValue;
    }
    float GetNodeValue(){return _nodeValue;}
    void SetNodeValue(float nodeValue){_nodeValue = nodeValue;}
    int GetNodeIndex(){return _nodeIndex;}
    void SetNodeIndex(int nodeIndex){_nodeIndex = nodeIndex;}

};
class MinHeap
{
private:
    vector<MinHeapNode*> _nodeList;
public:
    void Up(int i);
    void Down(int i);
    void Add(MinHeapNode* node);
    MinHeapNode* GetMin();
    void RemoveMin();

};

MinHeap.cpp源码

#pragma  once
#include "stdafx.h"
#include "MinHeap.h"
#include <vector>
using namespace std;

void MinHeap::Up(int i)
{
    MinHeapNode* upNode = _nodeList[i];
    int child = i;
    while(child >0)
    {
        int parent = (child-1)/2;
        if(upNode->GetNodeValue() >= _nodeList[parent]->GetNodeValue())
        {
            break;
        }
        _nodeList[child] = _nodeList[parent];
        _nodeList[child]->SetNodeIndex(child);
        child = parent;
    }
    _nodeList[child] = upNode;
    _nodeList[child]->SetNodeIndex(child);
}
void MinHeap::Down(int i)
{
    MinHeapNode* downNode = _nodeList[i];
    int parent = i;
    while(parent*2+1 < _nodeList.size()) //有子节点
    {
        int left = parent*2+1;
        int right = parent*2+2;
        int minIndex = left; //左右子节点中较小值的索引
        if(right < _nodeList.size()) 
        {
            if(_nodeList[right]->GetNodeValue() < _nodeList[left]->GetNodeValue())
            {
                minIndex = right;
            }
        }
        if(downNode->GetNodeValue() <= _nodeList[minIndex]->GetNodeValue()) //调整完毕
        {
            break;
        }
        _nodeList[parent] = _nodeList[minIndex]; //将较小的子节点向上拉变成父亲节点
        _nodeList[parent]->SetNodeIndex(parent);
        parent = minIndex;
    }
    _nodeList[parent] = downNode;
    _nodeList[parent]->SetNodeIndex(parent);
}
void MinHeap::Add(MinHeapNode* node)
{
    _nodeList.push_back(node);
    node->SetNodeIndex(_nodeList.size()-1);
    Up(_nodeList.size()-1);
}
MinHeapNode* MinHeap::GetMin()
{
    return _nodeList.size()>0?_nodeList[0]:NULL;
}
void MinHeap::RemoveMin()
{
    if(_nodeList.size()>0)
    {
        MinHeapNode* node = _nodeList[_nodeList.size()-1];
        _nodeList.pop_back();
        if(_nodeList.size()>0)
        {
            _nodeList[0] = node;
            _nodeList[0]->SetNodeIndex(0);
            Down(0);
        }

    }
}

MinHeapTest.h源码

#pragma once 
#include "stdafx.h"
#include "MinHeap.h"
#include <iostream>
using namespace std;
class MinHeapTest
{

public:
    void DoTest()
    {
        MinHeap* minHeap = new MinHeap();
        vector<MinHeapNode*> nodelist;

        nodelist.push_back(new MinHeapNode(4));
        nodelist.push_back(new MinHeapNode(5));
        nodelist.push_back(new MinHeapNode(8));
        nodelist.push_back(new MinHeapNode(1));
        nodelist.push_back(new MinHeapNode(10));
        nodelist.push_back(new MinHeapNode(6));
        nodelist.push_back(new MinHeapNode(3));
        nodelist.push_back(new MinHeapNode(7));
        nodelist.push_back(new MinHeapNode(2));
        nodelist.push_back(new MinHeapNode(9));

        for(int i=0;i<nodelist.size(); i++)
        {
            minHeap->Add(nodelist[i]);
        }

        while(minHeap->GetMin()!=NULL)
        {
            cout<<minHeap->GetMin()->GetNodeValue() << " ";
            minHeap->RemoveMin();
        }


        cout<<endl;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值