二叉树简单遍历、查找、删除(java)

简介: 二叉树简单遍历、查找、删除(java)
public class HeroNode {
    private int no;
    private String name;
    //默认为null
    private HeroNode left;
    //默认为null
    private HeroNode right;
 
    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }
 
    /**
     * 递归删除节点
     * 如果删除节点是叶子节点,直接删除
     * 如果删除节点是非叶子节点,删除数
     *
     * @param no
     */
    public void delNode(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.delNode(no);
        }
        if (this.right!=null) {
            this.right.delNode(no);
        }
 
    }
 
    /**
     * 前序遍历方法
     */
    public void preOrder() {
        //先输出父节点
        System.out.println(this);
        //    递归向左子树前序遍历
        if (this.left != null) {
            this.left.preOrder();
        }
        //遍历右子树前序遍历
        if (this.right != null) {
            this.right.preOrder();
        }
    }
 
    /**
     * 中序遍历方法
     */
    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);
    }
 
    /**
     * 前序查找
     *
     * @param no
     * @return
     */
    public HeroNode preOrderSearch(int no) {
        //    比较当前节点
        if (this.no == no) {
            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 {
    public static void main(String[] args) {
        //创建二叉树
        BinaryTree bt = new BinaryTree();
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
 
        bt.setRoot(root);
        1 2 3  4 5
        //bt.preOrder();
        2 1 5 3 4
        //bt.infixOrder();
        2 5 4 3 1
        //bt.postOrder();
        //System.out.println(bt.preOrderSearch(2));
        //System.out.println(bt.infixOrderSearch(2));
        //System.out.println(bt.postOrderSearch(2));
        bt.preOrder();
        System.out.println();
        bt.delNode(5);
        bt.preOrder();
 
    }
}
前序遍历
HeroNode{no=1, name='宋江'}
HeroNode{no=2, name='吴用'}
HeroNode{no=3, name='卢俊义'}
HeroNode{no=5, name='关胜'}
HeroNode{no=4, name='林冲'}
 
前序遍历
HeroNode{no=1, name='宋江'}
HeroNode{no=2, name='吴用'}
HeroNode{no=3, name='卢俊义'}
HeroNode{no=4, name='林冲'}
相关文章
|
7月前
|
存储 监控 Java
《从头开始学java,一天一个知识点》之:数组入门:一维数组的定义与遍历
**你是否也经历过这些崩溃瞬间?** - 看了三天教程,连`i++`和`++i`的区别都说不清 - 面试时被追问"`a==b`和`equals()`的区别",大脑突然空白 - 写出的代码总是莫名报NPE,却不知道问题出在哪个运算符 这个系列就是为你打造的Java「速效救心丸」!我们承诺:每天1分钟,地铁通勤、午休间隙即可完成学习;直击痛点,只讲高频考点和实际开发中的「坑位」;拒绝臃肿,没有冗长概念堆砌,每篇都有可运行的代码标本。明日预告:《多维数组与常见操作》。 通过实例讲解数组的核心认知、趣味场景应用、企业级开发规范及优化技巧,帮助你快速掌握Java数组的精髓。
127 23
|
11月前
|
存储 Java 开发者
在 Java 中,如何遍历一个 Set 集合?
【10月更文挑战第30天】开发者可以根据具体的需求和代码风格选择合适的遍历方式。增强for循环简洁直观,适用于大多数简单的遍历场景;迭代器则更加灵活,可在遍历过程中进行更多复杂的操作;而Lambda表达式和`forEach`方法则提供了一种更简洁的函数式编程风格的遍历方式。
4297 113
|
11月前
|
算法 Java
JAVA 二叉树面试题
JAVA 二叉树面试题
79 0
|
12月前
|
Java 程序员 编译器
Java|如何正确地在遍历 List 时删除元素
从源码分析如何正确地在遍历 List 时删除元素。为什么有的写法会导致异常,而另一些不会。
279 3
|
12月前
|
前端开发 小程序 Java
java基础:map遍历使用;java使用 Patten 和Matches 进行正则匹配;后端传到前端展示图片三种情况,并保存到手机
这篇文章介绍了Java中Map的遍历方法、使用Pattern和matches进行正则表达式匹配,以及后端向前端传输图片并保存到手机的三种情况。
136 1
|
12月前
|
Java
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(二)
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(二)
102 1
|
12月前
|
算法 Java C语言
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(一)
【用Java学习数据结构系列】震惊,二叉树原来是要这么学习的(一)
82 1
|
12月前
|
存储 算法 Java
Java一分钟之-数组的创建与遍历
数组作为Java中存储和操作一组相同类型数据的基本结构,其创建和遍历是编程基础中的基础。通过不同的创建方式,可以根据实际需求灵活地初始化数组。而选择合适的遍历方法,则可以提高代码的可读性和效率。掌握这些基本技能,对于深入学习Java乃至其他编程语言的数据结构和算法都是至关重要的。
92 6
|
域名解析 分布式计算 网络协议
java遍历hdfs路径信息,报错EOFException
java遍历hdfs路径信息,报错EOFException
143 3
|
缓存 Java 测试技术
探讨Java中遍历Map集合的最快方式
探讨Java中遍历Map集合的最快方式
295 1