/**
* 二叉查找樹
* 部分代碼參考自TreeMap的源碼
*/
public class BinarySearchTree<T> {
protected TreeNode<T> root = null;
private final Comparator<? super T> comparator;
private int size = 0;
public BinarySearchTree() {
comparator = null;
}
public BinarySearchTree(Comparator<? super T> comparator) {
this.comparator = comparator;
}
/**
* 向樹中插入新結點
*
* @param key 新結點的key值
*/
public void insert(T key) {
TreeNode<T> x = root;
TreeNode<T> y = null;
TreeNode<T> z = new TreeNode<T>(key, null);
int cmp;
if (x == null) { // 若是根結點是null,則新插入結點成爲根結點
root = z;
size = 1;
return;
}
while (x != null) { // x從根結點開始沿樹降低,直到找到z應當插入的位置。y用於找出z的父結點的位置
y = x;
cmp = compareKey(z.key, x.key);
if (cmp < 0)
x = x.left;
else
x = x.right;
}
z.parent = y;
cmp = compareKey(z.key, y.key);
if (cmp < 0)
y.left = z;
else
y.right = z;
size++;
}
/**
* 刪除樹中某個結點
*
* @param key 待刪除結點的key值
* @return 被刪除結點的key值
*/
public T remove(T key) {
TreeNode<T> p = find(key);
if (p == null)
return null;
T oldValue = p.key;
deleteNode(p);
return oldValue;
}
public boolean isEmpty() {
return size() == 0;
}
public int size() {
return size;
}
/**
* 返回二叉查找樹中最左子結點
*
* @return 返回二叉查找樹中最左子結點
*/
public TreeNode<T> firstNode() {
return getFirstNode(root);
}
/**
* 返回二叉查找樹中最右子結點
*
* @return 返回二叉查找樹中最右子結點
*/
public TreeNode<T> lastNode() {
return getLastNode(root);
}
/**
* 根據key值查找結點
*/
public TreeNode<T> find(T t) {
TreeNode<T> p = root;
while (p != null) {
int cmp = compareKey(t, p.key);
if (cmp < 0)
p = p.left;
else if (cmp > 0)
p = p.right;
else
return p;
}
return null;
}
/**
* 返回結點集
*/
public Set<TreeNode<T>> nodeSet() {
return new NodeSet();
}
/**
* 返回傳入結點的後繼結點
*/
private static <T> TreeNode<T> successor(TreeNode<T> t) {
if (t == null)
return null;
else if (t.right != null) // 若是t的右子樹不空,那麼t的後繼就是其右子樹中key值最小的結點
return getFirstNode(t.right);
else { // 若是t的右子樹爲空,左子樹不空,那麼t的後繼就是t的一個最低祖先結點,且該結點的左孩子也必須是t的祖先結點
TreeNode<T> p = t.parent;
TreeNode<T> ch = t;
while (p != null && ch == p.right) {
ch = p;
p = p.parent;
}
return p;
}
}
/**
* 返回傳入結點的前趨結點
*/
private static <T> TreeNode<T> predecessor(TreeNode<T> t) {
if (t == null)
return null;
else if (t.left != null)
return getLastNode(t.left);
else {
TreeNode<T> p = t.parent;
TreeNode<T> ch = t;
while (p != null && ch == p.left) {
ch = p;
p = p.parent;
}
return p;
}
}
private static <T> TreeNode<T> getFirstNode(TreeNode<T> t) {
TreeNode<T> p = t;
if (p != null)
while (p.left != null)
p = p.left;
return p;
}
private static <T> TreeNode<T> getLastNode(TreeNode<T> t) {
TreeNode<T> p = t;
if (p != null)
while (p.right != null)
p = p.right;
return p;
}
/**
* 用結點v替換結點u在樹中的位置
*/
private void transplant(TreeNode<T> u, TreeNode<T> v) {
if (u.parent == null)
root = v;
else if (u == u.parent.left)
u.parent.left = v;
else
u.parent.right = v;
if (v != null)
v.parent = u.parent;
}
/**
* 刪除樹中的結點
*/
private void deleteNode(TreeNode<T> p) {
size--;
if (p.left == null) // 若是結點的左子樹爲空,則用右子樹替換結點
transplant(p, p.right);
else if (p.right == null) // 若是結點的右子樹爲空,則用左子樹替換結點
transplant(p, p.left);
else { // 結點有兩個子結點
TreeNode<T> y = getFirstNode(p.right); // 首先找出結點p的後繼y
if (y.parent != p) { // 若是p不是y的父結點,那麼須要用y的右孩子替換y
transplant(y, y.right);
y.right = p.right;
y.right.parent = y;
}
transplant(p, y); // 最後用y替換p
y.left = p.left;
y.left.parent = y;
}
}
/**
* 用於比較樹中結點的key值
*/
private int compareKey(T key1, T key2) {
int cmp;
if (comparator != null)
cmp = comparator.compare(key1, key2);
else {
if (key1 == null || key2 == null)
throw new NullPointerException();
Comparable<? super T> k = (Comparable<? super T>) key1;
cmp = k.compareTo(key2);
}
return cmp;
}
private static boolean keyEquals(Object o1, Object o2) {
return (o1 == null ? o2 == null : o1.equals(o2));
}
/**
* 二叉查找樹的結點類
*/
public static final class TreeNode<T> {
T key;
TreeNode<T> left = null;
TreeNode<T> right = null;
TreeNode<T> parent;
TreeNode(T key, TreeNode<T> parent) {
this.key = key;
this.parent = parent;
}
public T getKey() {
return key;
}
public boolean equals(Object o) {
if (!(o instanceof TreeNode))
return false;
TreeNode<?> e = (TreeNode<?>) o;
return keyEquals(key, e.getKey());
}
public String toString() {
return "[" + key + "]";
}
}
/**
* 樹中結點的集合類,使用迭代器遍歷,從最左結點開始,依次取其後繼結點(即中序遍歷)
*/
final class NodeSet extends AbstractSet<TreeNode<T>> {
public Iterator<TreeNode<T>> iterator() {
return new NodeIterator(firstNode());
}
public int size() {
return BinarySearchTree.this.size();
}
}
final class NodeIterator extends PrivateNodeIterator<TreeNode<T>> {
NodeIterator(TreeNode<T> first) {
super(first);
}
public TreeNode<T> next() {
return nextNode();
}
public void remove() { }
}
abstract class PrivateNodeIterator<E> implements Iterator<E> {
TreeNode<T> next;
PrivateNodeIterator(TreeNode<T> first) {
next = first;
}
public boolean hasNext() {
return next != null;
}
final TreeNode<T> nextNode() {
TreeNode<T> e = next;
if (e == null)
throw new NoSuchElementException();
next = successor(e);
return e;
}
}
}