Changes AVL tree rendering to use visitor pattern
This commit is contained in:
parent
511950f24e
commit
a1ea0f3bf3
@ -1,16 +1,10 @@
|
||||
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.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
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>>
|
||||
{
|
||||
@ -54,9 +48,9 @@ public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entr
|
||||
|
||||
public void renderToPNG(String fileName) throws IOException
|
||||
{
|
||||
MutableGraph graph = mutGraph("AVLTree").setDirected(true);
|
||||
this.root.addToGraph(graph);
|
||||
Graphviz.fromGraph(graph).height(1000).render(Format.PNG).toFile(new File(fileName));
|
||||
AVLTreeGraphvizVisitor visitor = new AVLTreeGraphvizVisitor("AVLTree");
|
||||
this.root.accept(visitor);
|
||||
visitor.renderToPNG(fileName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -77,9 +71,9 @@ public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entr
|
||||
Optional<K> predecessor(K key);
|
||||
boolean isEmpty();
|
||||
StringBuilder toStringBuilder(int level);
|
||||
Optional<String> addToGraph(MutableGraph graph);
|
||||
boolean checkIfBST();
|
||||
boolean checkIfAVLT();
|
||||
Optional<ValueNode<K,V>> accept(Visitor visitor);
|
||||
|
||||
default ValueNode<K,V> toValueNode() {
|
||||
if (this.isEmpty()) {
|
||||
@ -349,17 +343,16 @@ public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entr
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> addToGraph(MutableGraph graph)
|
||||
public Optional<ValueNode<K,V>> accept(Visitor visitor)
|
||||
{
|
||||
String name = getGraphNodeName();
|
||||
MutableNode node = mutNode(name);
|
||||
graph.add(node);
|
||||
left.addToGraph(graph).ifPresent(n -> graph.add(node.addLink(mutNode(n))));
|
||||
right.addToGraph(graph).ifPresent(n -> graph.add(node.addLink(mutNode(n))));
|
||||
return Optional.of(name);
|
||||
String name = this.getName();
|
||||
visitor.visitNode(name);
|
||||
this.left.accept(visitor).ifPresent(n -> visitor.visitLink(name, n.getName()));
|
||||
this.right.accept(visitor).ifPresent(n -> visitor.visitLink(name, n.getName()));
|
||||
return Optional.of(this);
|
||||
}
|
||||
|
||||
private String getGraphNodeName()
|
||||
private String getName()
|
||||
{
|
||||
return this.height + ":" + this.key;
|
||||
}
|
||||
@ -459,7 +452,7 @@ public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entr
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> addToGraph(MutableGraph graph) {
|
||||
public Optional<ValueNode<K, V>> accept(Visitor visitor) {
|
||||
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
|
||||
{
|
||||
AVLTree<String, String> bst = new AVLTree<>(true);
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user