My集合框架第二弹 二叉树的实现

package com.wpr.collection;

import java.util.NoSuchElementException;

public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {

	private static class BinaryNode<AnyType> {
		AnyType element;
		BinaryNode<AnyType> left;
		BinaryNode<AnyType> right;
		
		public BinaryNode(AnyType element) {
			this(element,null,null);
		}

		public BinaryNode(AnyType element2,BinaryNode<AnyType> left, BinaryNode<AnyType> right) {
			this.element = element2;
			this.left = left;
			this.right = right;
		}
	}

	private BinaryNode<AnyType> root;

	public BinarySearchTree() {
		root = null;
	}

	public void makeEmpty() {
		root = null;
	}

	public boolean isEmpty() {
		return root == null;
	}

	/**
	 * 查找二叉树中是否含有x
	 * 
	 * @param x
	 * @return
	 */
	public boolean contains(AnyType x) {
		return contains(x, root);
	}

	/**
	 * 元素x是否包含在root的树中,递归的方式
	 * 
	 * @param x
	 * @param root2
	 * @return
	 */
	/*
	 * private boolean contains(AnyType x, BinaryNode<AnyType> root) { if(root
	 * == null) return false;
	 * 
	 * int compareResult = x.compareTo(root.element);
	 * 
	 * if(compareResult>0) return contains(x,root.right); else
	 * if(compareResult<0) return contains(x,root.left); else return true; }
	 */

	/**
	 * 元素x是否包含在root的树中,非递归的方式
	 * 
	 * @param x
	 * @param root2
	 * @return
	 */
	private boolean contains(AnyType x, BinaryNode<AnyType> root) {

		while (root != null) {
			if (root == null)
				return false;

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

			if (compareResult > 0)
				root = root.right;
			else if (compareResult < 0)
				root = root.left;
			else
				return true;
		}
		return false;
	}
	/**
	 * 查找二叉树中的最小元素
	 * @return
	 */
	public AnyType findMin(){
		if(isEmpty())
			throw new NoSuchElementException();
		return findMin(root).element;
	}
	/**
	 * 查找以p为根的二叉树中的最小值
	 * @param p
	 * @return
	 */
	private BinaryNode<AnyType> findMin(BinaryNode<AnyType> p) {
		while(p.left!=null){
			p=p.left;
		}
		return p;
	}
	
	/**
	 * 查找二叉树中的最大元素
	 * @return
	 */
	public AnyType findMax(){
		if(isEmpty())
			throw new NoSuchElementException();
		return findMax(root).element;
	}
	/**
	 * 查找以p为根的二叉树中的最大值
	 * @param p
	 * @return
	 */
	private BinaryNode<AnyType> findMax(BinaryNode<AnyType> p) {
		while(p.right!=null){
			p=p.right;
		}
		return p;
	}
	/**
	 * 在二叉树中插入一个新的节点
	 * @param x
	 */
	public void insert(AnyType x){
		root = insert(x,root);
	}
	/**
	 * 将新节点x插入到以p为根节点的二叉树中,递归的方式
	 * @param x 新节点
	 * @param p	根节点
	 * @return  存在不插入,不存在按照顺序插入
	 */
	private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> p) {
		//根节点为null的情况
		if(p==null)	
			return new BinaryNode<AnyType>(x,null,null);
		
		int compareResult = x.compareTo(p.element);

		if (compareResult > 0)
			p.right = insert(x,p.right);
		else if (compareResult < 0)
			p.left = insert(x,p.left);
	
		return p;
	}
	/**
	 * 将数据x,从二叉树中删除
	 * @param x
	 */
	public void remove(AnyType x){
		root = remove(x,root);
	}
	/**
	 * 从以p为根节点的二叉树中删除元素x
	 * 	存在以下情况:
	 * 		1.不存在要删除的节点,返回null
	 * 		2.要删除的节点是叶子节点,直接删除
	 * 		3.要删除的节点有一个儿子节点,将该节点的父节点调整链绕过该节点即可
	 * 		4.要删除的节点有两个儿子,采用右子树的最小的数据代替该节点的数据,并递归地删除那个节点(该最小节点无左节点)
	 * @param x 要删除的节点
	 * @param p 根节点
	 * @return
	 */
	private BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> p) {
		//元素x没在树中,返回null
		if(p==null)
			return null;

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

		if (compareResult > 0)
			p.right = remove(x,p.right);
		else if (compareResult < 0)
			p.left = remove(x,p.left);
		else if(p.left!=null&&p.right!=null){
			//存在2个儿子节点
			BinaryNode<AnyType> minNode = findMin(p.right);
			p.element = minNode.element;
			p.right = remove(p.element,p.right);
		}else
			p=(p.left!=null)?p.left:p.right;
		return p;
	}
	/**
	 * 打印树
	 */
	public void printTree(){
		if(isEmpty()){
			System.out.println("Empty Tree");
		}
		printTree(root);
	}
	/**
	 * 中序遍历树
	 * @param t
	 */
	private void printTree(BinaryNode<AnyType> t) {
		if(t!=null){
			System.out.print(t.element+"	");
			printTree(t.left);
			printTree(t.right);
		}
	}
	
}
原文地址:https://www.cnblogs.com/kakaxisir/p/4311665.html