p127-12
#include <stdio.h>
#include <stdlib.h>
#define ElemType int
typedef struct BiTNode {
ElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
// Mine
void Print(BiTree T, ElemType x) {
if (T->data == x) {
return;
}
BiTree S[100];
int top = -1;
S[++top] = T;
BiTree p = T, signal = NULL;
while (top != -1) {
if (p->data == x) {
top--;
break;
}
if (p->lchild) {
S[++top] = p->lchild;
p = p->lchild;
} else {
p = S[top];
if (p->rchild && p->rchild != signal) {
S[++top] = p->rchild;
p = p->rchild;
} else {
top--;
signal = p;
p = NULL;
}
}
}
while (top != -1) {
printf("%d", S[top--]->data);
}
}
typedef struct {
BiTree t;
int tag;
} stack;
void Search(BiTree bt, ElemType x) {
stack s[100];
int top = 0, i;
while (bt || top > 0) {
while (bt && bt->data != x) {
s[++top].t = bt;
s[top].tag = 0;
bt = bt->lchild;
}
if (bt->data == x) {
for (i = 1; i <= top; i++) {
printf("%d", s[i].t->data);
}
exit(1);
}
while (top != 0 && s[top].tag == 1) {
top--;
}
if (top != 0) {
s[top].tag = 1;
bt = s[top].t->rchild;
}
}
}