二叉树的递归,非递归前序/中序/后序操作
来源:互联网 发布:淘宝公司招聘 编辑:程序博客网 时间:2024/05/19 05:30
package com.nowcoder.校招真题2016; 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); } // 先序遍历非递归
A
B C
D E F G
H I J K L M N O
遍历顺序 ABDH ->I->E ->J->K->C->F->L->M->G->N->O
public static void preOrder2(BinTree t) {
Stack<BinTree> s = new Stack<BinTree>(); while (t != null || !s.empty()) { //第一次运行时 t !=null s.empty()成立 while (t != null) { System.out.print(t.date); s.push(t); // 将T对象压入栈内 t = t.lchild; 将这个的左子树赋值给 T, 不停的循环并将左子树的节点压入到栈内,最后遍历到最后一个左子树 } 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实现
- 数据结构:二叉树的前序,中序,后序遍历(递归和非递归)
- 二叉树的递归和非递归遍历(前序、中序、后序)
- 二叉树的前序、中序、后序遍历(递归&非递归)
- 二叉树的前序、中序、后序遍历(递归与非递归)
- 【二叉树】 前序、中序和后序的递归遍历与非递归遍历
- 二叉树的前序,中序,后序遍历(递归非递归实现)
- 实现二叉树的前序/中序/后序递归、非递归遍历
- 二叉树的递归与非递归遍历(前序、中序、后序)
- 二叉树的递归与非递归遍历(前序、中序、后序)
- vue v-for v-if
- 正向代理和反向代理的区别
- Nightmare Ⅱ HDU
- 关于linux更换IP、重启产生的问题
- idea-快捷键
- 二叉树的递归,非递归前序/中序/后序操作
- Servlet线程安全问题
- Feign的简单应用总结
- 全氢聚硅氮烷液体涂料材料
- 华为OJ----汽水瓶数
- java Runtime.getRuntime().exec 调用系统脚本/命令注意事项
- 解决win10无法安装framework3.5问题
- Java取整数
- HDU 3487 Play with Chain [平衡树splay]