JAVA 二叉树的递归和非递归遍历(转载)非递归后续遍历竟然用了4种方法,我当时一个方法没想出来,⊙﹏⊙b汗,有空多研究下。...

本文主要是介绍JAVA 二叉树的递归和非递归遍历(转载)非递归后续遍历竟然用了4种方法,我当时一个方法没想出来,⊙﹏⊙b汗,有空多研究下。...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载自:http://robinsoncrusoe.iteye.com/blog/808526


package edu.cumt.jnotnull; import java.util.Stack; public class BinaryTree { protected Node root; public BinaryTree(Node root) { this.root = root; } public Node getRoot() { return root; } /** 构造树 */ public static Node init() { Node a = new Node('A'); Node b = new Node('B', null, a); Node c = new Node('C'); Node d = new Node('D', b, c); Node e = new Node('E'); Node f = new Node('F', e, null); Node g = new Node('G', null, f); Node h = new Node('H', d, g); return h;// root } /** 访问节点 */ public static void visit(Node p) { System.out.print(p.getKey() + " "); } /** 递归实现前序遍历 */ protected static void preorder(Node p) { if (p != null) { visit(p); preorder(p.getLeft()); preorder(p.getRight()); } } /** 递归实现中序遍历 */ protected static void inorder(Node p) { if (p != null) { inorder(p.getLeft()); visit(p); inorder(p.getRight()); } } /** 递归实现后序遍历 */ protected static void postorder(Node p) { if (p != null) { postorder(p.getLeft()); postorder(p.getRight()); visit(p); } } /** 非递归实现前序遍历 */ protected static void iterativePreorder(Node p) { Stack<Node> stack = new Stack<Node>(); if (p != null) { stack.push(p); while (!stack.empty()) { p = stack.pop(); visit(p); if (p.getRight() != null) stack.push(p.getRight()); if (p.getLeft() != null) stack.push(p.getLeft()); } } } /** 非递归实现前序遍历2 */ protected static void iterativePreorder2(Node p) { Stack<Node> stack = new Stack<Node>(); Node node = p; while (node != null || stack.size() > 0) { while (node != null) {//压入所有的左节点,压入前访问它 visit(node); stack.push(node); node = node.getLeft(); } if (stack.size() > 0) {// node = stack.pop(); node = node.getRight(); } } } /** 非递归实现后序遍历 */ protected static void iterativePostorder(Node p) { Node q = p; Stack<Node> stack = new Stack<Node>(); while (p != null) { // 左子树入栈 for (; p.getLeft() != null; p = p.getLeft()) stack.push(p); // 当前节点无右子或右子已经输出 while (p != null && (p.getRight() == null || p.getRight() == q)) { visit(p); q = p;// 记录上一个已输出节点 if (stack.empty()) return; p = stack.pop(); } // 处理右子 stack.push(p); p = p.getRight(); } } /** 非递归实现后序遍历 双栈法 */ protected static void iterativePostorder2(Node p) { Stack<Node> lstack = new Stack<Node>(); Stack<Node> rstack = new Stack<Node>(); Node node = p, right; do { while (node != null) { right = node.getRight(); lstack.push(node); rstack.push(right); node = node.getLeft(); } node = lstack.pop(); right = rstack.pop(); if (right == null) { visit(node); } else { lstack.push(node); rstack.push(null); } node = right; } while (lstack.size() > 0 || rstack.size() > 0); } /** 非递归实现后序遍历 单栈法*/ protected static void iterativePostorder3(Node p) { Stack<Node> stack = new Stack<Node>(); Node node = p, prev = p; while (node != null || stack.size() > 0) { while (node != null) { stack.push(node); node = node.getLeft(); } if (stack.size() > 0) { Node temp = stack.peek().getRight(); if (temp == null || temp == prev) { node = stack.pop(); visit(node); prev = node; node = null; } else { node = temp; } } } } /** 非递归实现后序遍历4 双栈法*/ protected static void iterativePostorder4(Node p) { Stack<Node> stack = new Stack<Node>(); Stack<Node> temp = new Stack<Node>(); Node node = p; while (node != null || stack.size() > 0) { while (node != null) { temp.push(node); stack.push(node); node = node.getRight(); } if (stack.size() > 0) { node = stack.pop(); node = node.getLeft(); } } while (temp.size() > 0) { node = temp.pop(); visit(node); } } /** 非递归实现中序遍历 */ protected static void iterativeInorder(Node p) { Stack<Node> stack = new Stack<Node>(); while (p != null) { while (p != null) { if (p.getRight() != null) stack.push(p.getRight());// 当前节点右子入栈 stack.push(p);// 当前节点入栈 p = p.getLeft(); } p = stack.pop(); while (!stack.empty() && p.getRight() == null) { visit(p); p = stack.pop(); } visit(p); if (!stack.empty()) p = stack.pop(); else p = null; } } /** 非递归实现中序遍历2 */ protected static void iterativeInorder2(Node p) { Stack<Node> stack = new Stack<Node>(); Node node = p; while (node != null || stack.size() > 0) { while (node != null) { stack.push(node); node = node.getLeft(); } if (stack.size() > 0) { node = stack.pop(); visit(node); node = node.getRight(); } } } /** * @param args */ public static void main(String[] args) { BinaryTree tree = new BinaryTree(init()); System.out.print(" Pre-Order:"); preorder(tree.getRoot()); System.out.println(); System.out.print(" In-Order:"); inorder(tree.getRoot()); System.out.println(); System.out.print("Post-Order:"); postorder(tree.getRoot()); System.out.println(); System.out.print(" Pre-Order:"); iterativePreorder(tree.getRoot()); System.out.println(); System.out.print("Pre-Order2:"); iterativePreorder2(tree.getRoot()); System.out.println(); System.out.print(" In-Order:"); iterativeInorder(tree.getRoot()); System.out.println(); System.out.print(" In-Order2:"); iterativeInorder2(tree.getRoot()); System.out.println(); System.out.print(" Post-Order:"); iterativePostorder(tree.getRoot()); System.out.println(); System.out.print("Post-Order2:"); iterativePostorder2(tree.getRoot()); System.out.println(); System.out.print("Post-Order3:"); iterativePostorder3(tree.getRoot()); System.out.println(); System.out.print("Post-Order4:"); iterativePostorder4(tree.getRoot()); System.out.println(); } }

这篇关于JAVA 二叉树的递归和非递归遍历(转载)非递归后续遍历竟然用了4种方法,我当时一个方法没想出来,⊙﹏⊙b汗,有空多研究下。...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/472656

相关文章

Python中的魔术方法__new__详解

《Python中的魔术方法__new__详解》:本文主要介绍Python中的魔术方法__new__的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、核心意义与机制1.1 构造过程原理1.2 与 __init__ 对比二、核心功能解析2.1 核心能力2.2

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

关于pandas的read_csv方法使用解读

《关于pandas的read_csv方法使用解读》:本文主要介绍关于pandas的read_csv方法使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录pandas的read_csv方法解读read_csv中的参数基本参数通用解析参数空值处理相关参数时间处理相关

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim