zoukankan      html  css  js  c++  java
  • 二叉排序树

    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;
                }
            }
        }
    
    }
  • 相关阅读:
    一篇文章讲清楚markdown
    webservice初体验-cxf
    IReport与web项目整合
    泛型
    观察者模式
    策略模式
    设计模式与面向对象
    JavaI/O(输入/输出)
    面向对象
    Java基础类库
  • 原文地址:https://www.cnblogs.com/susexuexi011/p/14727089.html
Copyright © 2011-2022 走看看