手寫一個二叉查找樹

對二叉查找樹的增刪改查進行了java實現

package basicKnowledge.tree;



/**
 * @基本功能:二叉搜索樹(二叉排序樹)
 * @program:summary
 * @author:peicc
 * @create:2019-07-24 16:52:29
 **/
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {
    private BinaryNode<AnyType> root;//根節點
    //構造函數
    public BinarySearchTree(){
        root=null;
    }
    /**
     * @功能:二叉搜索樹的結點
     * @Date: 2019/7/24
     */
     static class BinaryNode<AnyType>{
        AnyType element;//數據元素
        BinaryNode<AnyType> left;//左孩子節點
        BinaryNode<AnyType> right;//右孩子節點
        BinaryNode(AnyType element,BinaryNode<AnyType> left,BinaryNode<AnyType> right){
            this.element=element;
            this.left=left;
            this.right=right;
        }
        BinaryNode(AnyType element){
            this(element,null,null);
        }
    }
    /**
     * @功能:判斷樹是否含有某一元素
     * @Param: [x, root]
     * @return: boolean
     * @Date: 2019/7/24
     */
    public 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.left);
        }
        if(compareResult>0){
            return contains(x,root.right);
        }
        return true;
    }
    /**
     * @功能:找到最小的結點(二叉搜索樹的最小結點一定是最左結點)
     * @Param: [root]
     * @return: AnyType
     * @Date: 2019/7/24
     */
    public BinaryNode<AnyType> findMin(BinaryNode<AnyType> root){
        if(root==null){
            return null;
        }
        if(root.left==null){
            return root;
        }else{
            return findMin(root.left);
        }
    }
    /**
     * @功能:找到最大的結點(二叉搜索樹的最大結點一定是最右節點 )
     * @Param: [root]
     * @return: basicKnowledge.tree.BinarySearchTree.BinaryNode<AnyType>
     * @Date: 2019/7/24
     */
    public BinaryNode<AnyType> findMax(BinaryNode<AnyType> root){
        if(root==null){
            return null;
        }
        while(root.right!=null){
            root=root.right;
        }
        return root;
    }
    /**
     * @功能:插入結點
     * @Param: [x, root]
     * @return: basicKnowledge.tree.BinarySearchTree.BinaryNode<AnyType>
     * @Date: 2019/7/24
     */
    public BinaryNode<AnyType> insert(AnyType x,BinaryNode<AnyType> root){
        if(root==null){
            return new BinaryNode<>(x,null,null);
        }
        int compareResult=x.compareTo(root.element);
        if(compareResult<0){
            root.left=insert(x,root.left);
        }else if(compareResult>0){
            root.right=insert(x,root.right);
        }else{//結點元素值相等
            ;
        }
        return root;
    }
    /**
     * @功能:刪除結點,比較麻煩
     * @Param: [x, root]
     * @return: basicKnowledge.tree.BinarySearchTree.BinaryNode<AnyType>
     * @Date: 2019/7/24
     */
    public BinaryNode<AnyType> remove(AnyType x,BinaryNode<AnyType> root){
        if(root==null){//原樹爲空,返回空
            return null;
        }
        int compareResult=x.compareTo(root.element);
        if(compareResult<0){//待刪除結點的值小於根節點
            root.left=remove(x,root.left);//從左子樹中刪除並返回刪除後的根節點
        }else if(compareResult>0){//待刪除結點的值大於根節點
            root.right=remove(x,root.right);//從右子樹中刪除並返回刪除後的根節點
        }else if(root.left!=null&&root.right!=null){//刪除的結點有兩個子節點
            root.element=findMin(root.right).element;//待刪除結點的右子樹上最小的值代替待刪除結點的值
            root.right=remove(root.element,root.right);//此時,右子樹上最小的那個節點爲無用結點,需要將其刪除
        }else{//刪除的結點只有一個孩子或爲葉子節點
            root=(root.left!=null)?root.left:root.right;//只有一個孩子,直接刪除該結點,並用該節點的唯一子節點頂替它的位置
        }
        return root;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章