*
A friendly place for programming greenhorns!
Big Moose Saloon
Search

Java FAQ

Recent Topics

Flagged Topics

Hot Topics

Zero Replies
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Swing / AWT / SWT
Author
Draw binary tree structure
sachin upatil
Greenhorn
Joined: May 17, 2010
Posts: 29
posted
Nov 16, 2011 22:46:13
0
Hello ,
I have created a huffman tree with Node using Comparable interface. working fine.
public class Node { char element; // character int weight; // weight of tree Node left; // left subtree Node right; // right subtree String code = ""; // The code of this node from the root int x = 0; int y = 0; }
Also i calculated the depth of tree. and i can travel though tree using following method.
public void printTree(Node node) { if (node != null) { if(node.left != null){printInOrder(node.left);} System.out.println(node.element); if(node.right!= null){printInOrder(node.right);} }
but now i want to display this tree on Jpanel like binary tree as follow :
A
/ \
/ \
B C
How i can achieve this ???
Sugantha Jeevankumar
Ranch Hand
Joined: Jun 06, 2007
Posts: 93
posted
Nov 17, 2011 03:57:41
0
Have you tried if JTree or swingx.JXTree suits your needs.. or can be customized?
SCJP 5.0
sachin upatil
Greenhorn
Joined: May 17, 2010
Posts: 29
posted
Nov 20, 2011 21:55:26
0
i did it. now i can draw binary tree using following method
/** Display a subtree rooted at position (x, y) */ private void displayTree(Graphics g, Node root, int x, int y, int hGap) { // Display the root g.drawOval(x  radius, y  radius, 2 * radius, 2 * radius); if(root.element != 0){ g.drawString(root.weight + "", x  6, y + 4); g.setColor(Color.red); g.drawString(root.element + "", x  6, y + 35); }else{ g.drawString(root.weight + "", x  6, y + 4); g.setColor(Color.red); } if (root.left != null) { // Draw a line to the left node g.setColor(Color.black); leftChild(g, x  hGap, y + vGap, x, y); //Draw the left subtree displayTree(g, root.left, x  hGap, y + vGap, hGap / 2); } if (root.right != null) { // Draw a line to the right node g.setColor(Color.black); rightChild(g, x + hGap, y + vGap, x, y); // Draw the right subtree recursively displayTree(g, root.right, x + hGap, y + vGap, hGap / 2); } } /* Draw left child */ private void leftChild(Graphics g, int x1, int y1, int x2, int y2) { double d = Math.sqrt(vGap * vGap + (x2  x1) * (x2  x1)); int x11 = (int)(x1 + radius * (x2  x1) / d); int y11 = (int)(y1  radius * vGap / d); int x21 = (int)(x2  radius * (x2  x1) / d); int y21 = (int)(y2 + radius * vGap / d); g.drawLine(x11, y11, x21, y21); g.drawString("0", ((x21+x11)20)/2, (y21+y11)/2); } /*Draw right child */ private void rightChild(Graphics g, int x1, int y1, int x2, int y2) { double d = Math.sqrt(vGap * vGap + (x2  x1) * (x2  x1)); int x11 = (int)(x1  radius * (x1  x2) / d); int y11 = (int)(y1  radius * vGap / d); int x21 = (int)(x2 + radius * (x1  x2) / d); int y21 = (int)(y2 + radius * vGap / d); g.drawLine(x11, y11, x21, y21); g.drawString("1", (x21+x11)/2, (y21+y11)/2); } //
But now i want to display this tree node by node using Thread like animation.
sachin upatil
Greenhorn
Joined: May 17, 2010
Posts: 29
posted
Nov 23, 2011 00:04:19
0
I tried it using for loop but it not working. it's showing complete tree.
which is any other option i can try ?
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
subject: Draw binary tree structure
Similar Threads
scapegoat tree rebuilding...please help!
Binary Search Tree
Binary search tree questions...
Binary Tree remove node algorithms
Insert numbers in a binary search tree
All times are in JavaRanch time: GMT6 in summer, GMT7 in winter