二叉樹算法筆記:二叉排序樹(二叉搜索樹) in java

本內容僅貼出三鏈二叉樹的操做(在二叉樹的兩篇文章裏已經有了以下代碼,徹底相同,只是這裏把二叉排序樹的代碼提取出來而已)。 java

二叉樹算法筆記:二叉樹基礎操做(三鏈二叉樹) in java
http://my.oschina.net/wangchen881202/blog/195027 node

二叉樹算法筆記:二叉樹基礎操做(二鏈二叉樹) in java
http://my.oschina.net/wangchen881202/blog/195025 算法


本文不包含二叉平衡樹和紅黑樹,這兩部分將在兩個單獨的博客中貼出。 數組

 

 

二叉樹結點類,注:包含toArray()方法和toList()方法: ui

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 );
		}
	}
}

 

 

二叉排序樹操做類: this

/**
 * 二叉排序樹
 * 定義:左子樹若不爲空,左子樹均小於根節點;若右子樹不爲空,右子樹均大於根節點。左右子樹分別爲二叉排序樹。
 * 	特色:
 * 		複雜度爲: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;
	}
}

 

 


如有錯誤或不妥之處,敬請諒解並指點。 spa

相關文章
相關標籤/搜索