Adds BST rendering

master
Abhinav Sarkar 2019-07-05 13:07:48 +05:30
parent 5962d43f20
commit 52033d8547
3 changed files with 41 additions and 4 deletions

View File

@ -48,7 +48,7 @@ public class AVLTree<K extends Comparable<K>,V> implements Iterable<AVLTree.Entr
public void renderToPNG(String fileName) throws IOException
{
AVLTreeGraphvizVisitor visitor = new AVLTreeGraphvizVisitor("AVLTree");
TreeGraphvizVisitor visitor = new TreeGraphvizVisitor("AVLTree");
this.root.accept(visitor);
visitor.renderToPNG(fileName);
}

View File

@ -1,5 +1,6 @@
package net.abhinavsarkar.algorist;
import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Optional;
@ -47,6 +48,13 @@ public class BinarySearchTree<K extends Comparable<K>,V> implements Iterable<Bin
return new PreOrderIterator<>(this.root);
}
public void renderToPNG(String fileName) throws IOException
{
TreeGraphvizVisitor visitor = new TreeGraphvizVisitor("AVLTree");
this.root.accept(visitor);
visitor.renderToPNG(fileName);
}
private interface Node<K extends Comparable<K>, V>
{
Node<K,V> insert(K key, V val);
@ -59,6 +67,7 @@ public class BinarySearchTree<K extends Comparable<K>,V> implements Iterable<Bin
boolean isEmpty();
StringBuilder toStringBuilder(int level);
boolean checkIfBST();
Optional<ValueNode<K,V>> accept(Visitor visitor);
default ValueNode<K,V> toValueNode() {
if (this.isEmpty()) {
@ -238,6 +247,21 @@ public class BinarySearchTree<K extends Comparable<K>,V> implements Iterable<Bin
{
return "{" + key + ':' + val + '}';
}
@Override
public Optional<ValueNode<K, V>> accept(Visitor visitor)
{
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 getName()
{
return this.key.toString();
}
}
private static class EmptyNode<K extends Comparable<K>,V> implements Node<K,V>
@ -309,6 +333,12 @@ public class BinarySearchTree<K extends Comparable<K>,V> implements Iterable<Bin
{
return "<NULL>";
}
@Override
public Optional<ValueNode<K, V>> accept(Visitor visitor)
{
return Optional.empty();
}
}
private static StringBuilder gutter(int times) {
@ -442,7 +472,13 @@ public class BinarySearchTree<K extends Comparable<K>,V> implements Iterable<Bin
}
public static void main(String[] args)
public interface Visitor
{
void visitNode(String nodeName);
void visitLink(String parentNodeName, String childNodeName);
}
public static void main(String[] args) throws IOException
{
BinarySearchTree<String, String> bst = new BinarySearchTree<>();
bst.insert("q", "barista");
@ -474,6 +510,7 @@ public class BinarySearchTree<K extends Comparable<K>,V> implements Iterable<Bin
System.out.println(bst);
System.out.println(bst.root.checkIfBST());
bst.renderToPNG("BinarySearchTree.png");
System.out.println(bst.get("a"));
System.out.println(bst.get("b"));

View File

@ -9,12 +9,12 @@ import guru.nidi.graphviz.model.MutableNode;
import java.io.File;
import java.io.IOException;
public class AVLTreeGraphvizVisitor implements AVLTree.Visitor
public class TreeGraphvizVisitor implements AVLTree.Visitor, BinarySearchTree.Visitor
{
private final MutableGraph graph;
private final AVLTree<String, MutableNode> index = new AVLTree<>(true);
public AVLTreeGraphvizVisitor(String graphName)
public TreeGraphvizVisitor(String graphName)
{
this.graph = mutGraph(graphName).setDirected(true);
}