never mind i solved this today and it works perfectly 
edit: my question was if i have a tree that represents huffman code ( google what the tree is like ), how could i get the huffman code corresponding to the chacters stored in the leaf nodes. what i did was make it so that each node knew its parent. this is my code :
which calls this function
edit: my question was if i have a tree that represents huffman code ( google what the tree is like ), how could i get the huffman code corresponding to the chacters stored in the leaf nodes. what i did was make it so that each node knew its parent. this is my code :
Code:
void Huffman::makeHuffCode(TreeNode *ptr)
{
//standard tree traversal
if(!ptr){
return;
}
makeHuffCode(ptr->left);
//dont print the huffman code for blank nodes
string str = getBitString(ptr);
if( isalpha(ptr->letter) ){
cout << ptr->letter << " with a frequence of " << ptr->freq << ", corresponds to : " << str << endl;
}
makeHuffCode(ptr->right);
}
which calls this function
Code:
//this function returns the bit string to a corresponding node
string getBitString(TreeNode *ptr){
string str;
//keep stepping up until you are at the root
while(ptr->parent){
if( ptr->parent){
//if this node is a left child of its parent
if(ptr->parent->left == ptr ){
str.push_back('0');
}
//if this node is a right child of its parent
else if(ptr->parent->right == ptr){
str.push_back('1');
}
ptr=ptr->parent;
}
}
//reverse str
string toRet;
for( int i = 0; i < str.size(); i++){
toRet.push_back(str[str.size()-1-i]);
}
return toRet;
}