Adds BST get

This commit is contained in:
Abhinav Sarkar 2019-06-29 17:15:09 +05:30
parent 00253e78ee
commit 9173f20491

View File

@ -1,75 +1,81 @@
package net.abhinavsarkar.algorist; package net.abhinavsarkar.algorist;
import java.util.Optional;
public class BinarySearchTree<K extends Comparable<K>,V> public class BinarySearchTree<K extends Comparable<K>,V>
{ {
private Node<K,V> root = (Node<K,V>) EmptyNode.INSTANCE;
private Node<K,V> root;
public BinarySearchTree() {
this.root = null;
}
public void insert(K key, V val) { public void insert(K key, V val) {
if (this.root == null) { this.root = this.root.insert(key, val);
this.root = new Node<>(key, val); }
} else {
this.root.insert(key, val); public Optional<V> get(K key) {
} return this.root.get(key);
} }
public String toString() { public String toString() {
return this.root.toString(0).toString(); return this.root.toStringBuilder(0).toString();
} }
public static class Node<K extends Comparable<K>,V> { public interface Node<K extends Comparable<K>, V>
{
Node<K,V> insert(K key, V val);
Optional<V> get(K key);
StringBuilder toStringBuilder(int level);
}
private static class ValueNode<K extends Comparable<K>,V> implements Node<K, V>
{
private K key; private K key;
private V val; private V val;
private Node<K,V> left; private Node<K,V> left = (Node<K,V>) EmptyNode.INSTANCE;
private Node<K,V> right; private Node<K,V> right = (Node<K,V>) EmptyNode.INSTANCE;
ValueNode(K key, V val) {
if (key == null) {
throw new IllegalArgumentException("Key cannot be null");
}
if (val == null) {
throw new IllegalArgumentException("Value cannot be null");
}
public Node(K key, V val) {
this.key = key; this.key = key;
this.val = val; this.val = val;
} }
public V getVal()
{
return val;
}
public K getKey()
{
return key;
}
public void insert(K key, V val) @Override
public Node<K, V> insert(K key, V val)
{ {
if (this.key == key) { if (this.key == key) {
this.val = val; this.val = val;
} else if (this.key.compareTo(key) > 0) { } else if (this.key.compareTo(key) > 0) {
if (this.left == null) { this.left = this.left.insert(key, val);
this.left = new Node<>(key, val);
} else {
this.left.insert(key, val);
}
} else { } else {
if (this.right == null) { this.right = this.right.insert(key, val);
this.right = new Node<>(key, val); }
} else { return this;
this.right.insert(key, val); }
}
@Override
public Optional<V> get(K key)
{
if (this.key == key) {
return Optional.of(val);
} else if (this.key.compareTo(key) > 0) {
return this.left.get(key);
} else {
return this.right.get(key);
} }
} }
public StringBuilder toString(int level) { @Override
StringBuilder sb = new StringBuilder() public StringBuilder toStringBuilder(int level) {
return new StringBuilder()
.append(gutter(level)) .append(gutter(level))
.append("<" + key + ":" + val + ">\n"); .append("<" + key + ':' + val + ">\n")
if (this.left != null) { .append(this.left.toStringBuilder(level + 1))
sb.append(this.left.toString(level + 1)); .append(this.right.toStringBuilder(level + 1));
}
if (this.right != null) {
sb.append(this.right.toString(level + 1));
}
return sb;
} }
private static StringBuilder gutter(int times) { private static StringBuilder gutter(int times) {
@ -82,6 +88,29 @@ public class BinarySearchTree<K extends Comparable<K>,V>
} }
} }
private static class EmptyNode<K extends Comparable<K>,V> implements Node<K,V> {
@SuppressWarnings("rawtypes")
private static final EmptyNode INSTANCE = new EmptyNode();
@Override
public Node<K, V> insert(K key, V val)
{
return new ValueNode<>(key, val);
}
@Override
public Optional<V> get(K key)
{
return Optional.empty();
}
@Override
public StringBuilder toStringBuilder(int level)
{
return new StringBuilder(0);
}
}
public static void main(String[] args) public static void main(String[] args)
{ {
BinarySearchTree<String, String> bst = new BinarySearchTree<>(); BinarySearchTree<String, String> bst = new BinarySearchTree<>();
@ -91,6 +120,11 @@ public class BinarySearchTree<K extends Comparable<K>,V>
bst.insert("d", "carl"); bst.insert("d", "carl");
System.out.println(bst); System.out.println(bst);
}
System.out.println(bst.get("a"));
System.out.println(bst.get("b"));
System.out.println(bst.get("c"));
System.out.println(bst.get("d"));
System.out.println(bst.get("z"));
}
} }