JAVA之查找二叉树ADT

查找二叉树ADT性质:对于树中每个节点X,它的左子树所有项的值小于X中的项而它的右子树所有项的值大于X中的项

/*
 * 查找二叉树ADT:
 * 性质:对于树中每个节点X,它的左子树所有项的值小于X中的项
 * 	而它的右子树所有项的值大于X中的项
 */
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {
	/*
	 * 树节点元素
	 */
	private static class BinaryNode<AnyType>{
		AnyType element;	//The data in the node(数据)
		BinaryNode<AnyType> left;	//lefi Child(左孩子:必须要比祖先节点小)
		BinaryNode<AnyType> right;	//right Child(右孩子:必须要比祖先节点大)
		public BinaryNode(AnyType theElement){
			this(theElement, null, null);
		}
		public BinaryNode(AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt){
			element = theElement;
			left = lt;
			right = rt;
		}
	}
	
	private BinaryNode<AnyType> root;	//根节点
	
	public boolean isEmpty(){
		return root == null;
	}
	
	public BinarySearchTree(){
		root = null;
	}
	
	public void makeEmpty(){
		root = null;
	}
	
	public boolean contains(AnyType x){
		return contains(x, root);
	}
	
	/*
	 * 利用递归的思想来递归调用
	 */
	private boolean contains(AnyType x, BinaryNode<AnyType> 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;
	}
	
	/*
	 * 取树中的最小值
	 */
	public AnyType findMin(){
		if(isEmpty())
			throw new NullPointerException();
		return findMin(root).element;
	}
	
	private BinaryNode<AnyType> findMin(BinaryNode<AnyType> t){
		if(t == null)
			return null;
		else if(t.left == null)
			return t;
		return(findMin(t.left));
	}
	/*
	 * 取树中的最大值
	 */
	public AnyType findMax(){
		if(isEmpty())
			throw new NullPointerException();
		return findMax(root).element;
	}
	
	private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t){
		if(t == null)
			return null;
		else if(t.right == null)
			return t;
		return findMax(t.right);
	}
	
	/*
	 * 插入数据
	 */
	public void insert(AnyType x){
		root = insert(x, root);
	}
	
	private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t){
		if(t == null)
			return new BinaryNode<>(x, null, null);
		
		int compareResult = x.compareTo(t.element);
		
		if(compareResult < 0)
			t.left = insert(x, t.left);
		if(compareResult > 0)
			t.right = insert(x, t.right);
		else
			;
			return t;
	}
	
	/*
	 * 删除节点
	 */
	public void remove(AnyType x){
		root = remove(x, root);
	}
	private BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t){
		if(t == null)
			return t;
		
		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){
			t.element = findMin(t.right).element;
			t.right = remove(t.element, t.right);
		}else
			t = (t.left != null)? t.left : t.right;
		return t;
	}
	
	public void printTree(){
		if(isEmpty())
			System.out.println("空树");
		else
			printTree(root);
	}
	
	private void printTree(BinaryNode<AnyType> t){
		if(t != null){
			printTree(t.left);
			System.out.print(t.element+" ");
			printTree(t.right);
		}
	}
	
/*
 *		测试代码
*/
	public static void main(String[] args){
		BinarySearchTree<Integer> bst = new BinarySearchTree<>();
		bst.insert(6);
		bst.insert(2);
		bst.insert(8);
		bst.insert(1);
		bst.insert(4);
		bst.insert(3);
		bst.printTree();
		System.out.println("最小值:" + bst.findMin());
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * 二叉树节点ADT接口 */ package dsa; public interface BinTreePosition extends Position { //判断是否有父亲(为使代码描述简洁) public boolean hasParent(); //返回当前节点的父节点 public BinTreePosition getParent(); //设置当前节点的父节点 public void setParent(BinTreePosition p); //判断是否为叶子 public boolean isLeaf(); //判断是否为左孩子(为使代码描述简洁) public boolean isLChild(); //判断是否有左孩子(为使代码描述简洁) public boolean hasLChild(); //返回当前节点的左孩子 public BinTreePosition getLChild(); //设置当前节点的左孩子(注意:this.lChild和c.parent都不一定为空) public void setLChild(BinTreePosition c); //判断是否为右孩子(为使代码描述简洁) public boolean isRChild(); //判断是否有右孩子(为使代码描述简洁) public boolean hasRChild(); //返回当前节点的右孩子 public BinTreePosition getRChild(); //设置当前节点的右孩子(注意:this.rChild和c.parent都不一定为空) public void setRChild(BinTreePosition c); //返回当前节点后代元素的数目 public int getSize(); //在孩子发生变化后,更新当前节点及其祖先的规模 public void updateSize(); //返回当前节点的高度 public int getHeight(); //在孩子发生变化后,更新当前节点及其祖先的高度 public void updateHeight(); //返回当前节点的深度 public int getDepth(); //在父亲发生变化后,更新当前节点及其后代的深度 public void updateDepth(); //按照中序遍历的次序,找到当前节点的直接前驱 public BinTreePosition getPrev(); //按照中序遍历的次序,找到当前节点的直接后继 public BinTreePosition getSucc(); //断绝当前节点与其父亲的父子关系 //返回当前节点 public BinTreePosition secede(); //将节点c作为当前节点的左孩子 public BinTreePosition attachL(BinTreePosition c); //将节点c作为当前节点的右孩子 public BinTreePosition attachR(BinTreePosition c); //前序遍历 public Iterator elementsPreorder(); //中序遍历 public Iterator elementsInorder(); //后序遍历 public Iterator elementsPostorder(); //层次遍历 public Iterator elementsLevelorder(); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值