Skip to content

Instantly share code, notes, and snippets.

@automaticgiant
Created October 17, 2014 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save automaticgiant/6ecf1b22af0705930d82 to your computer and use it in GitHub Desktop.
Save automaticgiant/6ecf1b22af0705930d82 to your computer and use it in GitHub Desktop.
2-3+ tree dot output
private String dotNode(BaseNode node){
String dotNode = "\n \"" + ((Object)node).toString() + "\" [label=<<table><tr><td port=\"l\">";
if (node instanceof InternalNode) {
dotNode += (((InternalNode)node).left == null) ? "null" : ((InternalNode)node).left;
dotNode += "\n </td><td port=\"n\">";
dotNode += ((Object)node).toString();
dotNode += "\n </td><td port=\"r\">";
dotNode += (((InternalNode)node).right == null) ? "null" : ((InternalNode)node).right;
dotNode += "\n </td></tr><tr><td port=\"cl\">";
dotNode += (((InternalNode)node).childleft == null) ? "null" : ((Object)((InternalNode)node).childleft).toString();
dotNode += "\n </td><td port=\"cc\">";
dotNode += (((InternalNode)node).childcenter == null) ? "null" : ((Object)((InternalNode)node).childcenter).toString();
dotNode += "\n </td><td port=\"cr\">";
dotNode += (((InternalNode)node).childright == null) ? "null" : ((Object)((InternalNode)node).childright).toString();
} else {
dotNode += (((LeafNode)node).left == null) ? "null" : ((LeafNode)node).left;
dotNode += "\n </td><td port=\"n\">";
dotNode += ((Object) node).toString();
dotNode += "\n </td><td port=\"r\">";
dotNode += (((LeafNode)node).right == null) ? "null" : ((LeafNode)node).right;
}
dotNode += "\n </td></tr></table>>];\n";
return dotNode;
}
public String graph(){
Set<BaseNode> visited = new HashSet<>();
LinkedList<BaseNode> queue = new LinkedList<>();
String dot = "digraph \"2-3+tree\" {\n node [shape=plaintext]";
if (root != null) queue.addLast(root);
BaseNode current;
while (!queue.isEmpty()) {
current = queue.pop();
if (!visited.add(current)) continue;
dot += dotNode(current);
if (current instanceof InternalNode) {
if (((InternalNode)current).childleft != null) {
dot += '"' + ((Object) current).toString() + "\":cl -> \"" + ((Object) ((InternalNode) current).childleft).toString() + "\"\n";
queue.addLast(((InternalNode)current).childleft);
}
if (((InternalNode)current).childcenter != null) {
dot += '"' + ((Object) current).toString() + "\":cc -> \"" + ((Object) ((InternalNode) current).childcenter).toString() + "\"\n";
queue.addLast(((InternalNode)current).childcenter);
}
if (((InternalNode)current).childright != null) {
dot += '"' + ((Object) current).toString() + "\":cr -> \"" + ((Object) ((InternalNode) current).childright).toString() + "\"\n";
queue.addLast(((InternalNode)current).childright);
}
}
}
return dot + "\n}";
}
public String writeGraph(){
StringUtils.writeFile(this.graph(), "last test graph.dot");
return this.toString();
}
public static void writeFile(String string, String filename) {
try {
FileWriter file = new FileWriter(filename);
file.write(string);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment