swing - BST graphical representation needed in java -
i using model–view–controller design pattern, need ui viewer bst/avl models ,i developed model , controller logics, need viewer, not need deal graphics right now, useful if has viewer class method takes root of tree , draws it, no need advanced animation or effects
something like
simplebstreeviewer.draw(mytree.getroot());
node structure of tree
public class nodebst <t extends comparable<t>> { private t data; private nodebst<t> left; private nodebst<t> right; private nodebst<t> parent; //other methods go here ... }
this helpful focus of coding logic part,
i have searched , found other java applets represents trees have model-logic , view in same class , working in model-view-controller pattern (mvc pattern) , better separate model view as possible , want viewer model
-note: sample of need (i use jtree , not satisfactory.)
get jgraphx library , starting
helloworld
in examples coded classusing this question calculate coordinates of nodes.
package algorithms.searching.bst; import java.util.random; import javax.swing.jframe; import log.logger; import algorithms.searching.bst.avl.avl; import com.mxgraph.swing.mxgraphcomponent; import com.mxgraph.view.mxgraph; @suppresswarnings("serial") public class treeviewer<t extends comparable<t>> extends jframe { static private int canvas_height = 600; static private int canvas_width = 1000; private int rooty = 10; private int node_size = 25; private int row_height = 50; mxgraph graph = new mxgraph(); object parent = graph.getdefaultparent(); /** * draws tree starting root * * @param root * @param depth * number of nodes root (including root) * @param index * index of node in level (leftchildindex = parentindex * 2 * - 1) , (rightchildindex = parentindex * 2) * @return */ public object drawtree(nodebst<t> root, int depth, int index) { if (root == null) { return null; } // draw root /* * leftchildindex = parentindex * 2 - 1 * rightchildindex = parentindex *2 * * * x = index * canvaswidth / (2^depth + 1) * * y = depth * canvasheight / treedepth */ int myx = (int) ((canvas_width * (index)) / (math.pow(2, depth - 1) + 1)); object rootvertex = graph.insertvertex(parent, null, root.getdata(), myx, depth * row_height + rooty, node_size, node_size); logger.log("new x coordinate=" + myx); // recurse right child object rightchildvertex = drawtree(root.getright(), depth + 1, index * 2); if (rightchildvertex != null) {// edge graph.insertedge(parent, null, "r", rootvertex, rightchildvertex, "startarrow=none;endarrow=none;strokewidth=1;strokecolor=green"); } object leftchildvertex = drawtree(root.getleft(), depth + 1, index * 2 - 1); // recurse right child if (leftchildvertex != null) { // edge graph.insertedge(parent, null, "l", rootvertex, leftchildvertex, "startarrow=none;endarrow=none;strokewidth=1;strokecolor=green"); } return rootvertex; } /** * redraw whole tree * * @param root * root of tree drawn */ public void update(nodebst<t> root) { graph.getmodel().beginupdate(); try { object[] cells = graph.getchildcells(parent, true, false); graph.removecells(cells, true); drawtree(root, 1, 1); } { graph.getmodel().endupdate(); } } public treeviewer(nodebst<t> root) { // super("hello, world!"); this.update(root); mxgraphcomponent graphcomponent = new mxgraphcomponent(graph); getcontentpane().add(graphcomponent); } public static void main(string[] args) { avl<integer> bst = new avl<integer>();//use avl tree class random randomgenerator = new random(); (int = 0; < 20; i++) {//try generating 20 random integers bst.insert(randomgenerator.nextint(1000)); } treeviewer<integer> mytreeviewer=new treeviewer<integer>(bst.getroot()); jframe frame = mytreeviewer; frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(canvas_width, canvas_height); frame.setvisible(true); } }
simply modify class take tree class
- thank neplatnyudaj !
Comments
Post a Comment