Adds BST rendering
parent
5962d43f20
commit
52033d8547
|
@ -48,7 +48,7 @@ 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
|
||||||
{
|
{
|
||||||
AVLTreeGraphvizVisitor visitor = new AVLTreeGraphvizVisitor("AVLTree");
|
TreeGraphvizVisitor visitor = new TreeGraphvizVisitor("AVLTree");
|
||||||
this.root.accept(visitor);
|
this.root.accept(visitor);
|
||||||
visitor.renderToPNG(fileName);
|
visitor.renderToPNG(fileName);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package net.abhinavsarkar.algorist;
|
package net.abhinavsarkar.algorist;
|
||||||
|
|
||||||
|
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;
|
||||||
|
@ -47,6 +48,13 @@ public class BinarySearchTree<K extends Comparable<K>,V> implements Iterable<Bin
|
||||||
return new PreOrderIterator<>(this.root);
|
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>
|
private interface Node<K extends Comparable<K>, V>
|
||||||
{
|
{
|
||||||
Node<K,V> insert(K key, V val);
|
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();
|
boolean isEmpty();
|
||||||
StringBuilder toStringBuilder(int level);
|
StringBuilder toStringBuilder(int level);
|
||||||
boolean checkIfBST();
|
boolean checkIfBST();
|
||||||
|
Optional<ValueNode<K,V>> accept(Visitor visitor);
|
||||||
|
|
||||||
default ValueNode<K,V> toValueNode() {
|
default ValueNode<K,V> toValueNode() {
|
||||||
if (this.isEmpty()) {
|
if (this.isEmpty()) {
|
||||||
|
@ -238,6 +247,21 @@ public class BinarySearchTree<K extends Comparable<K>,V> implements Iterable<Bin
|
||||||
{
|
{
|
||||||
return "{" + key + ':' + val + '}';
|
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>
|
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>";
|
return "<NULL>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<ValueNode<K, V>> accept(Visitor visitor)
|
||||||
|
{
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static StringBuilder gutter(int times) {
|
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<>();
|
BinarySearchTree<String, String> bst = new BinarySearchTree<>();
|
||||||
bst.insert("q", "barista");
|
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);
|
||||||
System.out.println(bst.root.checkIfBST());
|
System.out.println(bst.root.checkIfBST());
|
||||||
|
bst.renderToPNG("BinarySearchTree.png");
|
||||||
|
|
||||||
System.out.println(bst.get("a"));
|
System.out.println(bst.get("a"));
|
||||||
System.out.println(bst.get("b"));
|
System.out.println(bst.get("b"));
|
||||||
|
|
|
@ -9,12 +9,12 @@ import guru.nidi.graphviz.model.MutableNode;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class AVLTreeGraphvizVisitor implements AVLTree.Visitor
|
public class TreeGraphvizVisitor implements AVLTree.Visitor, BinarySearchTree.Visitor
|
||||||
{
|
{
|
||||||
private final MutableGraph graph;
|
private final MutableGraph graph;
|
||||||
private final AVLTree<String, MutableNode> index = new AVLTree<>(true);
|
private final AVLTree<String, MutableNode> index = new AVLTree<>(true);
|
||||||
|
|
||||||
public AVLTreeGraphvizVisitor(String graphName)
|
public TreeGraphvizVisitor(String graphName)
|
||||||
{
|
{
|
||||||
this.graph = mutGraph(graphName).setDirected(true);
|
this.graph = mutGraph(graphName).setDirected(true);
|
||||||
}
|
}
|
Loading…
Reference in New Issue