最大堆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;
}
};