二叉排序樹

package BST;


public class Binary{
    public static void main(String[] args) {
        int[] array = {6,2,8,3,9,1,4,7};
        Tree tree = new Tree();
        for (int i = 0;i<array.length;i++){
            tree.add(new Node(array[i]));
        }
        tree.infixOrder();
        System.out.println("根節點為:"+tree.getRoot());
        Node sN =tree.search(3);
        System.out.println(sN);
        Node sP = tree.serachP(2);
        System.out.println(sP);
        tree.delNode(7);
        tree.infixOrder();
        System.out.println("********************************");
        tree.delNode(3);
        tree.infixOrder();
        System.out.println("********************************");
        tree.delNode(6);
        tree.infixOrder();
        System.out.println("根節點為:"+tree.getRoot());
    }
}

/**
 * 
 */
class Tree{
    Node root;
    //獲取右子樹中最小的值,并刪除此結點,返回該值
    public int delRMax(Node node){
        Node temp = node.right;
        while(temp.left!=null){
            temp = temp.left;
        }
        delNode(temp.value);
        return temp.value;
    }
    //刪除結點
    public void delNode(int value){
        if (root==null){
            System.out.println("空的二叉樹~~");
            return;
        }
        //獲取目標結點
        Node node = root.searchNode(value);
        if (node==null){
            System.out.println("不存在該節點~~");
        }
        //獲取目標父節點
        Node nodeP = root.searchParent(value);
        //葉子結點
        if (node.left==null&&node.right==null){
            if(nodeP.left!=null&&nodeP.left.value==value){
                nodeP.left=null;
            }else if(nodeP.right!=null&&nodeP.right.value==value){
                nodeP.right=null;
            }
        }else if(node.left==null||node.right==null){//有一個子樹
                if (node.left!=null){
                    if(nodeP!=null) {//防止根節點一個子樹的情況空指針
                        if (nodeP.left.value == value) {
                            nodeP.left = node.left;
                        } else {
                            nodeP.right = node.left;
                        }
                    }else{
                        root = node.left;
                    }
                }else{
                    if(nodeP!=null) {
                        if (nodeP.left.value == value) {
                            nodeP.left = node.right;
                        } else {
                            nodeP.right = node.right;
                        }
                    }else{
                        root = node.right;
                    }
                }
        }else{//有兩顆子樹
            int val = delRMax(node);
            node.value=val;
        }
    }
    //封裝
    public void add(Node node){
        if (root==null){
            root = node;
        }else{
            root.add(node);
        }
    }
    public void infixOrder(){
        if(root==null){
            return;
        }else{
            root.infixOrder();
        }
    }
    public int getRoot(){
        if(root==null){
            return -1;
        }
        else {
            return root.value;
        }
    }
    //尋找節點
    public Node search(int value){
        if(root==null){
            return null;
        }else{
            return root.searchNode(value);
        }
    }
    //尋找父節點
    public Node serachP(int value){
        if (root==null){
            return null;
        }
        else{
            return root.searchParent(value);
        }
    }
}

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

    public Node(int value){
        this.value = value;
    }
    public void add(Node node){
        if(node==null){
            return;
        }
        if(this.value > node.value){
            if (this.left==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.value);
        if(this.right!=null){
            this.right.infixOrder();
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
    public Node searchNode(int value){
        if (this.value==value){
            return this;
        }
        if (this.value>value){
            if (this.left!=null) {
                return this.left.searchNode(value);
            }else {
                return null;
            }
        }else{
            if (this.right!=null) {
                return this.right.searchNode(value);
            }else{
                return null;
            }
        }
    }
    public Node searchParent(int value){
        if((this.left!=null&&this.left.value==value)||(this.right!=null&&this.right.value==value)){
            return this;
        }
        if(this.value>value&&this.left!=null){
            return this.left.searchParent(value);
        }else if(this.value<=value&&this.right!=null){
            return this.right.searchParent(value);
        }else{return null;}
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 每日5點 1.今天是休息日,雖然早上還是到點就醒了,連著3天跑不腿有點吃不消了,所以今天就還是休息吧,讓身體得到緩...
    淡墨非痕閱讀 155評論 0 0
  • 一個人不想努力的時候,你怎么幫他都沒有用! 一個人不想被點燃的時候,你怎么燃燒都沒有用! 自己想醒...
    憧趣閱讀 358評論 0 6
  • 世界很大,把心思放在琢磨別人、算計別人、嫉妒別人上,是很傻的,人要成功,必須克服這種人性的弱點。 你說你姐們賺了十...
    iJoyce閱讀 190評論 1 1
  • 今天青石的票圈出鏡率最高的,莫過于張藝謀的新片終于定檔了。 一張滿溢著水墨風的海報一次次的出現在票圈里,也就是老謀...
    青石電影閱讀 10,377評論 1 2
  • 一、jQuery簡介 JQ是JS的一個優秀的庫,大型開發必備。在此,我想說的是,JQ里面很多函數使用和JS類似,所...
    Welkin_qing閱讀 12,382評論 1 6