二叉树子树的删除

package 二叉树子树的删除;

public class BinaryTree {
            //根节点
            TreeNode root;
            //设置根节点
            public void setRoot(TreeNode root) {
                this.root = root;
            }
            public TreeNode getRoot() {
                return root;
            }
            //前序
            public void frontShow()
            {
                if(root!=null) {
                root.frontShow();
                }else
                {
                    System.out.println("此二叉树为空");
                    
                }
            }
            //中序
            public void midShow()
            {
                if(root!=null) {
                root.midShow();
                }else
                {
                    System.out.println("此二叉树为空");
                    
                }
            }
            //后序
            public void afterShow() 
            {
                if(root!=null) {
                    root.afterShow();
                }else
                {
                    System.out.println("此二叉树为空");
                    
                }
            }
            //前序查找
            public  TreeNode frontSearch(int i) {
                
                return root.frontSearch(i);
            }
            public void delete(int i) {
                if(root.value==i) 
                {
                    root=null;
                }else {
                    root.delete(i);
                }
                
            }
            

           public static void main(String[] args) {
                
                //创建一棵树
                BinaryTree bt=new BinaryTree();
                //创建一个根节点
                TreeNode root=new TreeNode(1);
                //把根节点赋给树
                bt.setRoot(root);
                //创建两个节点
                TreeNode rootL=new TreeNode(2);
                TreeNode rootR=new TreeNode(3);
                //把新创建的节点设置为根节点的子节点
                root.setleftNode(rootL);
                root.setrightNode(rootR);
                //为第二层的节点创建子节点
                rootL.setleftNode(new TreeNode(4));
                rootL.setrightNode(new TreeNode(5));
                rootR.setleftNode(new TreeNode(6));
                rootR.setrightNode(new TreeNode(7));
                //遍历二叉树
                //1.前序遍历(根,左,右)
                System.out.println("此二叉树的前序遍历为:");
                bt.frontShow();
                System.out.println();
                System.out.println("此二叉树的中序遍历为:");
                bt.midShow();
                System.out.println();
                System.out.println("此二叉树的后序遍历为:");
                bt.afterShow();
                System.out.println("================");
                //二叉树的前序查找
                TreeNode result=bt.frontSearch(9);
                System.out.println(result);
                //二叉树二叉树的子树
                bt.delete(2);
                bt.frontShow();    
            }  
}
class TreeNode
{
    
    //节点的权
    int value;
    //左孩子
    TreeNode leftNode;
    //右孩子
    TreeNode rightNode;
    
    public TreeNode(int value)
    {
        this.value=value;
    }

    //前序查找
    public TreeNode frontSearch(int i) {
        
        TreeNode target=null;
        //对比当前节点的值
        if(this.value==i) 
        {
            return this;
        //当前节点中的值不是要查找的
        }else {
            //查找左节点
            if(leftNode!=null) 
            {
            //有可能可以查到,有可能查找不到,查不到的话,target还是null
                target=leftNode.frontSearch(i);
            }
            //如果不为空,则查找成功
            if(target!=null) {
                return target;
            }
            //查找右节点
            if(rightNode!=null) 
            {
                target=rightNode.frontSearch(i);
            }
        }
        return target;
        
    }

    //前序遍历(根,左,右)
    public void frontShow() {
        
        //先遍历当前节点(根节点);
        System.out.print(value);
        //左节点
        if(leftNode!=null)
        {
            leftNode.frontShow();
        }
        //右节点
        if(rightNode!=null)
        {
            rightNode.frontShow();
        }

    }
    //中序遍历(左,根,右)
    public void midShow() {
                //先遍历左节点
                if(leftNode!=null)
                {
                    leftNode.midShow();
                }
                //在遍历当前节点(根节点);
                System.out.print(value);
                //最后右节点
                if(rightNode!=null)
                {
                    rightNode.midShow();
                }
        
    }
    //后序遍历(左,右,根)
    public void afterShow() {
        //先左节点
        if(leftNode!=null)
        {
            leftNode.afterShow();
        }
        //在右节点
        if(rightNode!=null)
        {
            rightNode.afterShow();
        }
        //最后遍历当前节点(根节点);
        System.out.print(value);
    }
    //删除二叉树的子树
    public void delete(int i) {
        TreeNode parent=this;
        //判断左孩子
        if(parent.leftNode!=null&&parent.leftNode.value==i)
        {
            parent.leftNode=null;
            return;
        }
        //判断右孩子
        if(parent.rightNode!=null&&parent.rightNode.value==i) 
        {
            parent.rightNode=null;
            return;
        }
        //如果都不是,递归检查并删除左孩子
        parent=leftNode;
        if(parent!=null)
        {
            parent.delete(i);
        }
        //递归检查并删除右孩子
        parent=rightNode;
        if(parent!=null)
        {
            parent.delete(i);
        }

    }


    //设置左孩子
    public void setleftNode(TreeNode lNode) {
        this.leftNode = lNode;
    }
    //设置右孩子
    public void setrightNode(TreeNode rNode) {
        this.rightNode = rNode;
    }
    
    }

备注:运行结果大家可以自己运行,当删除1也就是root的时候,运行结果: 
          此二叉树为空 ;
发布了98 篇原创文章 · 获赞 34 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_42133768/article/details/86762389