二叉树算法笔记:二叉排序树(二叉搜索树) in java

本内容仅贴出三链二叉树的操作(在二叉树的两篇文章里已经有了如下代码,完全相同,只是这里把二叉排序树的代码提取出来而已)。

二叉树算法笔记:二叉树基础操作(三链二叉树) in java
http://my.oschina.net/wangchen881202/blog/195027

二叉树算法笔记:二叉树基础操作(二链二叉树) in java
http://my.oschina.net/wangchen881202/blog/195025


本文不包含二叉平衡树和红黑树,这两部分将在两个单独的博客中贴出。

 

 

二叉树结点类,注:包含toArray()方法和toList()方法:

public class BinaryTreeNode {
	
	private BinaryTreeNode leftChild;
	private BinaryTreeNode rightChild;
	private BinaryTreeNode parent;
	private int value;
	
	public BinaryTreeNode(){
	}

	public BinaryTreeNode( int value ){
		this.value = value;
	}
	
	public BinaryTreeNode getLeftChild() {
		return leftChild;
	}

	public void setLeftChild(BinaryTreeNode leftChild) {
		this.leftChild = leftChild;
	}

	public BinaryTreeNode getRightChild() {
		return rightChild;
	}

	public void setRightChild(BinaryTreeNode rightChild) {
		this.rightChild = rightChild;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public BinaryTreeNode getParent() {
		return parent;
	}

	public void setParent(BinaryTreeNode parent) {
		this.parent = parent;
	}

	public LinkedList<BinaryTreeNode> toList(){
		LinkedList<BinaryTreeNode> list = new LinkedList<BinaryTreeNode>();
		toList( this , list );
		return list;
	}
	
	private void toList( BinaryTreeNode root , List<BinaryTreeNode> list ){
		if( root != null ){
			list.add( root );
			toList( root , list );
			toList( root , list );
		}
	}
	
	public int[] toArray(){
		int[] array = new int[BinaryTree2Links.getSubNodeNum(this) + 1 ];
		toArray( this , array , 0 );
		return array;
	}
	
	private void toArray( BinaryTreeNode root , int[] array, int i ){
		if( root != null ){
			array[i++] = root.getValue();
			toArray( root , array , i );
			toArray( root , array , i );
		}
	}
}

 

 

二叉排序树操作类:

/**
 * 二叉排序树
 * 定义:左子树若不为空,左子树均小于根节点;若右子树不为空,右子树均大于根节点。左右子树分别为二叉排序树。
 * 	特点:
 * 		复杂度为:O(logn)
 * 			最坏情况:O(n)
 * 	由此引申出来的概念:平衡二叉树、红黑树
 * 
 * @author CheN
 *
 */
public class BinarySortingTree {
	/**
	 * 将数组转化为二叉排序树
	 * @param array
	 */
	public static void buildBinarySortingTree( int[] array ){
		BinaryTreeNode root = new BinaryTreeNode();
		root.setValue(array[0]);
		for( int i = 1 ; i < array.length ; i++ ){
			addBinarySortingTreeNode( root , array[i] );
		}
	}
	
	/**
	 * 将二叉树转化为二叉排序树简单二叉排序树
	 * @param root
	 */
	public static void buildBinarySortingTree( BinaryTreeNode root ){
		List<BinaryTreeNode> list = root.toList();
		for(int i = 0 ; i < list.size() ; i++ ){
			if( list.get(i) == root )
				continue;
			addBinarySortingTreeNode( root , list.get(i).getValue() );
		}
	}
	
	/**
	 * 将数组转化为二叉排序树,注:array中不要包含root已赋值给root的值,否则会多出一个值
	 * @param root
	 * @param array
	 */
	public static void buildBinarySortingTree( BinaryTreeNode root , int[] array ){
		for(int i = 0 ; i < array.length ; i++ ){
			addBinarySortingTreeNode( root , array[i] );
		}
	}
	
	/**
	 * 将二叉树转化为二叉排序树简单二叉排序树:newRoot须为原树中的某一结点
	 * @param root
	 * @param newRoot
	 */
	public static void buildBinarySortingTree( BinaryTreeNode root , BinaryTreeNode newRoot ){
		List<BinaryTreeNode> list = root.toList();
		for(int i = 0 ; i < list.size() ; i++ ){
			if( list.get(i) == newRoot )
				continue;
			addBinarySortingTreeNode( newRoot , list.get(i).getValue() );
		}
	}
	
