here's the simplified code:
Code:
struct node {
node* one;
node* zero;
node* parent;
char value; //letter
int amount; //number of letters
node (int x) {
one=NULL; //for some reason this changes as it progresses through the code
zero=NULL;//ditto
value=x;
amount=1;
parent=NULL;
}
void add(); //just does amount++
};
class huffman_tree {
public:
vector<node*> node_list; //an array of nodes. once they get linked into a tree everything will be under node_list[0]
vector<int> code_temp; // a fairly global solution, will change later
int search(int x); //if the character 'x' is found in the node_list, return its location
void add(int x); //add letter 'x' onto the node_list
void bubblesort(); //sort node_list by frequency (ie, by amount)
void sort_it(); //calls bubblesort for now, will change later to better sorting method
void huff() {
//while there's still two nodes left:
//call join(), join node_list[0] and node_list[1]
//remove two nodes from beginning, replace with their parent
//locate the right space to insert the new parent node
//move the parent node there
}
void join(node* a, node* b) //make a node, put two other nodes under it
{
//call add function to create new node with value of 1 to distinguish it from letter nodes
//assign temp. 'parent' pointer to newly created node
//point the two nodes' parent pointers at it
//point ther parent's two children pointers at the two nodes
//make the parent node's amount the sum of the children nodes
}
void print_tree() ; //debugging purposes
void lookup() //create lookup table
{
//call get_letters(top node)
//then print the code
}
int get_letters(node* x) {
//if x's zero pointer is pointing to something, push a zero into the buffer
//if x's one pointer is pointing to something, push a one into the buffer
//in either case, call get_letters for a node under this one
}
};
int main()
{
huffman_tree tree;
string temp="yabba dabba doo";
char letter;
while (temp.length()) { //add 'temp' into huffman list
letter=temp.at(0);
temp.erase(0,1);
tree.add(letter);
}
tree.sort_it(); //sort tree by 'amount' value: 0 is smallest
tree.huff(); // organize into huffman table
tree.print_tree(); //right now it does practicly nothing
tree.lookup(); // organize tree into lookup table
}
hope it helps: