我面临着可视化这个字符串的问题:
"=IF(A2=1;0;IF(D2=D3;IF(C2=1;TRUE;FALSE);4))";
如您所见,一般语法类似于 excel 公式,因此我有 IF(TEST; TRUE; FALSE)
我的问题是我想用库 JUNG2 以二叉搜索树格式可视化这个字符串.您可以在下面看到树的外观示例:
这是一些可视化当前顶点的代码。
public class SimpleGraphView {
Graph<Integer, String> g;
Graph<String, String> n;
/** Creates a new instance of SimpleGraphView */
String text = "=IF(A2=1;0;IF(D2=D3;IF(C2=1;TRUE;FALSE);4))";
public SimpleGraphView() {
n = new SparseMultigraph<String, String>();
String str = text;
String delim = ";";
StringTokenizer tok = new StringTokenizer(str, delim, true);
text = text.replace("(", " ");
String s = text.replace(")", " ");
String[] r = s.split(";");
for (int i = 0; i < r.length; i++) {
//Vertex
if(r.equals("=IF(")) {
n.addVertex(r[i].toString());
}
if(i % 2==0){
n.addVertex(r[i].toString());
} else {
n.addVertex(r[i].toString());
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SimpleGraphView sgv = new SimpleGraphView(); // Creates the graph...
// Layout<V, E>, VisualizationViewer<V,E>
Layout<Integer, String> layout = new CircleLayout(sgv.n);
layout.setSize(new Dimension(300,300));
VisualizationViewer<Integer,String> vv = new VisualizationViewer<Integer,String>(layout);
vv.setPreferredSize(new Dimension(350,350));
// Show vertex and edge labels
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
// Create our "custom" mouse here. We start with a PluggableGraphMouse
// Then add the plugins you desire.
PluggableGraphMouse gm = new PluggableGraphMouse();
gm.add(new TranslatingGraphMousePlugin(MouseEvent.BUTTON1_MASK));
gm.add(new ScalingGraphMousePlugin(new CrossoverScalingControl(), 0, 1.1f, 0.9f));
vv.setGraphMouse(gm);
JFrame frame = new JFrame("Interactive Graph View 3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
}
}
我可以渲染数组 r
中字符串之外的所有顶点。我的问题是我不知道如何将所有这些顶点与正确的边连接起来。有什么建议,如何将所有顶点与正确的边连接起来?
非常感谢您的回答!
最佳答案
最简单的方法是以我认为的不同方式拆分文本。请注意,我用 addEdge
和 addVertex
替换了图形的方法:
String[] operands = text.substring(1, text.length()).split("[;()]+");
int numIfs = operands.length / 3; // actually (operands.length - 1) / 3 but int division makes it the same
String[] nodes = new String[numIfs]; // stores the nodes (test strings)
int[] operandNos = new int[numIfs]; // stores the number of operands the if currently has
int nodesIndex = -1; // the index of the if node currently parsed
for (String s : operands) {
if (s.equals("IF")) {
// new if found -> increase position in the "stack" (nodes)
operandNos[++nodesIndex] = 0;
} else {
addVertex(s);
switch (operandNos[nodesIndex]++) {
case 0:
// first operand = node name
nodes[nodesIndex] = s;
break;
case 1:
// second operand found -> add edge
addEdge(s, nodes[nodesIndex]);
break;
case 2:
// last operand found -> add edge and go back
do {
addEdge(s, nodes[nodesIndex]);
s = nodes[nodesIndex--];
} while (nodesIndex >= 0 && operandNos[nodesIndex]++ == 2);
if (nodesIndex >= 0) {
// was not the last operand of the IF
addEdge(s, nodes[nodesIndex]);
}
}
}
}
而不是 addEdge
和 addVertex
使用图的方法。我建议使用 DirectedSparseGraph
因为您的图形是有向的并且不允许平行边。要向图中添加顶点,请使用 graph.addVertex(vertexName)
并添加边,请使用 graph.addEdge(edge, sourceVertexName, destinationVertexName)
。
addEdge
和 addVertex
的实现如下所示:
void addVertex(String s) {
n.addVertex(s);
}
void addEdge(String source, String dest) {
n.addEdge("", source, dest);
}
或更好地内联它们。
关于java - 将字符串转换为二叉树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24244227/