所以我实际上有两个问题。 首先,我可以指定顶点显示的顺序吗?我的意思是,当我试图想象顶点的直线时,有时它会先到最后,有时会持续到第一个。这是我的代码片段。还有什么更好的方法来制作直线而不是将垂直尺寸改为低点?
Tree<GraphNode, String> g1 = q.transformToGraph();
Layout<GraphNode, String> layout = new ISOMLayout<GraphNode, String>(g1);
layout.setSize(new Dimension(1600, 25));
VisualizationViewer<GraphNode, String> vv = new VisualizationViewer<GraphNode, String>(layout);
vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
VertexLabelAsShapeRenderer<GraphNode, String> vlasr = new VertexLabelAsShapeRenderer<GraphNode, String>(vv.getRenderContext());
vv.getRenderContext().setVertexShapeTransformer(vlasr);
vv.getRenderContext().setVertexLabelTransformer(
new ChainedTransformer<GraphNode, String>(
new Transformer[]{new MyLabeller()}));
vv.getRenderer().setVertexLabelRenderer(vlasr);
return vv;
transformToGraph函数返回DirectedSparseGraph。
还有什么办法可以不编写我自己的布局来制作漂亮的二叉树可视化。特别是BST,因为TreeLayout使边缘长度不同并且交换了一些边缘的边(左边是右边而另一边是)
Tree<GraphNode, String> g1 = (DelegateTree) q.transformToGraph();
Layout<GraphNode, String> layout = new DynamicTreeLayout<GraphNode, String>(g1, 100, 100);
layout.setSize(new Dimension(1600, 100)); VisualizationViewer<GraphNode, String> vv = new VisualizationViewer<GraphNode, String>(layout);
vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
VertexLabelAsShapeRenderer<GraphNode, String> vlasr = new VertexLabelAsShapeRenderer<GraphNode, String>(vv.getRenderContext());
vv.getRenderContext().setVertexShapeTransformer(vlasr);
vv.getRenderContext().setVertexLabelTransformer(
new ChainedTransformer<GraphNode, String>(
new Transformer[]{new MyLabeller()}));
vv.getRenderer().setVertexLabelRenderer(vlasr);
return vv;
这里transformToGraph将我的BST更改为DelegateTree。
以下是
@Override
public DelegateTree<GraphNode, String> transformToGraph() {
DelegateTree<GraphNode, String> graph = new DelegateTree<GraphNode, String>();
if (root == null) {
graph.setRoot(null);
return graph;
}
GraphNode rootNode = new GraphNode(root.getValue());
graph.setRoot(rootNode);
if (root.getLeft() == root.getRight()) {
return graph;
}
addNodeToGraph(rootNode, root.getLeft(), graph);
addNodeToGraph(rootNode, root.getRight(), graph);
return graph;
}
protected void addNodeToGraph(GraphNode parent, TreeNode node, DelegateTree<GraphNode, String> graph) {
if (node == null) {
return;
}
GraphNode curr = new GraphNode(node.getValue());
graph.addChild(parent.getValue() + node.getValue(), parent, curr);
addNodeToGraph(curr, node.getLeft(), graph);
addNodeToGraph(curr, node.getRight(), graph);
}
答案 0 :(得分:0)
图中的节点或节点的邻居/后继者/前任者的迭代次序由图实现确定。有些实现强制执行一致的排序,有些则不执行。一个实现:http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/graph/DirectedOrderedSparseMultigraph.html
如果你想要一个折线图,你很可能会想要编写自己的Layout实现;不过应该很容易。 (一般图形的良好布局算法要困难得多,但对于一个简单的链条,它应该基本上是微不足道的。)