Thread: how to print a binary tree..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to print a binary tree..

    printing it as it is

    ??

  2. #2
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I use
    Code:
    void showtree(node *root){
    if (root){
         printf("%d\n ",root->data);
    if(root->left){
    showtree(root->left);
    }
    
    
    if(root->right){
    showtree(root->right);
    }
    }
    but its not separating them by levels

    ??

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably want to wrap ALL of the function in "if (root)", as you can not access root->left or root->right in any way if root is NULL.

    To pass show a level, you may want to pass a level along to the function and then indent based on the level (by printing level number of spaces, for example).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to connect the level of a node
    with \t and \n in printf
    ??

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    how to connect the level of a node
    with \t and \n in printf
    ??
    Like I said, you need to pass a level (and level + 1 when you recurse) in each call. You can then use varieties of spaces and such to show the level of each node by adding spaces (or tabs) according to level.

    If you actually want to draw a complete tree with arrows and such, then that would require a lot more.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I did what you said.
    I input 0 with the root and each time i call recursively i increase the level by 1.
    but how to involve this number to the printf command?
    Code:
    void showtree(node *root,level){
    if (root){
         printf("%d\n ",root->data);
    if(root->left){
    showtree(root->left,level+1);
    }
    
    
    if(root->right){
    showtree(root->right,level+1);
    }
    }
    Last edited by transgalactic2; 11-19-2008 at 02:01 PM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The simple method is to use a loop. More complicated version would be to use %*d, and pass level as the argument before data. %* means "use the next argument as width".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree
    By Ideswa in forum C Programming
    Replies: 12
    Last Post: 10-25-2007, 12:24 PM
  2. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  3. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  4. print a binary tree!
    By basilis in forum C Programming
    Replies: 1
    Last Post: 08-26-2001, 10:36 AM
  5. print a binary tree!
    By basilis in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-26-2001, 07:40 AM