后序遍历非递归算法
- 考研
- 关注:1.35W次
#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) //遍历左子树
{
= p;
= L; //标记为左子树
push(s,x);
p=p->lchild;
}
while (!StackEmpty(s) &&[]==R)
{
x = pop(s);
p = ;
visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点
}
if (!StackEmpty(s))
{
[] =R; //遍历右子树
p=[]->rchild;
}
}while (!StackEmpty(s));
}//PostOrderUnrec
- 文章版权属于文章作者所有,转载请注明 https://www.moshuiwu.com/kyjy/kwpoy1.html