Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying a binary tree in graphic form in an applet  RSS feed

 
Rick Bass
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a binary tree and I need to display it in an applet.
I was going to drawString for the root and then for the next line I was going to drawLine going left, skip some space, and draw a line to the right. Then on the next line I drawString for each child of the root. and so on so forth. But if the child is null...no line or string is drawn. I'm thinking I got to do a breadth first traversal, put each name in a queue, dequeue it and draw the necessary things. However, I can't get anywhere with drawing this tree.

Please help.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Since there seems to be nothing applet-specific in the question, I'll move it to the AWT/Swing/GUI forum.
[ March 30, 2007: Message edited by: Ulf Dittmer ]
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again:-

Welcome to the Ranch.

You are drawing a tree; you are presumably aware of the Swing classes used to draw a tree? Have a look in the Java tutorials, here, and see whether that is any help.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some GraphLayout examples in the demo folder of your sdk that show one way of
showing a tree graph (sdk1.5/demo/plugin/applets/GraphLayout). There may be a number of
ways of setting this up. One way I did this was to use the tree nodes to make up a set of
graphNodes to be used for drawing. Make up a new set of graphNodes for each
addition/removal of a node in your tree. Each of these graphNodes can keep an x, y
location for itself and for each child. Use these to draw the graphNode and a line to each
child as you move through the graphNodes. Use your binary tree methods to help figure out
the location of each graphNode relative to its parent node on up to the tree root node. Then
you can run through the graphNodes and calculate the width and height of the tree graph
and use this to locate/center (and draw) the tree in your gui graphic component and to set
the scrollbars on its parent JScrollPane.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!