二叉树的遍历、查找、节点删除

//定义树
class BinaryTree{
  private Hero root;

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

    //前序遍历
    public void perOrder(){
        if(this.root!=null){
            this.root.perOrder();
        }
        else {
            System.out.println("当前树为空");
        }
    }


    //中序遍历
    public void infixOrder(){
        if(this.root!=null){
            this.root.infixOrder();
        }
        else {
            System.out.println("当前树为空");
        }
    }

    //后序遍历
    public void postOrder(){
        if(this.root!=null){
            this.root.postOrder();
        }
        else {
            System.out.println("当前树为空");
        }
    }

    //前序查找
    public Hero perOrderSearch(int no){
        if(root==null){
            return null;
        }else{
            return root.perOrderSearch(no);
        }
    }

    //中序查找
    public Hero infixOrderSearch(int no){
        if(root==null){
            return null;
        }else{
            return root.infixOrderSearch(no);
        }
    }

    //后序查找
    public Hero postOrderSearch(int no){
        if(root==null){
            return null;
        }else{
            return root.postOrderSearch(no);
        }
    }


    //删除节点
    public void delNo(int no){
         if(root!=null){
             if(root.getNo() == no){
                 root = null;
             }else {
                 root.del(no);
             }
         }else {
             System.out.println("空树无法删除");
         }
    }
}

//定义节点
class Hero{
    private int no;
    private String name;
    private Hero left;
    private Hero right;

    public Hero(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Hero getLeft() {
        return left;
    }

    public void setLeft(Hero left) {
        this.left = left;
    }

    public Hero getRight() {
        return right;
    }

    public void setRight(Hero right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "Hero{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //前序遍历
    public void perOrder(){
        System.out.println(this);//先输出-节点
        //递归向左子树遍历
        if(this.left!=null){
            this.left.perOrder();
        }
        //递归向右子树遍历
        if(this.right!=null){
            this.right.perOrder();
        }
    }

    //中序遍历
    public void infixOrder(){
        //递归向左子树遍历
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);//输出当前节点
        //递归向右子树遍历
        if(this.right!=null){
            this.right.infixOrder();
        }
    }

    //后续遍历
    public void postOrder(){
        //递归向左子树遍历
        if(this.left!=null){
            this.left.postOrder();
        }
        //递归向右子树遍历
        if(this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);//输出当前节点
    }

    //前序遍历查找
    public Hero perOrderSearch(int no){
        if(this.no == no){
            return this;
        }
        Hero hero = null;
        if(this.left != null){
             hero = this.left.perOrderSearch(no);
        }
        if(hero!=null){
            //说明左子树上找到了
            return hero;
        }

        if(this.right!=null){
            hero = this.right.perOrderSearch(no);
        }
        return hero;
    }

    //中序遍历查找
    public Hero infixOrderSearch(int no){
        Hero hero = null;
        if(this.left != null){
            hero = this.left.infixOrderSearch(no);
        }
        if(hero!=null){
            return hero;
        }
        //如果找到就返回,如果没有找到,就和当前节点比较
        if(this.no == no){
            return this;
        }
        //如果左节点和当前节点都不是 则向右递归
        if(this.right!=null){
            hero = this.right.infixOrderSearch(no);
        }
        return hero;
    }

    //后序查找
    public Hero postOrderSearch(int no){
        Hero hero = null;
        if(this.left != null){
              hero = this.left.postOrderSearch(no);
        }
        if(hero != null){
            return hero;
        }
        if(this.right!=null){
            hero = this.right.postOrderSearch(no);
        }
        if(hero!=null){
            return hero;
        }
        if(this.no == no){
            return this;
        }
        return hero;
    }

    //删除节点
    public void del(int no){
      if(this.left!=null && this.left.no == no){
          this.left = null;
          return;
      }
      if(this.right!=null && this.right.no == no){
          this.right = null;
          return;
      }
      if(this.left!=null){
          this.left.del(no);
      }
      if(this.right!=null){
          this.right.del(no);
      }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值