traversing binary tree

This is a discussion on traversing binary tree within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering for huffman coding, how should I implement the binary representation of each character. (I want to ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    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,

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    18
    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.
    Last edited by syneii; 12-07-2010 at 08:29 AM.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    Thanks,
    I finally got that sorted out now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21