insert into binary tree question..

This is a discussion on insert into binary tree question.. within the C Programming forums, part of the General Programming Boards category; It looks like your tree insert function is alright, but your showtree function is wrong. You don't need that complex ...

  1. #31
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    It looks like your tree insert function is alright, but your showtree function is wrong. You don't need that complex if-statement in the middle. You already know that you're one a node that is not NULL (thanks to the earlier if-statement) so all you need to do is output that value (plus the recursive calls that you have correct).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right, because what that if statement does is avoid printing any node that isn't a leaf. You still need to print every element in the tree.

  3. #33
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I fixed it:
    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 i see that it prints the tree inorder traversal and not the tree structure.
    How to print the tree structure?
    Printing each level of the tree from the root level to the leaf level in descending order?

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 01:02 PM
  2. Binary Tree
    By Ideswa in forum C Programming
    Replies: 12
    Last Post: 10-25-2007, 12:24 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. binary search tree help
    By noob2c in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2003, 01:51 PM

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