Adds BST min, max, succ, pred

master
Abhinav Sarkar 2019-06-29 19:28:17 +05:30
parent 12edd9e409
commit 35a6a5057a
1 changed files with 88 additions and 5 deletions

View File

@ -18,6 +18,22 @@ public class BinarySearchTree<K extends Comparable<K>,V>
this.root.delete(key);
}
public Optional<K> minimum() {
return this.root.minimum();
}
public Optional<K> maximum() {
return this.root.maximum();
}
public Optional<K> successor(K key) {
return this.root.successor(key);
}
public Optional<K> predecessor(K key) {
return this.root.predecessor(key);
}
public String toString() {
return this.root.toStringBuilder(0).toString();
}
@ -27,6 +43,10 @@ public class BinarySearchTree<K extends Comparable<K>,V>
Node<K,V> insert(K key, V val);
Optional<V> get(K key);
Node<K,V> delete(K key);
Optional<K> minimum();
Optional<K> maximum();
Optional<K> successor(K key);
Optional<K> predecessor(K key);
boolean isEmpty();
ValueNode<K,V> toValueNode();
StringBuilder toStringBuilder(int level);
@ -141,6 +161,38 @@ public class BinarySearchTree<K extends Comparable<K>,V>
return successor;
}
@Override
public Optional<K> minimum()
{
return Optional.of(this.left.minimum().orElse(this.key));
}
@Override
public Optional<K> maximum()
{
return Optional.of(this.right.maximum().orElse(this.key));
}
@Override
public Optional<K> successor(K key)
{
if (this.key.compareTo(key) > 0) {
return Optional.of(this.left.successor(key).orElse(this.key));
} else {
return this.right.successor(key);
}
}
@Override
public Optional<K> predecessor(K key)
{
if (this.key.compareTo(key) < 0) {
return Optional.of(this.right.predecessor(key).orElse(this.key));
} else {
return this.left.predecessor(key);
}
}
@Override
public boolean checkIfBST()
{
@ -148,16 +200,16 @@ public class BinarySearchTree<K extends Comparable<K>,V>
&& isKeyMoreThanLeftKey() && isKeyLessThanRightKey();
}
private boolean isKeyLessThanRightKey()
{
return this.right.isEmpty() || this.key.compareTo(this.right.toValueNode().key) < 0;
}
private boolean isKeyMoreThanLeftKey()
{
return this.left.isEmpty() || this.key.compareTo(this.left.toValueNode().key) > 0;
}
private boolean isKeyLessThanRightKey()
{
return this.right.isEmpty() || this.key.compareTo(this.right.toValueNode().key) < 0;
}
@Override
public StringBuilder toStringBuilder(int level) {
return new StringBuilder().append(gutter(level))
@ -215,6 +267,26 @@ public class BinarySearchTree<K extends Comparable<K>,V>
return this;
}
@Override
public Optional<K> minimum()
{
return Optional.empty();
}
@Override
public Optional<K> maximum()
{
return Optional.empty();
}
public Optional<K> successor(K key) {
return Optional.empty();
}
public Optional<K> predecessor(K key) {
return Optional.empty();
}
@Override
public boolean checkIfBST()
{
@ -288,5 +360,16 @@ public class BinarySearchTree<K extends Comparable<K>,V>
bst.delete("c");
System.out.println(bst);
System.out.println(bst.root.checkIfBST());
System.out.println(bst.minimum());
System.out.println(bst.maximum());
System.out.println(bst.successor("a"));
System.out.println(bst.successor("z"));
System.out.println(bst.successor("q"));
System.out.println(bst.predecessor("a"));
System.out.println(bst.predecessor("z"));
System.out.println(bst.predecessor("q"));
}
}