二叉树递归遍历和非递归遍历
2014-07-10 13:57
316 查看
// vs_demo.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <Windows.h> #include <Shlobj.h> typedef struct st_BiTreeNode { char data; st_BiTreeNode* lchild; st_BiTreeNode* rchild; bool rchild_visited; st_BiTreeNode(){ data = '#'; lchild = rchild = NULL; rchild_visited = false; } }BiTreeNode, *BiTree; const int kMaxBiTreeNodes = 100; void VisitNode(BiTree t) { if (t) { printf("%c, ", t->data); } } void PreVisit(BiTree t) { if (t) { VisitNode(t); PreVisit(t->lchild); PreVisit(t->rchild); } } void PreVisitNoneRecursive(BiTree t) { if (t) { BiTree stack[kMaxBiTreeNodes]; int top = 0; BiTree p = t; while (p || top > 0) { while (p){ stack[top++] = p; VisitNode(p); p = p->lchild; } if (top > 0){ p = stack[--top]; p = p->rchild; } } } } void InorderVisit(BiTree t) { if (t) { InorderVisit(t->lchild); VisitNode(t); InorderVisit(t->rchild); } } void InorderVisitNoneRecursive(BiTree t) { if (t) { BiTree stack[kMaxBiTreeNodes]; int top = 0; BiTree p = t; while (p || top > 0) { while (p) { stack[top++] = p; p = p->lchild; } if (top > 0) { p = stack[--top]; VisitNode(p); p = p->rchild; } } } } void PostVisit(BiTree t) { if (t) { BiTree l = t->lchild; BiTree r = t->rchild; PostVisit(l); PostVisit(r); VisitNode(t); } } void PostVisitNoneRecursive(BiTree t) { if(t){ BiTree stack[kMaxBiTreeNodes]; int top = 0; BiTree p = t; if (p ) { while (p) { stack[top++] = p; p = p->lchild; } while (top > 0) { p = stack[--top]; if (!p->rchild || p->rchild_visited){ VisitNode(p); } else{ ++top; p->rchild_visited = true; p = p->rchild; while (p){ stack[top++] = p; p = p->lchild; } } } } } }
<pre name="code" class="cpp">//LRD NoneRecursive void PostOrderTraverse_NoneRecurisive(BiTree t) { if (t) { BiTree stack[MAX_BITREE_NODE]; int top = 0; BiTree p = t; BiTree q = NULL;//作为上一次访问的节点, while (p || top > 0) { while (p) { stack[top++] = p; p = p->lchild; } if (top>0) { p = stack[top-1]; if (p->rchild == NULL || p->rchild == q) { TraverseNode(p); q = p;//备份用以判断是否为右子树根节点 p = NULL; top--; } else{ p = p->rchild; } } } } }
int _tmain(int argc, _TCHAR* argv[], _TCHAR* env[]) { BiTree a = new BiTreeNode;a->data = 'a'; BiTree b = new BiTreeNode; b->data = 'b'; BiTree c= new BiTreeNode; c->data = 'c'; BiTree d = new BiTreeNode; d->data = 'd'; BiTree e = new BiTreeNode; e->data = 'e'; BiTree f = new BiTreeNode; f->data = 'f'; a->lchild = b; a->rchild = e; b->lchild = c; b->rchild = d; e->lchild = f; printf("\nPreVisit\n"); PreVisit(a); printf("\n"); PreVisitNoneRecursive(a); printf("\nInorderVisit\n"); InorderVisit(a); printf("\n"); InorderVisitNoneRecursive(a); printf("\nPostVisit\n"); PostVisit(a); printf("\n"); PostVisitNoneRecursive(a); getchar(); delete a;delete b; delete c; delete d; delete e;delete f; return 0; }
输出:
PreVisit
a, b, c, d, e, f,
a, b, c, d, e, f,
InorderVisit
c, b, d, a, f, e,
c, b, d, a, f, e,
PostVisit
c, d, b, f, e, a,
c, d, b, f, e, a,
相关文章推荐
- 请用递归和非递归俩种方法实现二叉树的前序遍历。
- 递归和非递归遍历二叉树
- 二叉树的几种遍历方法及递归和非递归的实现
- C语言进行二叉树的递归遍历和非递归遍历
- 二叉树的三种遍历方法(递归和非递归)
- 二叉树创建及遍历算法(递归及非递归)
- 二叉树实现(包括遍历等各种操作,递归与非递归)
- 二叉树的创建及递归和非递归遍历
- 二叉树的递归遍历以及非递归遍历
- 递归与非递归遍历二叉树(前序、中序、后序)
- 二叉树的三种遍历方式(递归和非递归的实现方法)
- 二叉树的基本操作(创建、递归和非递归遍历、求深度、求叶子数)
- 数据结构——二叉树遍历之“递归与非递归遍历”
- 二叉树----节点个数、高度、递归和非递归遍历、按层遍历
- 二叉树操作(插入、删除、遍历(递归与非递归))
- 二叉树的递归和非递归遍历
- 二叉树的遍历(递归与非递归)
- 二叉树的遍历(递归与非递归)
- 请用递归和非递归俩种方法实现二叉树的前序遍历。
- 二叉树的递归和非递归遍历