Hello.
I am currently working on a huffman code program for my class. As of right now, I am currently in the development/debugging stage. I've got the whole thing pretty much covered, but I am having trouble with my tree traversal. I am trying to do an inorder traversal but everytime my program runs, it crashes when I tell it to traverse. My tree making algorithm is fine, but there is something wrong with my traversal algorithm. Code is below.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include "Node.h"
using namespace std;
void fillList(char* sent, int size, vector<Node> &vec);
bool checkList(char ch, vector<Node> vec);
void getFreq(vector<Node> &vec, char* sent, int size);
void sort(vector<Node> &vec);
Node makeTree(vector<Node> &vec);
void traverse(Node *nde);
int main()
{
char ch = 0;
ifstream inFile;
ofstream outFile;
int count = 0;
vector<Node> list;
Node tree = 0;
inFile.open("message.txt");
if(!inFile)
{
cerr << "Unable to open file! Program terminating..." << endl;
return(EXIT_FAILURE);
}
while(inFile.good())
{
ch = inFile.get();
count++;
}
inFile.close();
char* msg = new char[count];
inFile.open("message.txt");
if(!inFile)
{
cerr << "Unable to open file! Program terminating..." << endl;
return(EXIT_FAILURE);
}
while(inFile.good())
{
inFile.getline(msg, count);
}
cout << msg << endl;
fillList(msg, count, list);
getFreq(list, msg, count);
sort(list);
for(size_t i = 0; i < list.size(); i++)
{
cout << list[i].letter << " - Frequency: " << list[i].frequency << endl;
}
tree = makeTree(list);
traverse(&tree);
delete[] msg;
}
void fillList(char* sent, int size, vector<Node> &vec)
{
char ch = 0;
for(int i = 0; i < size; i++)
{
ch = sent[i];
if(vec.size() == 0)
{
Node *temp = new Node(ch);
vec.push_back(*temp);
}
else if(!checkList(ch, vec))
{
Node *temp = new Node(ch);
vec.push_back(*temp);
}
}
}
bool checkList(char ch, vector<Node> vec)
{
for(size_t i = 0; i < vec.size(); i++)
{
if(ch == vec[i].letter)
{
return 1;
}
}
return 0;
}
void getFreq(vector<Node> &vec, char* sent, int size)
{
for(size_t i = 0; i < vec.size(); i++)
{
int count = 0;
for(int j = 0; j < size; j++)
{
if(vec[i].letter == sent[j])
{
count++;
}
}
vec[i].frequency = count;
}
}
void sort(vector<Node> &vec)
{
for(size_t i = 0; i < vec.size() - 1; i++)
{
for(size_t j = i + 1; j < vec.size(); j++)
{
if(vec[i].frequency > vec[j].frequency)
{
Node temp = vec[i];
vec[i] = vec[j];
vec[j] = temp;
}
}
}
}
Node makeTree(vector<Node> &vec)
{
while(vec.size() > 1)
{
Node min1 = vec[0];
Node min2 = vec[1];
vec.erase(vec.begin() + 0);
vec.erase(vec.begin() + 0);
Node* temp = new Node(min1.frequency + min2.frequency);
temp->left = &min2;
temp->right = &min1;
vec.push_back(*temp);
delete temp;
sort(vec);
}
return vec[0];
}
void traverse(Node *nde)
{
if(nde == NULL)
return;
else
{
traverse(nde->left);
cout << nde->frequency << endl;
traverse(nde->right);
}
}
Here is my node class:
#ifndef NODE_H
#define NODE_H
class Node
{
public:
Node(char ch);
Node(int freq);
char letter;
int frequency;
Node* left;
Node* right;
};
#endif
And its source file:
#include "Node.h"
#include <iostream>
using namespace std;
Node::Node(char ch)
: letter(ch)
{
left = NULL;
right = NULL;
}
Node::Node(int freq)
: frequency(freq)
{
left = NULL;
right = NULL;
letter = NULL;
}
It compiles just fine but I am just getting through the kinks. If anyone can provide insight, that would be much appreciated! Thanks!