找到了一篇经典的BST实现,文章地址:
http://www.cnblogs.com/maxupeng/archive/2011/04/02/2004114.html
以下是转载内容:
来自:数据结构与算法分析 JAVA语言描述 by Mark Allen Weiss
这书上的代码比我写的代码好看多了。还要努力啊。
import
java.util.*;
public
class
BinarySearchTree<T>
{
private
static
class
BinaryNode<T>
{
T element;
BinaryNode<T> left;
BinaryNode<T> right;
BinaryNode(T element) {
this
(element,
null
,
null
);
}
BinaryNode(T element, BinaryNode<T> left, BinaryNode<T> right) {
this
.element = element;
this
.left = left;
this
.right = right;
}
}
private
BinaryNode<T> root;
private
Comparator<?
super
T> cmp;
public
BinarySearchTree() {
this
(
null
); }
public
BinarySearchTree(Comparator<?
super
T> cmp) {
root =
null
;
this
.cmp = cmp;
}
public
void
makeEmpty() { root =
null
; }
public
boolean
isEmpty() {
return
root ==
null
; }
public
boolean
contains(T x) {
return
contains(x, root); }
public
T findMin() {
return
isEmpty() ?
null
: findMin(root).element; }
public
T findMax() {
return
isEmpty() ?
null
: findMax(root).element; }
public
void
insert(T x) { root = insert(x, root); }
public
void
remove(T x) { root = remove(x, root); }
public
void
printTree() {
if
(isEmpty()) System.out.println(
"Empty tree"
);
else
{
System.out.print(
"["
);
printTree(root);
System.out.println(
"]"
);
}
}
private
int
myCompare(T a, T b) {
if
(cmp !=
null
)
return
cmp.compare(a, b);
return
((Comparable)a).compareTo(b);
}
/**
* Internal method to find an item in a subtree.
* @param x is item to search for.
* @param t the node that roots the subtree.
* @return node containing the matched item.
*/
private
boolean
contains(T x, BinaryNode<T> t) {
if
(t ==
null
)
return
false
;
int
result = myCompare(x, t.element);
if
(result <
0
)
return
contains(x, t.left);
else
if
(result >
0
)
return
contains(x, t.right);
return
true
;
}
/**
* Internal method to find the smallest item in a subtree.
* @param t the node that roots the subtree.
* @return node containing the smallest item.
*/
private
BinaryNode<T> findMin(BinaryNode<T> t) {
if
(t ==
null
)
return
null
;
if
(t.left ==
null
)
return
t;
return
findMin(t.left);
}
/**
* Internal method to find the largest item in a subtree.
* @param t the node that roots the subtree.
* @return node containing the largest item.
*/
private
BinaryNode<T> findMax(BinaryNode<T> t) {
if
(t ==
null
)
return
null
;
if
(t.right ==
null
)
return
t;
return
findMax(t.right);
}
/**
* Internal method to insert into a subtree.
* @param x the item to insert.
* @param t the node that roots the subtree.
* @return the new root of the subtree.
*/
private
BinaryNode<T> insert(T x, BinaryNode<T> t) {
if
(t ==
null
)
return
new
BinaryNode<T>(x);
int
result = myCompare(x, t.element);
if
(result <
0
) t.left = insert(x, t.left);
else
if
(result >
0
) t.right = insert(x, t.right);
else
;
// Duplicate; do nothing
return
t;
}
/**
* Internal method to remove from a subtree.
* @param x the item to remove.
* @param t the node that roots the subtree.
* @return the new root of the subtree.
*/
private
BinaryNode<T> remove(T x, BinaryNode<T> t) {
if
(t ==
null
)
return
t;
// Item not found; do nothing
int
result = myCompare(x, t.element);
if
(result <
0
) t.left = remove(x, t.left);
else
if
(result >
0
) t.right = remove(x, t.right);
else
if
(t.left !=
null
&& t.right !=
null
) {
// Two children
t.element = removeMin(t.right).element;
}
else
{
// One children or leaf;
t = (t.left !=
null
) ? t.left : t.right;
}
return
t;
}
/**
* Internal method to remove the smallest node from a subtree.
* @param t the node that roots the subtree.
* @return the smallest node of the subtree.
*/
private
BinaryNode<T> removeMin(BinaryNode<T> t) {
if
(t ==
null
)
return
null
;
BinaryNode<T> n = t;
BinaryNode<T> p = n;
while
(n.left !=
null
) {
p = n;
n = n.left;
}
p.left = n.right;
return
n;
}
private
void
printTree(BinaryNode<T> t) {
if
(t !=
null
) {
printTree(t.left);
System.out.print(t.element +
" "
);
printTree(t.right);
}
}
}
|