CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2001
    Posts
    8

    Binary Tree Printing

    Hi,
    I need to print a binary tree
    1. in the order of its level ( root, left & right child, children of the left & right nodes, its children & so on )
    1, 2, 3, 4, 5, 6,7,8 .... where 2 & 3 are the children of 1, 4 & 5 are the children of 2, 6 & 7 are the children of 3 & so on...


    2. print them in the following manner
    1
    2
    3

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Does the tree contain OBJECTS or Integers?

  3. #3
    Join Date
    Feb 2001
    Posts
    8
    The tree contains simple integers

  4. #4
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    You have a strange binary tree. Normally it is left, middle then right but you want it printed as middle, left right. Oh well - we all have our reasons. Assuming the structure is
    Code:
    struct bintree
    {
        struct bintree* left;
        struct bintree* right;
        int val;
    };
    Printing it is just
    Code:
    void printtree (struct bintree* node)
    {
         cout << node->val << endl;
        if (node->left != 0) printree (node->left);
        if (node->right != 0) printree (node->right);
    }
    
    or if you are likely to start with a null node then
    void printtree (struct bintree* node)
    {
        if (node != 0)
        {
            cout << node->val << endl;
            printtree (node->left);
            printtree (node->right);
        }
    }
    The latter looks neater but nests more deeply at runtime. If you wanted it as left, value, right, just move the printree of the left before the cout.
    Last edited by cup; December 6th, 2002 at 05:12 PM.
    Succinct is verbose for terse

  5. #5
    Join Date
    Sep 2002
    Posts
    1,747
    Are you trying to print them like
    Code:
    1
         2
              4
              5
         3
              6
              7
    I was curious since you posted twice, and maybe weren't familiar as to how to get the tabbing to show up...
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by galathaea
    I was curious since you posted twice
    Sorry, no that was me. While writing the post he accidentally posted the half-done post and TheCPUWizard replied to that. I then merged the full question with this thread. But by deleting the first (uncomplete) post, I would delete the whole thread. Ok, I'm not trying to explain my life here, just that it's a technical glitch
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured