Huffman Tree

I am doing huffman tree. the instruction is if you go from the left subtree, you have to add "0" if you go from the right subtree you have to add "1". I am trying to recursive function to add "0" or "1" to my string. I was just wondering if you can really do this with recursive function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string HuffmanTree::add(BSTNode* current, char textchar)
{
    string temp = " ";
    string left = " ";
    string right = " ";
    if (current == NULL)
        return " ";
    
    left = add(current->left, textchar);
    right = add(current->right, textchar);
    if (current->data.frequency != textchar)
    {
        
    }
    return "";
}
Yes. It can also be done iteratively.
Hi Helios, I think I can do this iteratively, but I'm kind of wondering how would you implement it recursively. It been trying it for 3 days, I couldn't do it. Any hint would help
I mean the tail recursion wouldn't work, would it
You can do it with tail recursion, sure. That will probably require you changing the prototype of your function, though.

Your code is all wrong, logically speaking. You should never go down both right and left links. And why would you compare frequency to text?
Topic archived. No new replies allowed.