Changes AVL tree rendering to use visitor pattern

master
Abhinav Sarkar 2019-07-05 12:37:55 +05:30
parent 511950f24e
commit a1ea0f3bf3
2 changed files with 59 additions and 19 deletions

View File

@ -1,16 +1,10 @@
package net.abhinavsarkar.algorist; package net.abhinavsarkar.algorist;
import guru.nidi.graphviz.engine.Format;
import guru.nidi.graphviz.engine.Graphviz;
import guru.nidi.graphviz.model.MutableGraph;
import guru.nidi.graphviz.model.MutableNode;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Optional; import java.util.Optional;
import java.util.Stack; import java.util.Stack;
import static guru.nidi.graphviz.model.Factory.*;
public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entry<K,V>> public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entry<K,V>>
{ {
@ -54,9 +48,9 @@ public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entr
public void renderToPNG(String fileName) throws IOException public void renderToPNG(String fileName) throws IOException
{ {
MutableGraph graph = mutGraph("AVLTree").setDirected(true); AVLTreeGraphvizVisitor visitor = new AVLTreeGraphvizVisitor("AVLTree");
this.root.addToGraph(graph); this.root.accept(visitor);
Graphviz.fromGraph(graph).height(1000).render(Format.PNG).toFile(new File(fileName)); visitor.renderToPNG(fileName);
} }
@Override @Override
@ -77,9 +71,9 @@ public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entr
Optional<K> predecessor(K key); Optional<K> predecessor(K key);
boolean isEmpty(); boolean isEmpty();
StringBuilder toStringBuilder(int level); StringBuilder toStringBuilder(int level);
Optional<String> addToGraph(MutableGraph graph);
boolean checkIfBST(); boolean checkIfBST();
boolean checkIfAVLT(); boolean checkIfAVLT();
Optional<ValueNode<K,V>> accept(Visitor visitor);
default ValueNode<K,V> toValueNode() { default ValueNode<K,V> toValueNode() {
if (this.isEmpty()) { if (this.isEmpty()) {
@ -349,17 +343,16 @@ public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entr
} }
@Override @Override
public Optional<String> addToGraph(MutableGraph graph) public Optional<ValueNode<K,V>> accept(Visitor visitor)
{ {
String name = getGraphNodeName(); String name = this.getName();
MutableNode node = mutNode(name); visitor.visitNode(name);
graph.add(node); this.left.accept(visitor).ifPresent(n -> visitor.visitLink(name, n.getName()));
left.addToGraph(graph).ifPresent(n -> graph.add(node.addLink(mutNode(n)))); this.right.accept(visitor).ifPresent(n -> visitor.visitLink(name, n.getName()));
right.addToGraph(graph).ifPresent(n -> graph.add(node.addLink(mutNode(n)))); return Optional.of(this);
return Optional.of(name);
} }
private String getGraphNodeName() private String getName()
{ {
return this.height + ":" + this.key; return this.height + ":" + this.key;
} }
@ -459,7 +452,7 @@ public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entr
} }
@Override @Override
public Optional<String> addToGraph(MutableGraph graph) { public Optional<ValueNode<K, V>> accept(Visitor visitor) {
return Optional.empty(); return Optional.empty();
} }
} }
@ -544,6 +537,12 @@ public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entr
} }
} }
public interface Visitor
{
void visitNode(String nodeName);
void visitLink(String parentNodeName, String childNodeName);
}
public static void main(String[] args) throws IOException public static void main(String[] args) throws IOException
{ {
AVLTree<String, String> bst = new AVLTree<>(true); AVLTree<String, String> bst = new AVLTree<>(true);

View File

@ -0,0 +1,41 @@
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 AVLTreeGraphvizVisitor implements AVLTree.Visitor
{
private final MutableGraph graph;
private final AVLTree<String, MutableNode> index = new AVLTree<>(true);
public AVLTreeGraphvizVisitor(String graphName)
{
this.graph = mutGraph(graphName).setDirected(true);
}
@Override
public void visitNode(String nodeName)
{
MutableNode gNode = mutNode(nodeName);
graph.add(gNode);
index.insert(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));
}
}