二叉树简单遍历、查找、删除(java)_java 遍历树,删除没有叶子节点的分支(1)

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

        return this;
    }
    //定义返回内容
    HeroNode res = null;
    //搜索左子节点
    if (this.left != null) {
        res = this.left.preOrderSearch(no);
    }
    //搜索右子节点
    if (res == null && this.right != null) {
        res = this.right.preOrderSearch(no);
    }
    return res;
}

/**
 * 中序查找
 *
 * @param no
 * @return
 */
public HeroNode infixOrderSearch(int no) {
    //定义返回内容
    HeroNode res = null;
    //搜索左子节点
    if (this.left != null) {
        res = this.left.preOrderSearch(no);
    }
    //    比较当前节点
    if (res == null && this.no == no) {
        return this;
    }
    //搜索右子节点
    if (res == null && this.right != null) {
        res = this.right.preOrderSearch(no);
    }
    return res;
}

/**
 * 后序查找
 *
 * @param no
 * @return
 */
public HeroNode postOrderSearch(int no) {
    //定义返回内容
    HeroNode res = null;
    //搜索左子节点
    if (this.left != null) {
        res = this.left.preOrderSearch(no);
    }
    //搜索右子节点
    if (res == null && this.right != null) {
        res = this.right.preOrderSearch(no);
    }
    //    比较当前节点
    if (res == null && this.no == no) {
        return this;
    }
    return res;
}

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 HeroNode getLeft() {
    return left;
}

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

public HeroNode getRight() {
    return right;
}

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

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

}



public class BinaryTree {
private HeroNode root;

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

/**
 * 递归删除节点
 * 如果删除节点是叶子节点,直接删除
 * 如果删除节点是非叶子节点,删除数
 *
 * @param no
 */
public void delNode(int no) {
    if (root == null) {
        return;
    }
    if (root.getNo() == no) {
        root = null;
        return;
    }
    root.delNode(no);
}

/**
 * 前序遍历方法
 */
public void preOrder() {
    System.out.println("前序遍历");
    if (this.root != null) {
        this.root.preOrder();
    } else {
        System.out.println("二叉树为空");
    }
}

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

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

/**
 * 前序查找
 *
 * @param no
 * @return
 */
public HeroNode preOrderSearch(int no) {
    HeroNode res = null;
    System.out.println("前序查找");
    if (this.root != null) {
        res = this.root.preOrderSearch(no);
    } else {
        System.out.println("二叉树为空");
    }
    return res;
}

/**
 * 中序查找
 *
 * @param no
 * @return
 */
public HeroNode infixOrderSearch(int no) {
    HeroNode res = null;
    System.out.println("中序查找");
    if (this.root != null) {
        res = this.root.infixOrderSearch(no);
    } else {
        System.out.println("二叉树为空");
    }
    return res;
}

/**
 * 后序查找
 *
 * @param no
 * @return
 */
public HeroNode postOrderSearch(int no) {
    HeroNode res = null;
    System.out.println("后序查找");
    if (this.root != null) {
        res = this.root.postOrderSearch(no);
    } else {
        System.out.println("二叉树为空");
    }
    return res;
}

}



public class BinaryTreeDemo {

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

15378846835)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值