This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Displaying a binary tree in graphic form in an applet Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Displaying a binary tree in graphic form in an applet" Watch "Displaying a binary tree in graphic form in an applet" New topic
Author

Displaying a binary tree in graphic form in an applet

Rick Bass
Greenhorn

Joined: Mar 30, 2007
Posts: 1
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
Marshal

Joined: Mar 22, 2005
Posts: 35249
    
    7
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 ]

Android appsImageJ pluginsJava web charts
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
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

Joined: Jan 14, 2004
Posts: 1535
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying a binary tree in graphic form in an applet
 
Similar Threads
How does a PriorityQueue work ?
need help with binary search tree
Graph representation
Building a query for Tree
Path of TreeSelectionEvent