-
traversing binary tree
Hi,
I was wondering for huffman coding, how should I implement the binary representation of each character. (I want to store it in a vector <string>)
Originally, I had a string and I appended a "1" or "0" depending on which branch I took, but the problem was that I initialized my string in my recursive function so the string automatically resets itself to nothing.
on that note, I also would like to count up all the node. I'm thinking it is similar to the initial problem.
Any Suggestions?
Thanks,
-
Edit; my example wasn't for binary tree's, so I re-wrote the example.
You want to write a function that defines the recursive traversal, then invoke that function within another method. Below is a minimalistic representation of what I'm talking about, to help you understand what I mean;
Code:
int traverse(node *n, int depth)
{
depth++;
if (n->right != NULL) { traverse(n->right, depth); }
if (n->left != NULL) { traverse(n->left, depth); }
return 1;
}
void crawl_binarytree()
{
// initiate stuff here, then pass to params + initiate recursive traversal
int res = traverse(rootnode, 0);
}
When I write software to do this kind of stuff I'll design a class where I implement the traversal in private and then create a public method which actually calls the traversal and manages/returns results of the traversal.
-
Thanks,
I finally got that sorted out now