二叉树的遍历

二叉树的遍历 递归非递归 思路和 java实现


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

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


第二次访问是 pop(&S,&p);Visit(p->data);因为中序遍历跟第三次经过没关系。所以第二次就弹出栈了。
Java版的程序如下:

[java]  view plain   copy
  1. package com.tree;  
  2. import java.util.Stack;  
  3. public class BinaryTree {  
  4.     protected Node root;    
  5.         
  6.     public BinaryTree(Node root) {    
  7.         this.root = root;    
  8.     }    
  9.     
  10.     public Node getRoot() {    
  11.         return root;    
  12.     }    
  13.     
  14.     /** 构造树 */    
  15.     public static Node init() {    
  16.         Node a = new Node('A');    
  17.         Node b = new Node('B'null, a);    
  18.         Node c = new Node('C');    
  19.         Node d = new Node('D', b, c);    
  20.         Node e = new Node('E');    
  21.         Node f = new Node('F', e, null);    
  22.         Node g = new Node('G'null, f);    
  23.         Node h = new Node('H', d, g);    
  24.         return h;// root    
  25.     }    
  26.     
  27.     /** 访问节点 */    
  28.     public static void visit(Node p) {    
  29.         System.out.print(p.getKey() + " ");    
  30.     }    
  31.     
  32.     /** 递归实现前序遍历 */    
  33.     protected static void preorder(Node p) {    
  34.         if (p != null) {    
  35.             visit(p);    
  36.             preorder(p.getLeft());    
  37.             preorder(p.getRight());    
  38.         }    
  39.     }    
  40.     
  41.     /** 递归实现中序遍历 */    
  42.     protected static void inorder(Node p) {    
  43.         if (p != null) {    
  44.             inorder(p.getLeft());    
  45.             visit(p);    
  46.             inorder(p.getRight());    
  47.         }    
  48.     }    
  49.     
  50.     /** 递归实现后序遍历 */    
  51.     protected static void postorder(Node p) {    
  52.         if (p != null) {    
  53.             postorder(p.getLeft());    
  54.             postorder(p.getRight());    
  55.             visit(p);    
  56.         }    
  57.     }    
  58.   /**********************************************************************************************/  
  59.     /** 非递归实现前序遍历 */    
  60.     protected static void iterativePreorder(Node p) {    
  61.         Stack<Node> stack = new Stack<Node>();    
  62.         if (p != null) {    
  63.             stack.push(p);    
  64.             while (!stack.empty()) {    
  65.                 p = stack.pop();    
  66.                 visit(p);    
  67.                 if (p.getRight() != null)    
  68.                     stack.push(p.getRight());    
  69.                 if (p.getLeft() != null)  //为什么p.getLeft() 在后,getRight()在前应为while 循环第一句就是pop visit所以要把left放上,先访问。之中方法是即压即访问法。  
  70.                     stack.push(p.getLeft());    
  71.             }    
  72.         }    
  73.     }    
  74.       
  75.     /** 非递归实现中序遍历 */  //思路与上面iterativePreorder 一致。  
  76.     protected static void iterativeInorder(Node p) {    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值