• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Displaying a binary tree in graphic form in an applet

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic