java实现二叉查找树

package ustc.zyy.ArrayList;

/**
 * @author zhang yin ye 
 * @date 2014 6 17
 * 
 */

import java.util.DuplicateFormatFlagsException;

public class BinarySearchTree<E extends Comparable<? super E>> {
	// 树根
	private BinaryNode<E> root;

	public BinarySearchTree() {
		root = null;
	}

	// 使得树为空
	public void makeEmpty() {
		root = null;
	}

	// 判断树是不是空的
	public boolean isEmpty() {
		return root == null;
	}

	// 获得指定节点的元素值
	private E elementAt(BinaryNode<E> t) {
		return t == null ? null : t.element;
	}

	// 删除从跟节点开始的最小节点
	public void removeMin() {
		root = removeMin(root);
	}

	// 上面调用了这个受保护的方法 传递一个BinaryNode<E>类型的节点
	protected BinaryNode<E> removeMin(BinaryNode<E> t) {
		if (t == null)
			throw new ArrayIndexOutOfBoundsException();
		else if (t.left != null) {
			// 递归调用 直到没有左子节点为止
			t.left = removeMin(t.left);
			return t;
		} else
			// 要是没有左节点 直接返回右节点
			return t.right;

	}

	public void insert(E e) {
		root = insert(e, root);
	}

	// 递归的插入元素x
	protected BinaryNode<E> insert(E x, BinaryNode<E> t) {
		if (t == null)
			// 要是根节点为空 则以新插入的节点为根
			t = new BinaryNode<E>(x);
		else if (x.compareTo(t.element) < 0)
			t.left = insert(x, t.left);
		else if (x.compareTo(t.element) > 0)
			t.right = insert(x, t.right);
		else
			// 相等就抛出异常
			throw new DuplicateFormatFlagsException(x.toString());
		return t;
	}

	public E findMin() {
		return elementAt(findMin(root));
	}

	protected BinaryNode<E> findMin(BinaryNode<E> t) {
		if (t != null)
			while (t.left != null)
				t = t.left;
		return t;
	}

	public E findMax() {
		return elementAt(findMax(root));
	}

	protected BinaryNode<E> findMax(BinaryNode<E> t) {
		if (t != null)
			while (t.right != null)
				t = t.right;
		return t;
	}

	public E find(E x) {
		return elementAt(find(x, root));
	}

	protected BinaryNode<E> find(E x, BinaryNode<E> t) {
		while (t != null) {
			if (x.compareTo(t.element) < 0)
				t = t.left;
			else if (x.compareTo(t.element) > 0)
				t = t.right;
			else
				return t;
		}
		return null;
	}

	public void remove(E x) {
		root = remove(x, root);
	}

	//
	protected BinaryNode<E> remove(E x, BinaryNode<E> t) {
		if (t == null)
			// throw new ArithmeticException();
			System.out.print("根节点为空节点");
		if (x.compareTo(t.element) < 0)
			t.left = remove(x, t.left);
		else if (x.compareTo(t.element) > 0)
			t.right = remove(x, t.right);
		else if (t.left != null && t.right != null) {
			t.element = findMin(t.right).element;
			t.right = removeMin(t.right);
		} else {
			t = (t.left != null) ? t.left : t.right;
		}
		return t;

	}

}

二叉查找树(Binary Search Tree)是一种常见的数据结构,它的每个节点最多有两个子节点,左子节点的值小于父节点的值,右子节点的值大于父节点的值。Java 实现二叉查找树的代码如下: ``` public class BinarySearchTree { private Node root; private class Node { private int key; private Node left; private Node right; public Node(int key) { this.key = key; } } public void insert(int key) { root = insert(root, key); } private Node insert(Node x, int key) { if (x == null) { return new Node(key); } if (key < x.key) { x.left = insert(x.left, key); } else if (key > x.key) { x.right = insert(x.right, key); } return x; } public boolean contains(int key) { return contains(root, key); } private boolean contains(Node x, int key) { if (x == null) { return false; } if (key == x.key) { return true; } else if (key < x.key) { return contains(x.left, key); } else { return contains(x.right, key); } } public void delete(int key) { root = delete(root, key); } private Node delete(Node x, int key) { if (x == null) { return null; } if (key < x.key) { x.left = delete(x.left, key); } else if (key > x.key) { x.right = delete(x.right, key); } else { if (x.left == null) { return x.right; } else if (x.right == null) { return x.left; } else { Node t = x; x = min(t.right); x.right = deleteMin(t.right); x.left = t.left; } } return x; } private Node min(Node x) { if (x.left == null) { return x; } else { return min(x.left); } } private Node deleteMin(Node x) { if (x.left == null) { return x.right; } x.left = deleteMin(x.left); return x; } } ``` 这段代码实现二叉查找树的插入、查找和删除操作。其中,插入操作使用递归实现,查找和删除操作也是递归实现的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值