二叉查找树数据结构:
package tree;
/**
* 二叉查找数数据结构
*
* @author liyulin
*
* @param <E>
*
* @version 1.0 2013-10-17 下午12:15:15
*
*/
public class BinarySearchTree<E extends Comparable<? super E>> {
private BinaryNode<E> root;
public BinarySearchTree(){
root = null;
}
/**
* 是否是空树
*
* @return boolean
*/
public boolean isEmpty(){
return root == null;
}
/**
* 判断x是否在树中
*
* @param x 要查找的数据
* @return boolean
*/
public boolean contains(E x) {
return contains(x, root);
}
/**
* 查找二叉树中最小的元素
*
* @return E
*/
public E findMin(){
if(isEmpty()){
try {
throw new Exception("树为空异常!");
} catch (Exception e) {
e.printStackTrace();
}
}
return findMin(root).value;
}
/**
* 查找二叉树中最大的元素
*
* @return E
*/
public E findMax(){
if(isEmpty()){
try {
throw new Exception("树为空异常!");
} catch (Exception e) {
e.printStackTrace();
}
}
return findMax(root).value;
}
/**
* 将一个节点插入到树中
*
* @param x 节点
*/
public void insert(E x){
root = insert(x, root);
}
/**
* 从树中删除x
*
* @param x 要删除的数据
*/
public void remove(E x){
root = remove(x, root);
}
/**
* 打印二叉树
*/
public void printTree(){
if(root == null){
System.out.println("空树!");
}else{
printTree(root);
}
}
/**
* 向树中插入一个节点
*
* @param x 节点
* @param node 树
* @return BinaryNode
*/
private BinaryNode<E> insert(E x, BinaryNode<E> node) {
if (node == null) {
return new BinaryNode<E>(x, null, null);
}
int compareResult = x.compareTo(node.value);
if (compareResult < 0) {
node.left = insert(x, node.left);
} else if (compareResult > 0) {
node.right = insert(x, node.right);
} else {
}
return node;
}
/**
* 查找x是否在树中
*
* @param x 要查找的数据
* @param node 树
* @return boolean
*/
private boolean contains(E x, BinaryNode<E> node) {
if (node == null) {
return false;
}
int compareResult = x.compareTo(node.value);
if (compareResult < 0) {
return contains(x, node.left);
} else if (compareResult > 0) {
return contains(x, node.right);
} else {
return true;
}
}
/**
* 从树中删除x
*
* @param x 要删除的元素
* @param node 树
* @return BinaryNode
*/
private BinaryNode<E> remove(E x, BinaryNode<E> node) {
if( node == null){
return node;
}
int compareResult = x.compareTo(node.value);
if (compareResult < 0) {
node.left = remove(x, node.left);
} else if (compareResult > 0) {
node.right = remove(x, node.right);
} else if(node.left != null && node.right != null){
node.value = findMin(node.right).value;
node.right = remove(node.value, node.right);
}else{
node = (node.left != null)?node.left:node.right;
}
return node;
}
/**
* 查找二叉树中最小的元素
*
* @param node 树
* @return BinaryNode
*/
private BinaryNode<E> findMin(BinaryNode<E> node){
if(node == null){
return null;
} else if(node.left == null){
return node;
}
return findMin(node.left);
}
/**
* 查找二叉树中的最大元素
*
* @param node 树
* @return BinaryNode
*/
private BinaryNode<E> findMax(BinaryNode<E> node){
if(node != null){
while(node.right != null){
node = node.right;
}
}
return node;
}
/**
* 中序遍历二叉树
*
* @param node 树
*/
private void printTree(BinaryNode<E> node){
if(node != null){
printTree(node.left);
System.out.print(node.value +" ");
printTree(node.right);
}
}
/**
* 二叉树的树节点数据结构
*
* @author liyulin
* @vrsion 1.0 2013-10-17 上午11:59:34
*/
private static class BinaryNode<E> {
private E value;
private BinaryNode<E> left;
private BinaryNode<E> right;
public BinaryNode(E value, BinaryNode<E> left, BinaryNode<E> right) {
this.value = value;
this.left = left;
this.right = right;
}
}
}
测试代码:
package tree;
public class BinarySearchTreeTest {
public static void main(String[] args) {
BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree<Integer>();
binarySearchTree.insert(1);
binarySearchTree.insert(3);
binarySearchTree.insert(2);
binarySearchTree.insert(4);
binarySearchTree.insert(5);
binarySearchTree.insert(4);
binarySearchTree.insert(6);
binarySearchTree.insert(3);
binarySearchTree.printTree();
}
}