4 Replies - 10815 Views - Last Post: 31 March 2010 - 08:56 AM Rate Topic: -----

#1 secretX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-March 10

Print Binary Tree

Posted 08 March 2010 - 10:03 AM

Hi,
need help in printing my binary tree in such format:

1
/ \
2 3

i wan e output to be print like this in java. Does anyone noe how?
Is This A Good Question/Topic? 0
  • +

Replies To: Print Binary Tree

#2 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: Print Binary Tree

Posted 08 March 2010 - 10:16 AM

You'll run into problems with deeper trees expanding both ways(also if numbers are large)

I'd try something like this..
Write a function to return the depth from the current tree.

Then write a recursive function that prints...
//print tabs equal to the current depth and a space
// print the node
if(depth is greater then 0)
  // print a new line and tabs equal to depth and "/\"
  // make recursive call to print function on the sub trees



Something like that should work, most of the problems have to do with getting the formatting
right so the output looks neat and uniform.
Was This Post Helpful? 0
  • +
  • -

#3 secretX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-March 10

Re: Print Binary Tree

Posted 08 March 2010 - 12:19 PM

Thanks for ur reply but can u describe more on how to do it?
What if i am using an int array to store the value how am i going to print it?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Print Binary Tree

Posted 08 March 2010 - 05:03 PM

You're using an int[] to model a Tree? This defeats the whole purpose of using a binary tree, which is to easily order data and allow for such easy ordering upon insertion of new elements. As for displaying your output, you would do best to paint it in a GUI or use the JTree class to model it. Note that the JTree class will display more like an outline than a top down tree as you are looking for.

For more information on JTree, check out the API:
http://java.sun.com/...wing/JTree.html
Was This Post Helpful? 0
  • +
  • -

#5 BrainTuner  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 26-February 10

Re: Print Binary Tree

Posted 31 March 2010 - 08:56 AM

View Postmacosxnerd101, on 08 March 2010 - 04:03 PM, said:

You're using an int[] to model a Tree? This defeats the whole purpose of using a binary tree, which is to easily order data and allow for such easy ordering upon insertion of new elements. As for displaying your output, you would do best to paint it in a GUI or use the JTree class to model it. Note that the JTree class will display more like an outline than a top down tree as you are looking for.

For more information on JTree, check out the API:
http://java.sun.com/...wing/JTree.html


I also have the same problem,
how about if my input is 4 5 2 1 8 9 2 3
the expected output should be
               4                  --> to go to 4, needs 15 spaces
       5               2          --> to go to 5, needs 7 spaces (15 mod 2), then go to 2, needs 15 spaces
   1       8       9       2      --> to go to 1, needs 3 spaces (7 mod 2),then go to 8 needs 7 spaces,etc
 3                                --> to go to 3, needs 1 spaces (3 mod 1)



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1