gpt4 book ai didi

java - java中需要的BST图形表示

转载 作者:行者123 更新时间:2023-12-02 06:02:53 26 4
gpt4 key购买 nike

我正在使用Model–view–controller设计模式,我需要一个用于我的 BST/AVL 模型的 UI 查看器,我已经开发了模型和 Controller 逻辑,我只需要查看器,我现在不需要处理图形,它会如果有人有一个 Viewer 类,该类的方法可以获取树的根并绘制它,而不需要高级动画或效果

类似的东西

SimpleBSTreeViewer.draw(myTree.getRoot());

树的节点结构

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 ...
}

这将有助于集中精力编写逻辑部分,

我搜索并找到了其他代表树的java小程序,但它们在同一个类中具有模型逻辑和 View ,我正在模型- View - Controller 模式(MVC模式)中工作,最好将模型与 View 分开尽可能地,我只想要一个适合我的模型的查看器

-注意:这是我需要的示例(我已经使用 JTree,但它并不令人满意。)

enter image description here

最佳答案

  • 获取 Jgraphx库并从 HelloWorld 开始我编写此类的示例

  • 使用this计算节点坐标的问题。

    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 a tree starting from this root
    *
    * @param root
    * @param depth
    * number of nodes to the root (including root)
    * @param index
    * index of node in this level (leftChildIndex = parentIndex * 2
    * - 1) and (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 for 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 for right child

    if (leftChildVertex != null) { // edge
    graph.insertEdge(parent, null, "L", rootVertex, leftChildVertex,
    "startArrow=none;endArrow=none;strokeWidth=1;strokeColor=green");
    }

    return rootVertex;

    }

    /**
    * Redraw the whole tree
    *
    * @param root
    * the root of tree to be 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);

    } finally {
    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();
    for (int i = 0; i < 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);

    }



    }
  • 只需修改此类即可获取您的树类

  • 非常感谢NeplatnyUdaj !

Viewing AVL tree

关于java - java中需要的BST图形表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22535847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com