Adds BST get
This commit is contained in:
parent
00253e78ee
commit
9173f20491
@ -1,75 +1,81 @@
|
||||
package net.abhinavsarkar.algorist;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class BinarySearchTree<K extends Comparable<K>,V>
|
||||
{
|
||||
|
||||
private Node<K,V> root;
|
||||
|
||||
public BinarySearchTree() {
|
||||
this.root = null;
|
||||
}
|
||||
private Node<K,V> root = (Node<K,V>) EmptyNode.INSTANCE;
|
||||
|
||||
public void insert(K key, V val) {
|
||||
if (this.root == null) {
|
||||
this.root = new Node<>(key, val);
|
||||
} else {
|
||||
this.root.insert(key, val);
|
||||
}
|
||||
this.root = this.root.insert(key, val);
|
||||
}
|
||||
|
||||
public Optional<V> get(K key) {
|
||||
return this.root.get(key);
|
||||
}
|
||||
|
||||
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 V val;
|
||||
private Node<K,V> left;
|
||||
private Node<K,V> right;
|
||||
private Node<K,V> left = (Node<K,V>) EmptyNode.INSTANCE;
|
||||
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.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) {
|
||||
this.val = val;
|
||||
} else if (this.key.compareTo(key) > 0) {
|
||||
if (this.left == null) {
|
||||
this.left = new Node<>(key, val);
|
||||
} else {
|
||||
this.left.insert(key, val);
|
||||
}
|
||||
this.left = this.left.insert(key, val);
|
||||
} else {
|
||||
if (this.right == null) {
|
||||
this.right = new Node<>(key, val);
|
||||
} else {
|
||||
this.right.insert(key, val);
|
||||
}
|
||||
this.right = this.right.insert(key, val);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@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) {
|
||||
StringBuilder sb = new StringBuilder()
|
||||
@Override
|
||||
public StringBuilder toStringBuilder(int level) {
|
||||
return new StringBuilder()
|
||||
.append(gutter(level))
|
||||
.append("<" + key + ":" + val + ">\n");
|
||||
if (this.left != null) {
|
||||
sb.append(this.left.toString(level + 1));
|
||||
}
|
||||
if (this.right != null) {
|
||||
sb.append(this.right.toString(level + 1));
|
||||
}
|
||||
return sb;
|
||||
.append("<" + key + ':' + val + ">\n")
|
||||
.append(this.left.toStringBuilder(level + 1))
|
||||
.append(this.right.toStringBuilder(level + 1));
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
BinarySearchTree<String, String> bst = new BinarySearchTree<>();
|
||||
@ -91,6 +120,11 @@ public class BinarySearchTree<K extends Comparable<K>,V>
|
||||
bst.insert("d", "carl");
|
||||
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user