简体   繁体   English

如何遍历树形结构以绘制图形?

[英]How to traverse a tree structure in order to draw a graph?

I'm working with JGraphT library to draw a graph for my tree structure but the library draws a graph based on a given set of nodes and connected edges.I have read the Class "DirectedAcyclicGraph.VisitedArrayListImpl" in the library javadoc but I did not quite understand it and I'm not sure if it is what I'm looking for. 我正在使用JGraphT库为我的树结构绘制图形,但是该库基于给定的节点集和连接的边缘绘制图形。我已经阅读了javadoc库中的“ DirectedAcyclicGraph.VisitedArrayListImpl”类,但没有非常了解它,我不确定这是否是我想要的。

public class JGraphAdapterDemo
extends JApplet{

private static final long serialVersionUID = 3256444702936019250L;
private static final Color DEFAULT_BG_COLOR = Color.decode("#FAFBFF");
private static final Dimension DEFAULT_SIZE = new Dimension(530, 320);


private JGraphModelAdapter<String, DefaultEdge> jgAdapter;

/**
 * An alternative starting point for this demo, to also allow running this applet as an
 * application.
 *
 * @param args ignored.
 */
public static void main(String[] args)
{
    JGraphAdapterDemo applet = new JGraphAdapterDemo();
    applet.init();

    JFrame frame = new JFrame();
    frame.getContentPane().add(applet);
    frame.setTitle("JGraphT Adapter to JGraph Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}


  {@inheritDoc}

public void init()
{

    ListenableGraph<String, DefaultEdge> g =
        new ListenableDirectedMultigraph<>(DefaultEdge.class);

    // create a visualization using JGraph, via an adapter
    jgAdapter = new JGraphModelAdapter<>(g);

    JGraph jgraph = new JGraph(jgAdapter);

    adjustDisplaySettings(jgraph);
    getContentPane().add(jgraph);
    resize(DEFAULT_SIZE);

    String v1 = "+";
    String v2 = "3";
    String v3 = "*";
    String v4 = "4";
    String v5 = "5";

    // add some sample data (graph manipulated via JGraphT)
    g.addVertex(v1);
    g.addVertex(v2);
    g.addVertex(v3);
    g.addVertex(v4);
    g.addVertex(v5);

    g.addEdge(v1, v2);
    g.addEdge(v1, v3);
    g.addEdge(v3, v4);
    g.addEdge(v3, v5);

    positionVertexAt(v1, 130, 40);
    positionVertexAt(v2, 50, 150);
    positionVertexAt(v3, 280, 150);
    positionVertexAt(v4, 240, 250);
    positionVertexAt(v5, 400, 250);  

}

private void adjustDisplaySettings(JGraph jg)
{
    jg.setPreferredSize(DEFAULT_SIZE);

    Color c = DEFAULT_BG_COLOR;
    String colorStr = null;

    try {
        colorStr = getParameter("bgcolor");
    } catch (Exception e) {
    }

    if (colorStr != null) {
        c = Color.decode(colorStr);
    }

    jg.setBackground(c);
}

@SuppressWarnings("unchecked") 
private void positionVertexAt(Object vertex, int x, int y)
{
    DefaultGraphCell cell = jgAdapter.getVertexCell(vertex);
    AttributeMap attr = cell.getAttributes();
    Rectangle2D bounds = GraphConstants.getBounds(attr);

    Rectangle2D newBounds = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());

    GraphConstants.setBounds(attr, newBounds);


    AttributeMap cellAttr = new AttributeMap();
    cellAttr.put(cell, attr);
    jgAdapter.edit(cellAttr, null, null, null);
}

private static class ListenableDirectedMultigraph<V, E>
    extends DefaultListenableGraph<V, E>
    implements DirectedGraph<V, E>
{
    private static final long serialVersionUID = 1L;

    ListenableDirectedMultigraph(Class<E> edgeClass)
    {
        super(new DirectedMultigraph<>(edgeClass));
    }
}}

I'm using this demo code from the javadoc and I want to find a way to connect it to this tree structure. 我正在使用来自Javadoc的演示代码,并且想找到一种将其连接到此树形结构的方法。 Have anyone done something similar to this? 有人做过类似的事情吗?

This is my tree structure 这是我的树形结构

public class Tree {
    Tree left, right;
char op;
int val;
boolean isOp;

Tree(char op, Tree l, Tree r) {
this.isOp = true;
this.left = l;
this.right = r;
this.op = op;
}

Tree (int v){
this.isOp = false;
this.val = v;
}

static void toString(Tree t) {
if (t.isOp){
    toString(t.left);
    System.out.print(t.op);
    toString(t.right);
}
else
    System.out.print(t.val);
}


public void preorderIter(Tree root) {  

     if(root == null)  
         return;  

     Stack<Tree> stack = new Stack<Tree>();  
     stack.push(root);  

     while(!stack.empty()){  

         Tree n = stack.pop();  
         System.out.printf("%s ",n.op);  


         if(n.right != null){  
             stack.push(n.right);
             System.out.printf("%s",n.right);
         }  
         if(n.left != null){  
             stack.push(n.left);
             System.out.printf("%s",n.left);


         }  

     }  

 }  

I would appreciate any help 我将不胜感激任何帮助

JGraphT library is using its own graph classes to draw, so you either need to: JGraphT库使用其自己的图类进行绘制,因此您需要:

  • give up your own tree implementation and use library's classes 放弃自己的树实现并使用库的类
  • extend an appropriate tree class in library with your own implementation 用自己的实现扩展库中适当的树类
  • use your own tree as you like but when you need to draw with jgraph, copy your tree's nodes and edges to one of framework's graph implementations 根据需要使用自己的树,但是当需要使用jgraph进行绘制时,将树的节点和边复制到框架的图实现之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM