#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct huffNode
{
int weight;
huffNode *L;
huffNode *R;
};
bool comp(huffNode const *a,huffNode const *b)
{
if((*a).weight < (*a).weight)
{
return true;
}
else
{
return false;
}
}
int main()
{
vector<huffNode *> map;
vector<int> original;
original.push_back(1);
original.push_back(7);
original.push_back(3);
original.push_back(4);
original.push_back(9);
original.push_back(8);
for(int i=0;i<original.size();i++)
{
huffNode *temp = new huffNode;
(*temp).L = NULL;
(*temp).R = NULL;
(*temp).weight = original[i];
map.push_back(temp);
}
sort(map.begin(),map.end(),comp);
while (map.size() != 1)
{
huffNode *temp = new huffNode;
(*temp).L = map[0];
(*temp).R = map[1];
(*temp).weight = (*map[0]).weight + (*map[1]).weight;
map.erase(map.begin());
map.erase(map.begin());
map.push_back(temp);
sort(map.begin(),map.end(),comp);
}
system("pause");
return 0;
}