Java实现的二叉树常用操作【前序建树,前中后递归非递归遍历及层序遍历】

 更新时间:2018年01月20日 10:48:36   作者:idealemail  
这篇文章主要介绍了Java实现的二叉树常用操作,包括二叉树的前序建树,前中后递归非递归遍历及层序遍历等相关操作技巧,需要的朋友可以参考下

本文实例讲述了Java实现的二叉树常用操作。分享给大家供大家参考,具体如下:

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;
//二叉树的建树,前中后 递归非递归遍历 层序遍历
//Node节点
class Node {
    int element;
    Node left;
    Node right;
    public Node() {
    }
    public Node(int element) {
        this.element = element;
    }
}
// BinaryTree
public class Tree {
    // creat tree from array
    public static Node creatTree(int[] data, int i) {
        if (i >= data.length || data[i] == -1)
            return null;
        Node temp = new Node(data[i]);
        temp.left = creatTree(data, i * 2 + 1);
        temp.right = creatTree(data, i * 2 + 2);
        return temp;
    }
    // pre前序遍历递归
    public static void pre(Node temp) {
        if (temp == null)
            return;
        System.out.print(temp.element + " ");
        pre(temp.left);
        pre(temp.right);
    }
    // mid中序遍历递归
    public static void mid(Node temp) {
        if (temp == null)
            return;
        mid(temp.left);
        System.out.print(temp.element + " ");
        mid(temp.right);
    }
    // last后序遍历递归
    public static void last(Node temp) {
        if (temp == null)
            return;
        last(temp.left);
        last(temp.right);
        System.out.print(temp.element + " ");
    }
    // pre1前序遍历非递归
    public static void pre1(Node temp) {
        Stack<Node> stack = new Stack<>();
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.push(temp);
                System.out.print(temp.element + " ");
                temp = temp.left;
            }
            if (!stack.isEmpty()) {
                temp = stack.pop().right;
            }
        }
    }
    // mid1中序遍历非递归
    public static void mid1(Node temp) {
        Stack<Node> stack = new Stack<>();
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.push(temp);
                temp = temp.left;
            }
            if (!stack.isEmpty()) {
                temp = stack.pop();
                System.out.print(temp.element + " ");
                temp = temp.right;
            }
        }
    }
    // last1后序遍历非递归
    public static void last1(Node temp) {
        Stack<Node> stack = new Stack<>();
        Stack<Node> stack2 = new Stack<>();
        while (temp != null || !stack.isEmpty()) {
            while (temp != null) {
                stack.push(temp);
                stack2.push(temp);
                temp = temp.right;
            }
            if (!stack.isEmpty()) {
                temp = stack.pop().left;
            }
        }
        while (!stack2.isEmpty())
            System.out.print(stack2.pop().element + " ");
    }
    // ceng层序遍历
    public static void ceng(Node temp) {
        if (temp == null)
            return;
        Queue<Node> queue = new ArrayDeque<>();
        queue.offer(temp);
        while (!queue.isEmpty()) {
            temp = queue.poll();
            System.out.print(temp.element + " ");
            if (temp.left != null)
                queue.offer(temp.left);
            if (temp.right != null)
                queue.offer(temp.right);
        }
    }
    // Demo
    public static void main(String[] args) {
        int[] array = { 1, 2, 3, 4, 5, 6, 7, -1, -1, 10, -1, -1, 13 };
        Node tree = creatTree(array, 0);
        System.out.println("脚本之家测试结果:");
        pre(tree);
        System.out.println();
        pre1(tree);
        System.out.println();
        mid(tree);
        System.out.println();
        mid1(tree);
        System.out.println();
        last(tree);
        System.out.println();
        last1(tree);
        System.out.println();
        ceng(tree);
    }
}

运行结果:

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

相关文章

  • 解决IDEA springboot

    解决IDEA springboot"spring-boot-maven-plugin"报红问题

    这篇文章主要介绍了解决IDEA springboot"spring-boot-maven-plugin"报红问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Java中的static关键字用法总结

    Java中的static关键字用法总结

    这篇文章主要介绍了Java中的static关键字用法总结,static是Java50个关键字之一,static关键字可以用来修饰代码块表示静态代码块,修饰成员变量表示全局静态成员变量,修饰方法表示静态方法,需要的朋友可以参考下
    2023-11-11
  • Mybatis使用foreach批量更新数据报无效字符错误问题

    Mybatis使用foreach批量更新数据报无效字符错误问题

    这篇文章主要介绍了Mybatis使用foreach批量更新数据报无效字符错误问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • 解决springcloud 配置gateway 出现错误的问题

    解决springcloud 配置gateway 出现错误的问题

    今天给大家分享springcloud 配置gateway 出现错误的问题,其实解决方法很简单,只需要降低springcloud版本,改成Hoxton.SR5就好了,再次改成Hoxton.SR12,也不报错了,下面给大家展示下,感兴趣的朋友一起看看吧
    2021-11-11
  • 一文带你掌握Java Future模式的灵活应用

    一文带你掌握Java Future模式的灵活应用

    Future模式,简单来说,就是一种能够管理异步操作的方式,它可以让咱们的程序在执行一个耗时任务的同时,还能继续做其他事情,下面我们就来看看Future模式的具体应用吧
    2024-01-01
  • Java命令设计模式优雅解耦命令和执行提高代码可维护性

    Java命令设计模式优雅解耦命令和执行提高代码可维护性

    本文介绍了Java命令设计模式,它将命令请求封装成对象,以达到解耦命令请求和执行者的目的,从而提高代码可维护性。本文详细阐述了该模式的设计原则、实现方法和优缺点,并提供了实际应用场景和代码示例,帮助读者深入理解和应用该模式
    2023-04-04
  • Java中的@PostConstruct注解用法详解

    Java中的@PostConstruct注解用法详解

    @PostConstruct注解是Java中一个强大的特性,它允许开发人员在Bean被构造并且依赖被注入后执行初始化逻辑,本文将从源码和用法的角度深入解析@PostConstruct注解,探讨其实现细节和实际应用
    2023-07-07
  • SpringBoot集成redisson全过程

    SpringBoot集成redisson全过程

    本文主要介绍了如何集成Redisson,包括环境配置、引入Redisson的依赖、添加Redisson的配置类以及Redisson的常见使用方法,在类中注入Redisson后,可以获取锁对象和使用,这些都是作者的个人经验,供读者参考
    2024-10-10
  • SpringBoot整合Security权限控制登录首页

    SpringBoot整合Security权限控制登录首页

    这篇文章主要为大家介绍了SpringBoot整合Security权限控制登录首页示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Java实现的汉语拼音工具类完整实例

    Java实现的汉语拼音工具类完整实例

    这篇文章主要介绍了Java实现的汉语拼音工具类,结合完整实例形式分析了java基于pinyin4j包实现编码转换的相关操作技巧,需要的朋友可以参考下
    2017-11-11

最新评论