dimanche 8 mars 2015

Tree generator algorithm in Java



I am trying to generate an n-ary or k-ary tree using the GraphStream API, in Java.


The algorithm may or may not generate a full tree. This code is pretty far off from being able to generate such a tree:



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;
}
}



Aucun commentaire:

Enregistrer un commentaire