最大堆MaxHeap源码

最大堆MaxHeap源码


MaxHeap.h源码

#pragma  once
#include "stdafx.h"
#include <vector>
using namespace std;
class MaxHeapNode
{
private:
    int _nodeIndex;
    float _nodeValue;
public:
    MaxHeapNode(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 MaxHeap
{
private:
    vector<MaxHeapNode*> _nodeList;
public:
    void Up(int i);
    void Down(int i);
    void Add(MaxHeapNode* node);
    MaxHeapNode* GetMax();
    void RemoveMax();

};

MaxHeap.cpp源码

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

void MaxHeap::Up(int i)
{
    MaxHeapNode* 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 MaxHeap::Down(int i)
{
    MaxHeapNode* 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 MaxHeap::Add(MaxHeapNode* node)
{
    _nodeList.push_back(node);
    node->SetNodeIndex(_nodeList.size()-1);
    Up(_nodeList.size()-1);
}
MaxHeapNode* MaxHeap::GetMax()
{
    return _nodeList.size()>0?_nodeList[0]:NULL;
}
void MaxHeap::RemoveMax()
{
    if(_nodeList.size()>0)
    {
        MaxHeapNode* node = _nodeList[_nodeList.size()-1];
        _nodeList.pop_back();
        if(_nodeList.size()>0)
        {
            _nodeList[0] = node;
            _nodeList[0]->SetNodeIndex(0);
            Down(0);
        }

    }
}

MaxHeapTest.h源码

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

public:
    void DoTest()
    {
        MaxHeap* maxHeap = new MaxHeap();
        vector<MaxHeapNode*> nodelist;

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

        for(int i=0;i<nodelist.size(); i++)
        {
            maxHeap->Add(nodelist[i]);
        }
        for(int i=0;i<nodelist.size(); i++)
        {
            cout << nodelist[i]->GetNodeValue() << " " << nodelist[i]->GetNodeIndex() << endl;
        }

        nodelist[1]->SetNodeValue(9);
        maxHeap->Up(nodelist[1]->GetNodeIndex());
        while(maxHeap->GetMax()!=NULL)
        {
            cout<<maxHeap->GetMax()->GetNodeValue() << " ";
            maxHeap->RemoveMax();
        }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值