二叉查找樹 java實現

1.什麼是二叉查找樹

二叉排序樹(Binary Sort Tree),又稱二叉查找樹(Binary Search Tree),亦稱二叉搜索樹

二叉排序樹或者是一棵空樹,或者是具有下列性質的二叉樹
(1)若左子樹不空,則左子樹上所有結點的值均小於或等於它的根結點的值;
(2)若右子樹不空,則右子樹上所有結點的值均大於或等於它的根結點的值;
(3)左、右子樹也分別爲二叉排序樹;

這是什麼意思?通俗理解就是一棵二叉樹,無論是子樹還是根,他左邊的子樹只要不是空都是小於他的,他右邊只要不是空都是大於他的。
  知道這個特性實現起來就很簡單了,因爲樹裏都是按結構存的,例如找小於的情況,只需要從根遞歸左邊的數就可以找到了。
一旦位置被鎖定,插入,刪除這些常規操作就簡單了

2.二叉查找樹的實現

package Tree;

/*
 * 二叉查找樹
 * @author WLNSSS
 * @time 2017.12.26
 * */
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {
	
	/*
	 * 二叉查找樹內置節點(內部類)
	 * */
	private static class BinaryNode<AnyType> {
		//構造器
		BinaryNode(AnyType theElement) {
			this(theElement,null,null);
		}
		
		BinaryNode(AnyType theElement,BinaryNode leftTree,BinaryNode rightTree) {
			element = theElement;
			left = leftTree;
			right = rightTree;
		}
		
		//存儲的數據
		AnyType element;
		//左子樹
		BinaryNode<AnyType> left;
		//右子樹
		BinaryNode<AnyType> right;
	}
	
	// 根節點
	private BinaryNode<AnyType> root;
	
	//構造器
	public BinarySearchTree(){
		this.init();
	}
	
	public void makeEmpty(){
		this.init();
	}
	
	//初始化二叉查找樹
	public void init(){
		root = null;
	}
	
	//判斷二叉查找樹是否爲空
	public boolean isEmpty(){
		return 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;
	}
	
	//查找樹裏值的最大節點
	public AnyType findMax(){
		if(isEmpty()){
			throw new NullPointerException();
		}
		return findMax(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);
	}
	
	private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t){
		if(t != null){
			while(t.right != null){
				t = t.right;
			}
		}
		
		return t;
	}
	
	//樹的插入
	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);
		}else if(compareResult >0){
			t.right = insert(x, t.right);
		}else{
			//什麼都不做
			;
		}
		return t;
		
	}
	
	//樹的刪除
	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 static void main(String[] args) {
		
	}
	
	

}



發佈了30 篇原創文章 · 獲贊 9 · 訪問量 6090
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章