二叉查找树

本文详细介绍了二叉查找树的数据结构及其基本操作,包括插入、查找、删除等,并提供了完整的Java实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉查找树数据结构:

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();
		
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值