二叉树的非递归遍历

      个人觉得借助栈或队列的非递归只是另一种形式的递归。当然这种方式对于效率的提高还是有好处的。
      先序遍历很简单,根指针入栈,然后不断出栈,右孩子入栈,左孩子入栈:

Code:
  1. void pre_order(struct BiTNode *T, void (*visit)(TElemType e))   
  2. {   
  3.     struct stack *s;   
  4.     struct BiTNode *p;   
  5.        
  6.     if (T != NULL) {   
  7.         s = init_stack();   
  8.         push(s, T);   
  9.         while (! stack_empty(s)) {   
  10.             p = top(s);   
  11.             pop(s);   
  12.             visit(p->data);   
  13.             if (p->rchild != NULL) {   
  14.                 push(s, p->rchild);   
  15.             }   
  16.             if (p->lchild != NULL) {   
  17.                 push(s, p->lchild);   
  18.             }   
  19.         }   
  20.     }
  21.     destory_stack(s);
  22. }  

      中序遍历:首先根指针入栈、遍历左子树,出栈,遍历右子树:

Code:
  1. void in_order(struct BiTNode *T, void (*visit)(TElemType e))   
  2. {   
  3.     struct stack *s;   
  4.     struct BiTNode *p;   
  5.        
  6.     p = T;   
  7.     s = init_stack();   
  8.     while (p != NULL || ! stack_empty(s)) {   
  9.         if (p != NULL) { /* push the root pointer, and traverse the left child tree */  
  10.             push(s, p);   
  11.             p = p ->lchild;   
  12.         }    
  13.         else { /* pop and  visit the pointer, traverse the right child tree */  
  14.             p = top(s);   
  15.             pop(s);   
  16.             visit(p->data);   
  17.             p = p->rchild;   
  18.         }   
  19.     }
  20.     destory_stack(s);  
  21. }  

      后序遍历:根指针入栈、遍历左子树,根据右孩子的值判断是否出栈,遍历右子树:

Code:
  1. void post_order(struct BiTNode *T, void (*visit)(TElemType e))   
  2. {   
  3.     struct stack *s;   
  4.     struct BiTNode *p, *pre;/* pre means recently visit */  
  5.   
  6.     p = T;   
  7.     pre = NULL;   
  8.     s = init_stack();   
  9.     while (p != NULL || ! stack_empty(s)) {   
  10.         if (p != NULL ) {   
  11.             push(s, p);   
  12.             p = p->lchild;   
  13.         }   
  14.         else {   
  15.             p = top(s);   
  16.             if (p->rchild == NULL || p->rchild == pre) {   
  17.                 visit(p->data);   
  18.                 pop(s);   
  19.                 pre = p;   
  20.                 p = NULL;   
  21.             }   
  22.             else {   
  23.                 p = p->rchild;   
  24.             }   
  25.         }   
  26.     }
  27.     destory_stack(s);
  28. }  

      层次遍历:很简单,根指针入队,出队,左孩子入队,右孩子入队:

Code:
  1. void level_order(struct BiTNode *T, void (*visit)(TElemType e))   
  2. {   
  3.     struct queue *q;   
  4.     struct BiTNode *p;   
  5.        
  6.     p = T;   
  7.     q = init_queue();   
  8.     if (T != NULL) {   
  9.         en_queue(q, p);   
  10.         while(! queue_empty(q)) {   
  11.             p = gethead(q);   
  12.             de_queue(q);   
  13.             visit(p->data);   
  14.             if (p->lchild != NULL) {   
  15.                 en_queue(q, p->lchild);   
  16.             }   
  17.             if (p->rchild != NULL) {   
  18.                 en_queue(q, p->rchild);   
  19.             }   
  20.         }   
  21.     }
  22.     destory_queue(q);  
  23. }  

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值