二叉树的建立及前中后序遍历及查找指定元素以及删除子树的代码实现

//构建树
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();
    }
    //树的中序遍历
    public void middleshow(){
        if (root!=null)
        root.middleshow();
    }
    //树的后序遍历
    public void aftershow(){
        if (root!=null)
        root.aftershow();
    }
    //树的前序查找
    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 class TreeNode {
    int value;//树结点中存储的值
    TreeNode leftnode;//左儿子
    TreeNode rightnode;//右儿子
    public TreeNode(int value){
        this.value=value;
    }

    public void setLeftnode(TreeNode node){
        this.leftnode=node;
    }

    public void setRightnode(TreeNode rightnode) {
        this.rightnode = rightnode;
    }

    //前序遍历 根左右
    public void frontShow(){
        System.out.println(value);
        if(leftnode!=null){
            leftnode.frontShow();
        }
        if (rightnode!=null){
            rightnode.frontShow();
        }
    }
    //中序遍历 左根右
    public void middleshow(){
        if(leftnode!=null){
            leftnode.middleshow();
        }
        System.out.println(value);
        if (rightnode!=null){
            rightnode.middleshow();
        }
    }
    //后序遍历 左右跟
    public void aftershow(){
        if (leftnode!=null){
            leftnode.aftershow();
        }
        if (rightnode!=null){
            rightnode.aftershow();
        }
        System.out.println(value);
    }

    //前序查找
    public TreeNode frontsearch(int i) {
        TreeNode target=null;
        if (i==this.value)
            return this;
        else {
            if (leftnode!=null){
                target=leftnode.frontsearch(i);//递归调用,如果递归过程中找到了该结点,用target存储该结点,若没找到target仍为null
            }
            if (target!=null){//代表找到了
                return target;
            }
            if (rightnode!=null){
                target=rightnode.frontsearch(i);
            }
            if (target!=null){
                return target;
            }
        }
        return target;//最后执行该语句,代表没找到
    }
    //删除一个子树
    public void delete(int i) {
        TreeNode parent=this;
        if (parent.leftnode!=null&&parent.leftnode.value==i){//如果该结点的左儿子不为空且value相等
            parent.leftnode=null;//删除
            return;
        }
        if (parent.rightnode!=null&&parent.rightnode.value==i){
            parent.rightnode=null;
            return;
        }

        //如果左右儿子都不等于i,再向下找,调用递归
        parent=leftnode;
        if (parent!=null){
            parent.delete(i);
        }
        parent=rightnode;
        if (parent!=null){
            parent.delete(i);
        }
    }
}





//主方法
public class TestBaritree {
    public static void main(String[] args) {
        Binarytree bintree=new Binarytree();
        //设置头结点
        TreeNode root = new TreeNode(1);
        //将头结点赋给树当根节点
        bintree.setRoot(root);
        TreeNode leftnode = new TreeNode(2);
        TreeNode rightnode = new TreeNode(3);
        //设置左节点右节点
        root.setLeftnode(leftnode);
        root.setRightnode(rightnode);
        //为第二层结点设置左右结点
        leftnode.setLeftnode(new TreeNode(4));
        leftnode.setRightnode(new TreeNode(5));
        //为第二层结点设置左右结点
        rightnode.setLeftnode(new TreeNode(6));
        rightnode.setRightnode(new TreeNode(7));
        //进行前中后序遍历
        //bintree.frontShow();
        //bintree.middleshow();
        //bintree.aftershow();
        TreeNode result=bintree.frontsearch(3);//查找3
        System.out.println(result);//出地址值
        System.out.println(result==rightnode);//经过地址值对比,是同一个值

        //删除一个子树。
        bintree.delete(1);
        bintree.frontShow();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值