Java递归、非递归实现二叉树遍历
来源:互联网 发布:windows10安装数据库 编辑:程序博客网 时间:2024/06/15 07:12
- import java.util.Stack;
- import java.util.HashMap;
- public class BinTree {
- private char date;
- private BinTree lchild;
- private BinTree rchild;
- public BinTree(char c) {
- date = c;
- }
- // 先序遍历递归
- public static void preOrder(BinTree t) {
- if (t == null) {
- return;
- }
- System.out.print(t.date);
- preOrder(t.lchild);
- preOrder(t.rchild);
- }
- // 中序遍历递归
- public static void InOrder(BinTree t) {
- if (t == null) {
- return;
- }
- InOrder(t.lchild);
- System.out.print(t.date);
- InOrder(t.rchild);
- }
- // 后序遍历递归
- public static void PostOrder(BinTree t) {
- if (t == null) {
- return;
- }
- PostOrder(t.lchild);
- PostOrder(t.rchild);
- System.out.print(t.date);
- }
- // 先序遍历非递归
- public static void preOrder2(BinTree t) {
- Stack<BinTree> s = new Stack<BinTree>();
- while (t != null || !s.empty()) {
- while (t != null) {
- System.out.print(t.date);
- s.push(t);
- t = t.lchild;
- }
- if (!s.empty()) {
- t = s.pop();
- t = t.rchild;
- }
- }
- }
- // 中序遍历非递归
- public static void InOrder2(BinTree t) {
- Stack<BinTree> s = new Stack<BinTree>();
- while (t != null || !s.empty()) {
- while (t != null) {
- s.push(t);
- t = t.lchild;
- }
- if (!s.empty()) {
- t = s.pop();
- System.out.print(t.date);
- t = t.rchild;
- }
- }
- }
- // 后序遍历非递归
- public static void PostOrder2(BinTree t) {
- Stack<BinTree> s = new Stack<BinTree>();
- Stack<Integer> s2 = new Stack<Integer>();
- Integer i = new Integer(1);
- while (t != null || !s.empty()) {
- while (t != null) {
- s.push(t);
- s2.push(new Integer(0));
- t = t.lchild;
- }
- while (!s.empty() && s2.peek().equals(i)) {
- s2.pop();
- System.out.print(s.pop().date);
- }
- if (!s.empty()) {
- s2.pop();
- s2.push(new Integer(1));
- t = s.peek();
- t = t.rchild;
- }
- }
- }
- public static void main(String[] args) {
- BinTree b1 = new BinTree('a');
- BinTree b2 = new BinTree('b');
- BinTree b3 = new BinTree('c');
- BinTree b4 = new BinTree('d');
- BinTree b5 = new BinTree('e');
- /**
- * a
- * / /
- * b c
- * / /
- * d e
- */
- b1.lchild = b2;
- b1.rchild = b3;
- b2.lchild = b4;
- b2.rchild = b5;
- BinTree.preOrder(b1);
- System.out.println();
- BinTree.preOrder2(b1);
- System.out.println();
- BinTree.InOrder(b1);
- System.out.println();
- BinTree.InOrder2(b1);
- System.out.println();
- BinTree.PostOrder(b1);
- System.out.println();
- BinTree.PostOrder2(b1);
- }
- }
0 0
- 非递归遍历二叉树--java实现
- 二叉树非递归遍历Java实现
- Java实现二叉树的递归与非递归遍历
- Java递归、非递归实现二叉树遍历
- Java实现二叉树的创建、递归/非递归遍历
- Java实现二叉树遍历(递归,非递归)
- Java实现二叉树的递归、非递归遍历
- JAVA递归、非递归遍历二叉树
- java递归,非递归遍历二叉树
- JAVA递归、非递归遍历二叉树
- java 实现二叉树【递归/非递归】
- java 二叉树的递归遍历和非递归遍历
- java实现二叉树的非递归遍历
- java中实现二叉树的非递归遍历
- Java实现的二叉树的非递归遍历
- 二叉树的非递归遍历——java实现
- 二叉树遍历的非递归实现 java版本
- 【数据结构】Java实现二叉树遍历(非递归)
- HDU kebab(网络流,最大流)
- 欧几里得定理与扩展欧几里得
- PHP的APNS消息推送
- 技术不牛如何才拿到国内IT巨头的Offer
- java巩固基础之函数(九九乘法表)
- Java递归、非递归实现二叉树遍历
- 开源网络库RakNet for unity3d iOS unity3d android的使用及实现。
- web c标签
- GRE分数查询 fail
- Wifite批量破解WEP/WPA/WPS
- GNU中的make和makefile
- linux server之间建立信任
- item18: 让接口容易被正确使用,不易被误用
- 虚拟机下的ubuntu dhcp自动连网(NAT)