ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

二叉排序树

2021-05-03 11:04:37  阅读:85  来源: 互联网

标签:Node null value 二叉 right 排序 root 节点


package BinarrSortTree;

public class BSTTreeDemo01 {
    /*
    二叉树排序树:比根节点小的数在左边,大的在右边
     */
    public static void main(String[] args) {

    }
}

//创建二叉树类
class  BinarySortTree{
   private Node root;

    public void setRoot(Node root) {
        this.root = root;
    }

    public Node getRoot() {
        return root;
    }
    //添加节点的方法
    public void add(Node node){
       if(root==null){
           root=node;
       }else{
           this.root.addNode(node);
       }
   }
    //中序遍历
   public void midOrder(){
        if(this.root!=null){
            this.root.midOrder();
        }else{
            System.out.println("该树为空");
        }
   }


   //删除节点:
    //1.查找要删除的节点
    public  Node search(int value){
        if(root==null){
            return null;
        }
        return root.search(value);
    }
    //2.查找要删除节点的父节点
    public Node searchParent(int value){
        if (root==null){
            return null;
        }else{
            return root.searchParent(value);
        }
    }
    //3.删除节点(返回删除的节点)
    public Node deleteNode(int value){
        if(root==null){
            return null;
        }else{
            Node targetNode = root.search(value);
            Node ordinalNode = targetNode;
            Node parentNode = root.searchParent(value);
            if(root.value==value && root.left==null && root.right==null){
                //要删除的节点是根节点
                root=null;
            }
            if(targetNode==null) {
                return null;
            }else {
                if(targetNode.left==null&&targetNode.right==null){
                    //要删除的节点是叶子结点
                    if(parentNode.left!=null && parentNode.left.value==value){
                        parentNode.left=null;
                    }else if(parentNode.right!=null && parentNode.right.value==value){
                        parentNode.right=null;
                    }
                }else if(targetNode.right!=null && targetNode.left!=null){
                    //要删除的节点有两个子节点
                    //需要找到其右子节点向左查找的最小的节点的值(或者左子节点向右查找的最大值)写一个方法来完成
                    //将上一步查找的值替换掉待删除节点的值,删除最小值节点(相当于待删除节点替换为查找到的该节点
                    // 保留了BST的特性)
                    int targetvalue = delRightMinvalue(targetNode.right);
                    targetNode.value=targetvalue;
                }else{
                    //要删除的节点只有一个子节点

                    if(targetNode.left!=null){
                        //要删除的节点有左子节点
                        if (parentNode!=null){
                            if(parentNode.left.value==value){
                                //待删除的的节点为父节点的左子节点
                                parentNode.left=targetNode.left;
                            }else{
                                parentNode.right=targetNode.left;
                            }
                        }else{
                            root=targetNode.left;
                        }

                    }else{
                        //要删除的节点为右子节点
                        if(parentNode!=null){
                            if(parentNode.left.value==value){
                                parentNode.left=targetNode.right;
                            }else{
                                parentNode.right=targetNode.right;
                            }
                        }else{
                            root=targetNode.right;
                        }

                    }
                }
            }

            return ordinalNode;
        }

    }
    public int delRightMinvalue(Node node){
        Node target = node;
        int value=target.value;
        while(target.left!=null){
            target=target.left;
        }
        deleteNode(value);
        return value;
    }
}

//创建节点类
class Node{
    int value;
    Node left;
    Node right;
    public Node(){

    }
    public Node(int value){
        this.value=value;
    }
    //添加节点的方法
    //要求满足二叉排序树的要求
    public void addNode(Node node){
        if(node==null){
            return;
        }
        if(node.value<this.value){
            if(this.left==null){
                this.left=node;
            }else{
                this.left.addNode(node);
            }
        }else{
            if(this.right==null){
                this.right=node;
            }else{
                this.right.addNode(node);
            }
        }
    }
    //中序遍历
    public void midOrder(){
        if(this.left!=null){
            this.left.midOrder();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.midOrder();
        }
    }



    //删除节点:
    //1.查找要删除的节点
    public  Node search(int value){//value:要删除节点的值
        if(value==this.value){
            return this;
        }else if(value<this.value){
            if(this.left!=null){
                return this.left.search(value);
            }else{
                return null;
            }
        }else if(value>this.value){
            if(this.right!=null){
                return this.right.right.search(value);
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
    //2.查找要删除节点的父节点
    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;
            }
        }
    }

}

 

标签:Node,null,value,二叉,right,排序,root,节点
来源: https://www.cnblogs.com/susexuexi011/p/14727089.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有