c语言实现二叉树不递归前序遍历的代码,请使用链栈
时间: 2024-05-02 19:19:46 浏览: 89
C语言实现二叉树的前序遍历(非递归)
好的,请看下面的代码:
```
typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
typedef struct LinkStack {
BiTree data;
struct LinkStack *next;
} LinkStack, *LiStack;
void PreOrderTraversal(BiTree T) {
LiStack S = NULL;
BiTree p = T;
while (p || S) {
if (p) {
printf("%c", p->data);
if (p->rchild) {
S = Push(S, p->rchild);
}
p = p->lchild;
} else {
p = Pop(S);
}
}
}
LiStack Push(LiStack top, BiTree T) {
LiStack s = (LiStack)malloc(sizeof(LinkStack));
s->data = T;
s->next = top;
top = s;
return top;
}
BiTree Pop(LiStack top) {
if (top) {
BiTree T = top->data;
top = top->next;
return T;
}
return NULL;
}
```
希望能够帮到您!接下来,请问需不需要帮您解答其他问题?
阅读全文