M
Michael
Guys,
I'm trying to compile this code to build a huffman tree, but I'm getting a
runtime error and my degugger ends up somewhere in the STL files. What am i
doing wrong. I appreciate its not effiecient/stylistic, etc, it was just
something i wanted to try
Thanks
Mike
#include <assert.h>
#include <vector>
#include <list>
using std::list;
using std::vector;
class HuffmanNode
{
public:
HuffmanNode* pLeft;
HuffmanNode* pRight;
char Represented;
float frequency;
HuffmanNode() : pLeft(NULL), pRight(NULL), Represented(-1),
frequency(-1) {}
void AddSubTreeToVector(vector<string>& v, string currentString);
};
int main()
{
list<HuffmanNode> nodes;
for(int i=0;i<256;i++)
{
HuffmanNode n;
n.Represented = i;
n.frequency = (i*i *10 )% 300;
nodes.push_back(n);
}
cout << "Populated Source List: " << nodes.size();
while(nodes.size() >1)
{
cout << "\n\tListSize: " << nodes.size();
HuffmanNode newNode;
newNode.pLeft = new HuffmanNode;
newNode.pRight = new HuffmanNode;
list<HuffmanNode>::iterator it;
list<HuffmanNode>::iterator smallest = nodes.begin();
//Find Smallest
for(it = nodes.begin();it != nodes.end();it++)
if(it->frequency <= smallest->frequency) smallest = it;
*newNode.pLeft = *(smallest);
newNode.frequency = smallest->frequency;
nodes.erase(smallest);
//Find the next Snmallest
smallest = nodes.begin();
for(it = nodes.begin();it != nodes.end();it++)
if(it->frequency <= smallest->frequency) smallest = it;
*newNode.pRight = *smallest;
newNode.frequency += smallest->frequency;
nodes.erase(smallest);
//Add the New Node
nodes.push_back(newNode);
}
HuffmanNode hmt = *(nodes.begin());
//Move Tree into vector for encoding:
vector<string> HuffmanCodes;
for(int i=0;i<256;i++)
{
string str;
HuffmanCodes.push_back(str);
}
hmt.AddSubTreeToVector(HuffmanCodes,"");
for(int i=0;i<256;i++)
{
string str = HuffmanCodes;
cout << i << ": " << str.c_str() << endl;
}
system("PAUSE");
}
void HuffmanNode::AddSubTreeToVector(vector<string>& v, string
currentString)
{
assert(this);
if(Represented != -1) v[Represented] = currentString;
//LeftSubTree
if(pLeft)
{
string LeftString;
LeftString = currentString;
LeftString.append("0");
pLeft->AddSubTreeToVector(v,LeftString);
}
//RightSubTree
if(pRight)
{
string RightString;
RightString = currentString;
RightString.append("0");
pRight->AddSubTreeToVector(v,RightString);
}
}
I'm trying to compile this code to build a huffman tree, but I'm getting a
runtime error and my degugger ends up somewhere in the STL files. What am i
doing wrong. I appreciate its not effiecient/stylistic, etc, it was just
something i wanted to try
Thanks
Mike
#include <assert.h>
#include <vector>
#include <list>
using std::list;
using std::vector;
class HuffmanNode
{
public:
HuffmanNode* pLeft;
HuffmanNode* pRight;
char Represented;
float frequency;
HuffmanNode() : pLeft(NULL), pRight(NULL), Represented(-1),
frequency(-1) {}
void AddSubTreeToVector(vector<string>& v, string currentString);
};
int main()
{
list<HuffmanNode> nodes;
for(int i=0;i<256;i++)
{
HuffmanNode n;
n.Represented = i;
n.frequency = (i*i *10 )% 300;
nodes.push_back(n);
}
cout << "Populated Source List: " << nodes.size();
while(nodes.size() >1)
{
cout << "\n\tListSize: " << nodes.size();
HuffmanNode newNode;
newNode.pLeft = new HuffmanNode;
newNode.pRight = new HuffmanNode;
list<HuffmanNode>::iterator it;
list<HuffmanNode>::iterator smallest = nodes.begin();
//Find Smallest
for(it = nodes.begin();it != nodes.end();it++)
if(it->frequency <= smallest->frequency) smallest = it;
*newNode.pLeft = *(smallest);
newNode.frequency = smallest->frequency;
nodes.erase(smallest);
//Find the next Snmallest
smallest = nodes.begin();
for(it = nodes.begin();it != nodes.end();it++)
if(it->frequency <= smallest->frequency) smallest = it;
*newNode.pRight = *smallest;
newNode.frequency += smallest->frequency;
nodes.erase(smallest);
//Add the New Node
nodes.push_back(newNode);
}
HuffmanNode hmt = *(nodes.begin());
//Move Tree into vector for encoding:
vector<string> HuffmanCodes;
for(int i=0;i<256;i++)
{
string str;
HuffmanCodes.push_back(str);
}
hmt.AddSubTreeToVector(HuffmanCodes,"");
for(int i=0;i<256;i++)
{
string str = HuffmanCodes;
cout << i << ": " << str.c_str() << endl;
}
system("PAUSE");
}
void HuffmanNode::AddSubTreeToVector(vector<string>& v, string
currentString)
{
assert(this);
if(Represented != -1) v[Represented] = currentString;
//LeftSubTree
if(pLeft)
{
string LeftString;
LeftString = currentString;
LeftString.append("0");
pLeft->AddSubTreeToVector(v,LeftString);
}
//RightSubTree
if(pRight)
{
string RightString;
RightString = currentString;
RightString.append("0");
pRight->AddSubTreeToVector(v,RightString);
}
}