Thread: insert into binary tree question..

  1. #31
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    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
    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
    Join Date
    Oct 2008
    I fixed it:
    void showtree(node *root){
    if (root){
         printf("%d\n ",root->data);
    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?

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, 02:02 PM
  2. Binary Tree
    By Ideswa in forum C Programming
    Replies: 12
    Last Post: 10-25-2007, 01:24 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. binary search tree help
    By noob2c in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2003, 02:51 PM
Website Security Test