【數據結構:樹的應用】:二叉排序樹--刪除節點

1.思路分析

在這裏插入圖片描述
在這裏插入圖片描述
三種刪除節點情況:
在這裏插入圖片描述

2.代碼實現

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9,2};
        BinarySortTree binarySortTree = new BinarySortTree();
        //循環添加節點到二叉排序樹
        for(int i=0;i<arr.length;i++){
            binarySortTree.add(new Node(arr[i]));
        }

        //中序遍歷二叉排序樹
        binarySortTree.infixOrder();

        //測試刪除葉子節點
        binarySortTree.deleteNode(12);
        binarySortTree.deleteNode(1);
        binarySortTree.deleteNode(7);
        binarySortTree.deleteNode(3);
        binarySortTree.deleteNode(10);
        System.out.println("刪除節點後");
        binarySortTree.infixOrder();
    }
}

class BinarySortTree{
    private Node root;
    public void add(Node node){
        if(root==null){
            root = node;//直接讓root指向node
        }else{
            root.add(node);
        }
    }

    //中序遍歷
    public void infixOrder(){
        if(root!=null){
            root.infixOrder();
        }else{
            System.out.println("二叉排序樹爲空");
        }
    }

    //查找要刪除的節點
    public Node search(int value){
        if(root == null){
            return null;
        }else{
            return root.search(value);
        }
    }

    //查找要刪除節點的父節點
    public Node searcParent(int value){
        if(root==null){
            return null;
        }else {
            return root.searchParent(value);
        }
    }


    //刪除節點
    public void deleteNode(int value){
        if(root==null){
            return;
        }else{
            //1.找到要刪除的節點,targetNode
            Node targetNode = search(value);
            //如果沒有找到刪除的節點
            if(targetNode==null){
                return;
            }
            //如果這顆二叉排序樹只有一個節點
            if(root.left == null && root.right == null){
                root = null;
                return;
            }
            //2.找到targetNode的父節點parent
            Node parent = searcParent(value);

            if(targetNode.left==null && targetNode.right==null){//刪除的節點是葉子節點
                //判斷targetNode是父節點的左子節點還是父子節點的右子節點
                if(parent.left !=null && parent.left.value == value){
                    parent.left = null;
                }else if(parent.right !=null && parent.right.value == value){
                    parent.right = null;
                }
            }else if(targetNode.left!=null && targetNode.right!=null){//刪除兩顆子樹的節點
                int minValue = delRightTreeMIn(targetNode.right);
                targetNode.value = minValue;

            }else{//刪除只有一顆子樹的節點
                if(targetNode.left!=null){//如果要刪除的節點有左子節點
                    if(root!=null){
                        //如果要刪除的節點是父節點的左子節點
                        if(parent.left.value == value){
                            parent.left = targetNode.left;
                        }else{
                            parent.right = targetNode.left;
                        }
                    }else{
                        root = targetNode.left;
                    }

                }else{//如果要刪除的節點有右子節點
                    if(root!=null){
                        //如果要刪除的節點是父節點的左子節點
                        if(parent.left.value == value){
                            parent.left = targetNode.right;
                        }else{
                            parent.right = targetNode.right;
                        }
                    }else{
                        root = targetNode.right;
                    }
                }
            }
        }
    }

    /**
     *
     * @param node 傳入的節點
     * @return 返回的是以node爲跟節點的二叉排序樹的最小節點的值
     */
    public int delRightTreeMIn(Node node){
        Node target = node;
        //循環的查找左節點就會找到最小值
        while (target.left!=null){
            target = target.left;
        }
        //這是target就指向了最小節點的值
        //刪除最小節點
        deleteNode(target.value);
        return target.value;
    }
}

class Node{
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    //查找要刪除的節點
    public Node search(int value){
        if(value == this.value){//說明就是該節點
            return this;
        }else if(value<this.value){//如果查找的樹小於當前節點,向左子樹遞歸查找
            //如果左子節點爲空
            if(this.left==null){
                return null;
            }
            return this.left.search(value);
        }else{//查找的值不小於當前節點
            if(this.right==null){
                return null;
            }
            return this.right.search(value);
        }
    }

    //查找要刪除節點的父節點
    public Node searchParent(int value){
        //如果當前節點就是要刪除的節點的父節點
        if((this.left!=null && this.left.value == value) || (this.right!=null && this.right.value==value)){
            return this;
        }else{
            //如果查找的值小於當前節點的值
            if(value<this.value && this.left!=null){
                return this.left.searchParent(value);//向左子樹遞歸查找
            }else if(value>=this.value && this.right!=null){
                return this.right.searchParent(value);//向右子樹遞歸查找
            }else{
                return null;//沒有找到父節點
            }
        }
    }


    //添加節點
    public void add(Node node){
        if(node==null){
            return;
        }
        //判斷傳入節點的值和當前子樹根節點的值的關係
        if(node.value<this.value){
            if(this.left==null){//如果當前左子節點爲Null
                this.left = node;
            }else{
                this.left.add(node);//遞歸向左子樹添加
            }
        }else{
            if(this.right==null){//如果當前右子節點爲空
                this.right = node;
            }else {
                this.right.add(node);//遞歸向右子樹添加
            }
        }
    }

    public void infixOrder(){
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.infixOrder();
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}

在這裏插入圖片描述

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