	/**
	 * 将指定的元素插入二叉排序树中
	 * @param root
	 * @param target
	 */
	private static void addBinarySortingTreeNode( BinaryTreeNode root , int target ){
		int value = root.getValue();
		if( target < value ){//插入右子树
			if(root.getLeftChild()==null){
				BinaryTreeNode node=new BinaryTreeNode(target);
				root.setLeftChild(node);
			}else{
				addBinarySortingTreeNode( root.getLeftChild() , target );
			}
		}else if(target>value){
			if(root.getRightChild()==null){
				BinaryTreeNode node=new BinaryTreeNode(target);
				root.setRightChild(node);
			}else{
				addBinarySortingTreeNode( root.getRightChild() , target );
			}
		}
	}
	
	/**
	 * 查找二叉排序树
	 * @param root
	 * @param target
	 */
	public static BinaryTreeNode BinarySerch( BinaryTreeNode root,int target ){
		if( root == null ){
			return null;
		}else if(root.getValue()==target){
			return root;
		}else if(root.getValue()>target){
			 return BinarySerch(root.getLeftChild(),target);
		}else{
			 return BinarySerch(root.getRightChild(),target);
		}
	}
	
	/**
	 * 获取二叉排序树最小值(即最左侧结点)
	 * @param root
	 * @return
	 */
	public static BinaryTreeNode getMinFromBinarySortingTree( BinaryTreeNode root ){
		if( root.getLeftChild() == null ){
			return root;
		}else{
			return getMinFromBinarySortingTree( root.getLeftChild() );
		}
	}

	/**
	 * 获取二叉排序树最大值(即最右侧结点)
	 * @param root
	 * @return
	 */
	public static BinaryTreeNode getMaxFromBinarySortingTree( BinaryTreeNode root ){
		if( root.getRightChild() == null ){
			return root;
		}else{
			return getMaxFromBinarySortingTree( root.getRightChild() );
		}
	}

	/**
	 * 在二叉排序树上增加结点
	 * @param root
	 * @param node
	 * @return
	 */
	public static boolean addNodeToBinarySortingTree( BinaryTreeNode root , BinaryTreeNode node ){
		if( root.getValue() > node.getValue() ){
			if( root.getLeftChild() == null ){
				BinaryTree3Links.addLeftChild(root, node);
				return true;
			}
			return addNodeToBinarySortingTree( root.getLeftChild() , node );
		}else{
			if( root.getRightChild() == null ){
				BinaryTree3Links.addRightChild(root, node);
				return true;
			}
			return addNodeToBinarySortingTree( root.getRightChild() , node );
		}
	}

	/**
	 * 在二叉排序树上删除节点
	 * @param node
	 * @return
	 */
	public static boolean deleteNodeFromBinarySortingTree( BinaryTreeNode node ){
		BinaryTreeNode parent = node.getParent();
		BinaryTreeNode rightChild = node.getRightChild();
		BinaryTreeNode leftChild = node.getLeftChild();
		if( BinaryTree3Links.isLeaf(node) ){//如果是叶子结点,则直接删了
			if( BinaryTree3Links.isLeftChild(node))
				parent.setLeftChild(null);
			else
				parent.setRightChild(null);
			node.setParent(null);
			return true;
		}else{
			if( leftChild == null ){//如果只有右结点,则将右结点与父结点相连
				if( BinaryTree3Links.isLeftChild(node)){
					parent.setLeftChild(rightChild);
				}else{
					parent.setLeftChild(leftChild);
				}
				rightChild.setParent(parent);
				node.setRightChild(null);
				node.setParent(null);
				return true;
			}else if(rightChild == null ){//如果只有左结点,则将左结点与父结点相连
				if( BinaryTree3Links.isLeftChild(node)){
					parent.setRightChild(rightChild);
				}else{
					parent.setRightChild(leftChild);
				}
				leftChild.setParent(parent);
				node.setLeftChild(null);
				node.setParent(null);
				return true;
			}else{//若左右子树都存在,则在左子树中找到最大的一个结点替代这个结点,若该节点有左子树,则将其左子树与其父结点连接
				BinaryTreeNode temp = leftChild;
				while( temp.getRightChild() != null ){
					temp = temp.getRightChild();
				}
				if( temp.getLeftChild() != null ){
					BinaryTreeNode tempParent = temp.getParent();
					temp.getLeftChild().setParent(tempParent);
					tempParent.setRightChild(temp.getLeftChild());
				}
				temp.setParent(parent);
				temp.setLeftChild(leftChild);
				temp.setRightChild(rightChild);
				node.setParent(null);
				node.setLeftChild(null);
				node.setRightChild(null);
			}
		}
		return false;
	}
}

 

 


若有错误或不妥之处,敬请谅解并指点。

 

转载于:https://my.oschina.net/wangchen881202/blog/196067

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值