Non-recursive algorithm for 3 kinds of traversal of binary tree

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.