Thread: need help with printing out a tree

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    need help with printing out a tree

    * Each node should be printed on a separate line;
    * the left subtree should be printed after the root;
    * The right subtree should be printed before the root;
    * Tthe root should not be indented, the keys in its subtrees should be intended 2 spaces, the keys in their subtrees 4 spaces, and so on.
    Code:
    For example, the complete tree containing {1,2,3,4,5,6} would be printed like this: 
    
      6
        5
    4
        3
      2
        1

    if you tilt the above diagram, you can see a tree with 4 being the root and 1,3,5 being the leaves....

    should i start printing the largest number first?
    but the values of keys in the tree does not need to be always 1 less than the previous one...so i can;t just search for the next key that is 1 less than the previous one...
    confuesd...need some help, thx >.<

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    You have to print the values in the tree by traversing your tree.I think you have inserted the values to a tree in the tree format.You simply follow the same way to print the values.While printing the values,first print right subtree until it reaches null,then root and then you print left sub tree.If you face any problem,revert me.
    Last edited by vivekraj; 03-02-2010 at 10:36 PM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    57
    Actually, you are trying to print the tree in reverse of inorder traversal.

    In order traversal prints the tree in sorting order and it follow LVR (left, visit, Right )

    But you are trying to print reverse sorting order so you are following RVL ( Right , visit , Left ).

    No need to worry about largest number or something. If you are using the Binary Tree it always have the left node is less than its parent and right node is greater than its parent.

    I just gave you sample algorithm.
    Code:
    void printtree(root)
    {
      printtree(root->right);
      print node; 
      printtree(root->left);
    }
    For indenting you can use the depth of the tree. If the depth of the node is 0 no need to print any space. If the node depth is 1 you can print 2 spaces. Like that you can use depth.
    Last edited by sganesh; 03-03-2010 at 01:29 AM.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Quote Originally Posted by sganesh View Post
    Actually, you are trying to print the tree in reverse of inorder traversal.

    In order traversal prints the tree in sorting order and it follow LVR (left, visit, Right )

    But you are trying to print reverse sorting order so you are following RVL ( Right , visit , Left ).

    No need to worry about largest number or something. If you are using the Binary Tree it always have the left node is less than its parent and right node is greater than its parent.

    I just gave you sample algorithm.
    Code:
    void printtree(root)
    {
      inorder(root->right);
      print node; 
      inorder(root->left);
    }
    For indenting you can use the depth of the tree. If the depth of the node is 0 no need to print any space. If the node depth is 1 you can print 2 spaces. Like that you can use depth.
    I think I understand what you mean...
    But now I am looking at this tree from wikipedia...
    Binary search tree - Wikipedia, the free encyclopedia
    When I reach 14, I print out 14
    but how do I get back to 13?
    and even i get to 13, how do i get to 10?

    do i have to make a function something like "delete" to delete 14 and 13?
    so that when i reach 10, 10->right will be null?

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    I didn't understand what you have explained.Did you follow the algorithm mentioned by ganesh?
    Tell me whether you stored all the values what you have mentioned in tree format or not.You can print the values in the tree if you insert those values in correct order.If not,you can't get it.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Quote Originally Posted by vivekraj View Post
    I didn't understand what you have explained.Did you follow the algorithm mentioned by ganesh?
    Tell me whether you stored all the values what you have mentioned in tree format or not.You can print the values in the tree if you insert those values in correct order.If not,you can't get it.
    my question is...when i am trying to print root->right, how do i print root->right->left as well...?

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: need help with printing out a tree

    print tree is a recursive process. It will take one node then it will apply traversal method.(In your case Right, visit, Left).

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    As ganesh mentioned,its a recursive process.When you call the print function with root->right as an argument.Then in the print function,you will print the right node of the current node which is

    root->right->right ,then you print the current node which is root->right and then left node which is root->right->left.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  3. 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
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM