JAVA递归、非递归遍历二叉树

转载:http://blog.csdn.net/haijiaoxiaowu/article/details/4851059

二叉树遍历首先弄清遍历流程。

中序遍历:第一次经过从它找左,第二次经过找右,第三次经过回来。第二次经过访问它。

[java]  view plain  copy
  1. import java.util.Stack;    
  2. import java.util.HashMap;    
  3.     
  4. public class BinTree {    
  5.     private char date;    
  6.     private BinTree lchild;    
  7.     private BinTree rchild;    
  8.     
  9.     public BinTree(char c) {    
  10.         date = c;    
  11.     }    
  12.     
  13.     // 先序遍历递归     
  14.     public static void preOrder(BinTree t) {    
  15.         if (t == null) {    
  16.             return;    
  17.         }    
  18.         System.out.print(t.date);    
  19.         preOrder(t.lchild);    
  20.         preOrder(t.rchild);    
  21.     }    
  22.     
  23.     // 中序遍历递归     
  24.     public static void InOrder(BinTree t) {    
  25.         if (t == null) {    
  26.             return;    
  27.         }    
  28.         InOrder(t.lchild);    
  29.         System.out.print(t.date);    
  30.         InOrder(t.rchild);    
  31.     }    
  32.     
  33.     // 后序遍历递归     
  34.     public static void PostOrder(BinTree t) {    
  35.         if (t == null) {    
  36.             return;    
  37.         }    
  38.         PostOrder(t.lchild);    
  39.         PostOrder(t.rchild);    
  40.         System.out.print(t.date);    
  41.     }    
  42.     //以下三种遍历,中心思想都是把子树压栈,直到没有左子树,然后右子树中重复以前的操作;不同的是
  43.     //节点的访问顺序;另外,与前序和中序不同,后序遍历用另一个栈作为节点是否出栈的标志,因此节点的访问
  44.     //也有较大不同,中间节点首先peek(),等右子树入栈后再pop()。
  45.     // 先序遍历非递归     
  46.     public static void preOrder2(BinTree t) {    
  47.         Stack<BinTree> s = new Stack<BinTree>();    
  48.         while (t != null || !s.empty()) {    
  49.             while (t != null) {    
  50.                 System.out.print(t.date);    
  51.                 s.push(t);    
  52.                 t = t.lchild;    
  53.             }    
  54.             if (!s.empty()) {    
  55.                 t = s.pop();    
  56.                 t = t.rchild;    
  57.             }    
  58.         }    
  59.     }    
  60.     
  61.     // 中序遍历非递归     
  62.     public static void InOrder2(BinTree t) {    
  63.         Stack<BinTree> s = new Stack<BinTree>();    
  64.         while (t != null || !s.empty()) {    
  65.             while (t != null) {    
  66.                 s.push(t);    
  67.                 t = t.lchild;    
  68.             }    
  69.             if (!s.empty()) {    
  70.                 t = s.pop();    
  71.                 System.out.print(t.date);    
  72.                 t = t.rchild;    
  73.             }    
  74.         }    
  75.     }    
  76.     
  77.     // 后序遍历非递归     
  78.     public static void PostOrder2(BinTree t) {    
  79.         Stack<BinTree> s = new Stack<BinTree>();    
  80.         Stack<Integer> s2 = new Stack<Integer>();    
  81.         Integer i = new Integer(1);    
  82.         while (t != null || !s.empty()) {    
  83.             while (t != null) {    
  84.                 s.push(t);    
  85.                 s2.push(new Integer(0));    
  86.                 t = t.lchild;    
  87.             }    
  88.             while (!s.empty() && s2.peek().equals(i)) {    
  89.                 s2.pop();    
  90.                 System.out.print(s.pop().date);    
  91.             }    
  92.     
  93.             if (!s.empty()) {    
  94.                 s2.pop();    
  95.                 s2.push(new Integer(1));    
  96.                 t = s.peek();    //后序,因此中间节点排最后,所以不用pop()
  97.                 t = t.rchild;    
  98.             }    
  99.         }    
  100.     }    
  101.     
  102.     public static void main(String[] args) {    
  103.         BinTree b1 = new BinTree('a');    
  104.         BinTree b2 = new BinTree('b');    
  105.         BinTree b3 = new BinTree('c');    
  106.         BinTree b4 = new BinTree('d');    
  107.         BinTree b5 = new BinTree('e');    
  108.     
  109.         /**  
  110.          *      a   
  111.          *     / /  
  112.          *    b   c  
  113.          *   / /  
  114.          *  d   e  
  115.          */    
  116.         b1.lchild = b2;    
  117.         b1.rchild = b3;    
  118.         b2.lchild = b4;    
  119.         b2.rchild = b5;    
  120.     
  121.         BinTree.preOrder(b1);    
  122.         System.out.println();    
  123.         BinTree.preOrder2(b1);    
  124.         System.out.println();    
  125.         BinTree.InOrder(b1);    
  126.         System.out.println();    
  127.         BinTree.InOrder2(b1);    
  128.         System.out.println();    
  129.         BinTree.PostOrder(b1);    
  130.         System.out.println();    
  131.         BinTree.PostOrder2(b1);    
  132.     }    
  133. }    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值