42 lines
1.2 KiB
Java
42 lines
1.2 KiB
Java
package net.abhinavsarkar.algorist;
|
|
|
|
import guru.nidi.graphviz.engine.Format;
|
|
import guru.nidi.graphviz.engine.Graphviz;
|
|
import static guru.nidi.graphviz.model.Factory.mutGraph;
|
|
import static guru.nidi.graphviz.model.Factory.mutNode;
|
|
import guru.nidi.graphviz.model.MutableGraph;
|
|
import guru.nidi.graphviz.model.MutableNode;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
|
|
public class TreeGraphvizVisitor implements AVLTree.Visitor, BinarySearchTree.Visitor
|
|
{
|
|
private final MutableGraph graph;
|
|
private final AVLTree<String, MutableNode> index = new AVLTree<>(true);
|
|
|
|
public TreeGraphvizVisitor(String graphName)
|
|
{
|
|
this.graph = mutGraph(graphName).setDirected(true);
|
|
}
|
|
|
|
@Override
|
|
public void visitNode(String nodeName)
|
|
{
|
|
MutableNode gNode = mutNode(nodeName);
|
|
graph.add(gNode);
|
|
index.put(nodeName, gNode);
|
|
}
|
|
|
|
@Override
|
|
public void visitLink(String parentNodeName, String childNodeName)
|
|
{
|
|
index.get(parentNodeName)
|
|
.ifPresent(gNode -> graph.add(gNode.addLink(mutNode(childNodeName))));
|
|
}
|
|
|
|
public void renderToPNG(String fileName) throws IOException
|
|
{
|
|
Graphviz.fromGraph(graph).height(1000).render(Format.PNG).toFile(new File(fileName));
|
|
}
|
|
}
|