Reputation: 16081
I am trying to create binary trees in JUNG (a graph package for Java). However, I cannot get manage to accomplish this.
Here is my source:
Tree tree1 = new OrderedKAryTree<String, Integer>(2);
tree1.addVertex("v0");
tree1.addEdge(1, "v0", "v1");
tree1.addEdge(2, "v0", "v2");
Here is the exception I get:
java.lang.IllegalArgumentException: 'index' must be in [0, [order-1]]
In the documentation, I see one of the overloaded methods addEdge()
takes an integer parameter called index as the 4th argument. However, there appears to be no implementation of that on my machine.
This should create a binary tree of height 1. I've tried adding vertices before edges as well:
tree1.addVertex("v0");
tree1.addVertex("v1");
tree1.addVertex("v2");
tree1.addEdge(1, "v0", "v1");
tree1.addEdge(2, "v0", "v2");
Here is the exception I get:
java.lang.UnsupportedOperationException:
Unless you are setting the root, use addEdge() or addChild()
The weird part is, there is no addChild()
method in the documentation (I assuming addChild()
was part of an older version of the package and was not removed in the latest update)
Creating a small binary tree should not be hard! Can someone help?
Upvotes: 2
Views: 1037
Reputation: 11
for tree you need to use setRoot and addChild function
DelegateTree<String, String> tree = new DelegateTree<String,String>();
tree.setRoot("A");
tree.addChild("A-B1", "A", "B1");
tree.addChild("A-B2", "A", "B2");
BasicVisualizationServer<String, String> vs = new BasicVisualizationServer<String, String>(
new FRLayout<String,String>(tree), new Dimension(1100, 640));
Transformer<String,Paint> vertexPaint = new Transformer<String,Paint>() {
public Paint transform(String i) {
return Color.GRAY;
}
};
RenderContext<String, String> renderContext = vs.getRenderContext();
renderContext.setVertexFillPaintTransformer(vertexPaint);
Transformer<String, String> transformer = new ToStringLabeller<String>();
renderContext.setEdgeLabelTransformer(transformer);
Transformer<String, String> vertexTransformer = new ToStringLabeller<String>();
renderContext.setVertexLabelTransformer(vertexTransformer);
vs.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
JFrame frame = new JFrame();
frame.getContentPane().add(vs);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
Upvotes: 1