Binary tree

This is a discussion on Binary tree within the C Programming forums, part of the General Programming Boards category; I have a project to do that requires me to print out a Binary tree i have got it to ...

  1. #1
    cis
    cis is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    15

    Question Binary tree

    I have a project to do that requires me to print out a Binary tree i have got it to print the three orders ( inorder, preorder, and postorder ) which is really easy to do but now I got to print out the actual tree itself. I have no clue where to start and i was woundering if someone could help me.

    Example output:
    ---------------------------------------------
    d
    / \
    b f
    / \ / \
    a c e g
    ----------------------------------------------
    (or)
    ----------------------------------------------
    g
    f
    e
    d
    c
    b
    a

  2. #2
    cis
    cis is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    15

    that messed up

    the second tree was suppose to be the same as the first but it just printed to the left side

    I should of used preview

    Anyway can someone give me some help

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    void printnode ( char c, int h )
    {
      int i;
      for ( i = 0; i < h; i++ )
        printf ( " " );
      printf ( "%c\n", c );
    }
    
    void show ( struct node x, int h )
    {
      if ( x == NULL ) {
        printnode ( '*', h );
        return;
      }
      show ( x->r, h + 1 );
      printnode ( x->item, h );
      show ( x->l, h + 1 );
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Do a level order traversal by using a queue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  2. 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
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

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