1. 程式人生 > >Java版資料結構之二叉排序樹

Java版資料結構之二叉排序樹

簡介

  • 新增結點
  • 查詢結點
  • 刪除結點

程式碼實現

public class MyBinarySortTree {

    int data;//結點權值
    MyBinarySortTree leftTree;//左子樹
    MyBinarySortTree rightTree;//右子樹

    public MyBinarySortTree(int data) {
        this.data = data;
        leftTree=null;
        rightTree=null;
    }

    //中序遍歷
    public void middleShow(){
        if(this==null){
            return;
        }
        if(this.leftTree!=null){
            this.leftTree.middleShow();
        }
        System.out.println(this.data);
        if(this.rightTree!=null){
            this.rightTree.middleShow();
        }
    }

    //新增一個結點
    public void addNode(MyBinarySortTree node){
        if(this==null||node==null){
            return;
        }
        if(node.data<this.data){
            if(this.leftTree==null){
                this.leftTree=node;
            }else {
                this.leftTree.addNode(node);
            }
        }
        if(node.data>this.data){
            if(this.rightTree==null){
                this.rightTree=node;
            }else{
                this.rightTree.addNode(node);
            }
        }
    }

    //查詢指定值結點
    public MyBinarySortTree search(int data){
        if(this==null){
            return null;
        }
        if(data==this.data){
            return this;
        }
        if(data<this.data&&this.leftTree!=null){
            return this.leftTree.search(data);
        }
        if(data>this.data&&this.rightTree!=null){
            return this.rightTree.search(data);
        }
        return null;
    }

    //獲取指定值結點的父節點
    public MyBinarySortTree getParent(int data){
        if(this==null){
            return null;
        }
        if(data<this.data&&this.leftTree!=null){
            if(this.leftTree.data==data){
                return this;
            }else{
                return this.leftTree.getParent(data);
            }
        }
        if(data>this.data&&this.rightTree!=null){
            if(this.rightTree.data==data){
                return this;
            }else{
                return this.rightTree.getParent(data);
            }
        }
        return null;
    }

    //找到刪除結點右子樹中最小的
    public MyBinarySortTree getRightMin(){
        if(this==null){
            return null;
        }
        if(this.leftTree!=null){
            return this.leftTree.getRightMin();
        }
        return this;
    }

    //刪除指定值結點
    public void delete(int data){
        if(this==null){
            return;
        }
        //找到要刪除的結點
        MyBinarySortTree node = search(data);
        //如果結點不存在,直接返回
        if(node==null){
            return;
        }
        //獲取刪除結點的父結點
        MyBinarySortTree parent = this.getParent(data);
        //刪除結點為葉子結點
        if(node.leftTree==null && node.rightTree==null){
            if(parent!=null&&parent.leftTree==node){
                parent.leftTree=null;
            }
            if(parent!=null&&parent.rightTree==node){
                parent.rightTree=null;
            }
        }
        //刪除結點為包含一個結點
        //只有左結點
        if(node.leftTree!=null&&node.rightTree==null){
            if(parent!=null&&parent.leftTree==node){
                parent.leftTree=node.leftTree;
            }
            if(parent!=null&&parent.rightTree==node){
                parent.rightTree=node.leftTree;
            }
        }
        //只有右結點
        if(node.rightTree!=null&&node.leftTree==null){
            if(parent!=null&&parent.leftTree==node){
                parent.leftTree=node.rightTree;
            }
            if(parent!=null&&parent.rightTree==node){
                parent.rightTree=node.rightTree;
            }
        }
        //刪除結點為包含兩個結點
        if(node.leftTree!=null&&node.rightTree!=null){
            //找到刪除結點右子樹中最小的
            MyBinarySortTree rightMin = node.rightTree.getRightMin();
            //刪除最小
            this.delete(rightMin.data);
            //將最小的賦值給刪除結點
            node.data=rightMin.data;
        }
    }
}