http://blog.csdn.net/pipisorry/article/details/37353037
C Implementation:1. First order traversal non-recursive algorithm
#define MAXSIZE 100
typedef struct {
Bitree Elem[maxsize];
int top;
} Sqstack;
void Preorderunrec (Bitree t) {
Sqstack s;
Stackinit (s);
p=t;
while (P!=null | |! Stackempty (s)) {
while (p!=null) {//Traverse Zuozi
Visite (P->data);
Push (S,P);
p=p->lchild;
}//endwhile
if (! Stackempty (s)) {//through the inline while in the next loop to implement the right subtree traversal
P=pop (s);
p=p->rchild;
}//endif
}//endwhile
}//preorderunrec
2. Middle sequence traversal non-recursive algorithm
#define MAXSIZE 100
typedef struct {
Bitree Elem[maxsize];
int top;
} Sqstack;
void Inorderunrec (Bitree t) {
Sqstack s;
Stackinit (s);
p=t;
while (P!=null | |! Stackempty (s)) {
while (p!=null) {//Traverse Zuozi
Push (S,P);
p=p->lchild;
}//endwhile
if (! Stackempty (s)) {
P=pop (s);
Visite (P->data); Visit the root node.
p=p->rchild; Right subtree traversal via Next loop
}//endif
}//endwhile
}//inorderunrec
3. Post-traversal non-recursive algorithm
#define MAXSIZE 100
typedef enum {l,r} tagtype;
typedef struct {
Bitree ptr;
Tagtype tag;
} Stacknode;
typedef struct {
Stacknode Elem[maxsize];
int top;
} Sqstack;
void Postorderunrec (Bitree t) {
Sqstack s;
Stacknode x;
Stackinit (s);
p=t;
do {
while (p!=null) {//Traverse Zuozi
X.ptr = p;
X.tag = L; Mark as Zuozi
Push (S,X);
p=p->lchild;
}
while (! Stackempty (s) && s.elem[s.top].tag==r) {
x = Pop (s);
p = x.ptr;
Visite (P->data); Tag R, which indicates that the right subtree is complete, so visit the root node.
}
if (! Stackempty (s)) {
S.elem[s.top].tag =r; Traversing the right sub-tree
p=s.elem[s.top].ptr->rchild;
}
} while (! Stackempty (s));
}//postorderunrec
C + + implementation:Ref: Two non-recursive algorithm http://blog.csdn.net/sgbfblog/article/details/7773103 of cross-tree traversal
ref:http://siwei1987.blog.51cto.com/430256/118551
from:http://blog.csdn.net/pipisorry/article/details/37353037
Copyright notice: This article blog Http://blog.csdn.net/pipisorry original articles, blogs, without consent may not be reproduced.
Non-recursive algorithm for 3 kinds of traversal of binary tree