FAQ Printing a binary tree?? (C)

This is a discussion on FAQ Printing a binary tree?? (C) within the FAQ Board forums, part of the General Programming Boards category; I am currently working on a function that accepts the root for a binary tree and prints out the tree ...

  1. #1
    Unregistered
    Guest

    Question Printing a binary tree??

    I am currently working on a function that accepts the root for a binary tree and prints out the tree structure with the numbers. I want to know where do I begin. Do I need to count the height of the tree or what... I need some help....

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > I want to know where do I begin.
    At the root of the tree

    > Do I need to count the height of the tree or what
    That would depend on how you choose to represent the tree when you've printed it.

    If its just a text dump, no formatting is required

    But if it's a nice graphical thing, then more work is required.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Unregistered
    Guest
    I want a graphically output:
    example:
    100
    50
    40
    25 30
    12
    ......and such

  4. #4
    Unregistered
    Guest
    that didnt come out right....cuz I guess this forum doesnt display the spaces. I want a graphically tree with the corresponding numbers.

  5. #5
    Unregistered
    Guest

    Question

    i still need some help???what this printing of a tree structure.....

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Still not too sure what your asking, but here goes...

    1) The generic treeprinting program
    Code:
    void printtree(node * tree)
    {
     if (tree != NULL)
     {
      print (tree -> info); // Not a real command
      printtree (tree -> left);
      printtree (tree -> right);
     }
    }
    2) If you want a graphical tree, might I suggest something using ASCII characters 179, 192, and 195? You know, just do those to draw lines from nodes to their children, with each node being on a different line. It's not spaced out as a tree diagram usually is, but it does have connectivity lines, which is the importatnt thing, personally.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree Search
    By C++Newbie in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2011, 01:17 AM
  2. determine if tree is strictly binary bst
    By ashir30000 in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2010, 12:51 PM
  3. Binary Search Tree - one last question
    By tms43 in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2006, 02:58 AM
  4. 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
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM

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