java數據結構之二叉排序樹

二叉排序樹:

   二叉排序樹的左子子樹的值小於父節點的值,右子樹的值大於父節點的值,與父節點相等的值可約定放在左或右子樹。

代碼:

package cn.agan.binarysorttree;

public class BinarySortTree {
    public static void main(String[] args) {
        int a[] = {7, 3, 10, 12, 5, 1, 9, 2};
        BSortTree bSortTree = new BSortTree();
        for (int i = 0; i < a.length; i++) {
            bSortTree.add(new Node(a[i]));
        }

        bSortTree.infixOrder();

        bSortTree.delNode(7);
//        bSortTree.delNode(5);
        System.out.println();
        bSortTree.infixOrder();
    }

}

//樹
class BSortTree {
    private Node root;
    //添加節點
    public void add(Node node) {
        if (root == null) {
            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 searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    /**
     * 返回以node爲根的二叉排序樹的最小節點的值,刪除node爲根節點的二叉排序樹的最小節點
     * @param node 傳入的節點(當作二叉排序樹的根節點)
     * @return 返回的以node爲根節點的二叉排序樹的最小節點的值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        //循環查找左子結點,直到找到最小值
        while (target.getLeft() != null) {
            target = target.getLeft();
        }
        //此時target指向最小值
        delNode(target.getValue());
        return node.getValue();
    }
    //刪除節點
    public void  delNode(int val) {
        if (root == null) {
            return;
        } else {
            Node targetNode = search(val);
            if (targetNode == null) {
                return;
            }
            //如果targetNode沒有父節點,則這個是根節點
            if (root.getLeft() == null && root.getRight() == null) {
                root = null;
            }
            //找到其父節點
            Node parent = searchParent(val);
            if (targetNode.getLeft() == null && targetNode.getRight() == null) { //葉子節點
                //判斷
                if (parent.getLeft() != null && parent.getLeft().getValue() == val) {
                    parent.setLeft(null);
                } else if (parent.getRight() != null && parent.getRight().getValue() == val) {
                    parent.setRight(null);
                }

            } else if (targetNode.getLeft() != null && targetNode.getRight() != null) { //有兩個子樹的節點
                int minVal = delRightTreeMin(targetNode.getRight());
                targetNode.setValue(minVal);

            } else { //只有一顆子樹的節點
                //如果要刪除的節點有左子節點
                if (targetNode.getLeft() != null) {
                    //如果是parent的左子節點
                    if (parent.getLeft().getValue() == val) {
                        parent.setLeft(targetNode.getLeft());
                    } else { //targetNode是parent的右子節點
                        parent.setRight(targetNode.getLeft());
                    }
                } else { //要刪除的節點有右子節點
                    if (parent.getLeft().getValue() == val) {
                        parent.setLeft(targetNode.getRight());
                    } else { //targetNode是parent的右子節點
                        parent.setRight(targetNode.getRight());
                    }

                }
            }
        }
    }

}

//節點
class Node {
    private int value;
    private Node left;
    private Node right;

    public int getValue() {
        return value;
    }

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

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }

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

    //查找要刪除的節點
    public Node search(int value) {
        if (this.value == value) {
            //找到
            return this;
        } else if (value < this.value) { //應該往當前節點的左邊查找
            if (left == null) {
                return null;
            }
            return this.left.search(value);
        } else { //只能往右邊查找了
            if (right == null) {
                return null;
            }
            return right.search(value);
        }

    }

    //查找目標節點的父節點
    public Node searchParent(int value) {
        if ((left != null && left.value == value) ||
                (right != null && right.value == value)){

            return this;
        } else { //如果查找的值小於當前節點的值,並且當前節點的左子節點不爲空
            if (value < this.value && left != null) {
                return left.searchParent(value);
            } else if (value >= this.value && right != null) {
                return right.searchParent(value); //向右子樹查找
            } else {
                return  null;
            }
        }

    }

    //add 添加節點, 遞歸的形式添加
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //
        if (node.value < this.value) {
            if (left == null) {
                left = node;
            } else {
                left.add(node);
            }
        } else {
            if (right == null) {
                right = node;
            } else {
                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 +
                '}';
    }
}

 

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