数据结构之二叉查找树

package cn.thj.data_structures;

/**
 * 二叉查找树
 * 
 * @author 谭恒杰
 */
public class BinarySearchTree<T extends Comparable<? super T>> {
	/**
	 * 构造方法
	 */
	public BinarySearchTree() {
		root = null;
	}

	// 节点类
	private static class BinaryNode<AnyType> {

		AnyType element; // 节点下的元素
		BinaryNode<AnyType> left; // 左孩子
		BinaryNode<AnyType> right; // 右孩子

		// 构造
		BinaryNode(AnyType theElement) {
			this(theElement, null, null);
		}

		BinaryNode(AnyType theElement, BinaryNode<AnyType> lt,
				BinaryNode<AnyType> rt) {
			element = theElement;
			left = lt;
			right = rt;
		}

	}

	// 树的根节点
	private BinaryNode<T> root;

	/**
	 * 插入一个节点
	 * 
	 * @param x
	 */
	public void insert(T x) {
		root = insert(x, root);
	}

	/**
	 * 移除一个节点
	 * 
	 * @param x
	 *            被移除的节点
	 * 
	 */
	public void remove(T x) {
		root = remove(x, root);
	}

	/**
	 * 查找树中的最小的节点
	 * 
	 * @return 最小的节点若树为空则返回null
	 */
	public T findMin() {
		if (isEmpty())
			throw new RuntimeException();
		return findMin(root).element;
	}

	/**
	 * 查找树中的最大的节点
	 * 
	 * @return 最大的节点若树为空则返回null
	 */
	public T findMax() {
		if (isEmpty())
			throw new RuntimeException();
		return findMax(root).element;
	}

	/**
	 * 查找节点
	 * 
	 * @return 存在则返回true 否则返回flase
	 */
	public boolean contains(T x) {
		return contains(x, root);
	}

	/**
	 * 创建一个空树
	 */
	public void makeEmpty() {
		root = null;
	}

	/**
	 * 判断树是否为空
	 * 
	 * @return 为空返回true否则返回false
	 */
	public boolean isEmpty() {
		return root == null;
	}

	/**
	 * 打印树中的节点
	 */
	public void printTree() {
		if (isEmpty())
			System.out.println("这是空树");
		else
			printTree(root);
	}

	/**
	 * 向树中插入一个节点
	 * 
	 * @param x
	 *            要插入的节点
	 * @param t
	 * @return 新树的根节点
	 */
	private BinaryNode<T> insert(T x, BinaryNode<T> t) {
		if (t == null)
			return new BinaryNode<T>(x, null, null);

		int compareResult = x.compareTo(t.element);

		if (compareResult < 0)
			t.left = insert(x, t.left);
		else if (compareResult > 0)
			t.right = insert(x, t.right);
		else
			; // Duplicate; do nothing
		return t;
	}

	/**
	 * 从树中移除一个节点
	 * 
	 * @param x
	 *            要移除的节点
	 */
	private BinaryNode<T> remove(T x, BinaryNode<T> t) {
		if (t == null)
			return t; // Item not found; do nothing

		int compareResult = x.compareTo(t.element);

		if (compareResult < 0)
			t.left = remove(x, t.left);
		else if (compareResult > 0)
			t.right = remove(x, t.right);
		else if (t.left != null && t.right != null) // Two children
		{
			t.element = findMin(t.right).element;
			t.right = remove(t.element, t.right);
		} else
			t = (t.left != null) ? t.left : t.right;
		return t;
	}

	/**
	 * 查找树中的最小的节点
	 * 
	 * @param 树的根点
	 * @return 最小节点.
	 */
	private BinaryNode<T> findMin(BinaryNode<T> t) {
		if (t == null)
			return null;
		else if (t.left == null)
			return t;
		return findMin(t.left);
	}

	/**
	 * 查找树中的最大的节点
	 * 
	 * @param 树的根点
	 * @return 最大节点.
	 */
	private BinaryNode<T> findMax(BinaryNode<T> t) {
		if (t != null)
			while (t.right != null)
				t = t.right;

		return t;
	}

	/**
	 * 判断树中是否包含节点
	 * 
	 * @param x
	 *            要查找的节点
	 * @param t
	 *            子树的根
	 * @return 要查找的节点
	 */
	private boolean contains(T x, BinaryNode<T> t) {
		if (t == null)
			return false;

		int compareResult = x.compareTo(t.element);

		if (compareResult < 0)
			return contains(x, t.left);
		else if (compareResult > 0)
			return contains(x, t.right);
		else
			return true; // Match
	}

	/**
	 * 按升序打印树
	 */
	private void printTree(BinaryNode<T> t) {
		if (t != null) {
			printTree(t.left);
			System.out.println(t.element);
			printTree(t.right);
		}
	}

	/**
	 * 返回树的高度
	 */
	private int height(BinaryNode<T> t) {
		if (t == null)
			return -1;
		else
			return 1 + Math.max(height(t.left), height(t.right));
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值