Java 中的树生成器算法
Tree generator algorithm in Java
我正在尝试在 Java.
中使用 GraphStream API 生成 n-ary
或 k-ary
树
该算法可能会也可能不会生成完整的树。这段代码离生成这样一棵树还很远:
public class Demo {
public static void main(String args[]) {
for (int i = 0; i <= 30; i ++) {
graph.addNode("a" + i);
graph.addNode("b" + i);
}
TreeGenerator(graph, 5, 3);
graph.display();
}
static Graph graph = new SingleGraph("graph");
static Graph TreeGenerator(Graph g, int n, int tiers) {
while (n > 0) {
for (int i = n; i > 0; i--)
g.addEdge("ab" + n,"a" + (n-n),"b" + n);
//g.addEdge("ad" + n,"a" + n,"b" + n*Math.round(Math.log(n)));
n--;
}
if (tiers > 0) {
tiers--;
return TreeGenerator(g, n, tiers);
} else return g;
}
}
public static Graph karyTree(Graph g, int V, int k) {
int[] vertices = new int[(int)Math.pow(V, k)];
for (int i = 0; i < (int)Math.pow(V, k); i++) vertices[i] = i;
for (int i = 1; i < (int)Math.pow(V, k); i++) {
g.addEdge("ax" + returnRandom(0,100000000), "a"+vertices[i],"a"+vertices[(i-1)/k]);
}
return g;
}
这是我的问题的解决方案,我在二叉树生成函数的帮助下弄明白了
我正在尝试在 Java.
中使用 GraphStream API 生成n-ary
或 k-ary
树
该算法可能会也可能不会生成完整的树。这段代码离生成这样一棵树还很远:
public class Demo {
public static void main(String args[]) {
for (int i = 0; i <= 30; i ++) {
graph.addNode("a" + i);
graph.addNode("b" + i);
}
TreeGenerator(graph, 5, 3);
graph.display();
}
static Graph graph = new SingleGraph("graph");
static Graph TreeGenerator(Graph g, int n, int tiers) {
while (n > 0) {
for (int i = n; i > 0; i--)
g.addEdge("ab" + n,"a" + (n-n),"b" + n);
//g.addEdge("ad" + n,"a" + n,"b" + n*Math.round(Math.log(n)));
n--;
}
if (tiers > 0) {
tiers--;
return TreeGenerator(g, n, tiers);
} else return g;
}
}
public static Graph karyTree(Graph g, int V, int k) {
int[] vertices = new int[(int)Math.pow(V, k)];
for (int i = 0; i < (int)Math.pow(V, k); i++) vertices[i] = i;
for (int i = 1; i < (int)Math.pow(V, k); i++) {
g.addEdge("ax" + returnRandom(0,100000000), "a"+vertices[i],"a"+vertices[(i-1)/k]);
}
return g;
}
这是我的问题的解决方案,我在二叉树生成函数的帮助下弄明白了