#include<stdio.h>
#include<stdlib.h>
typedef char ElemType;
typedef struct node {
ElemType data;
int ltag,rtag;
struct node *right,*left;
} BiTree;
BiTree *CreateBiTree() {
char ch;
BiTree *t;
scanf("%c",&ch);
if(ch!='#') {
t=(BiTree *)malloc(sizeof(BiTree));
t->data=ch;
t->ltag=0;
t->rtag=0;
t->left=CreateBiTree();
t->right=CreateBiTree();
} else {
t=NULL;
}
return t;
}
BiTree *ThrtBiTree(BiTree *t) {
BiTree *thrt,*s[100],*p,*pre;
int top=0;
thrt=(BiTree *)malloc(sizeof(BiTree));
thrt->data='\0';
thrt->right=thrt;
thrt->ltag=0;
thrt->rtag=1;
if(t==NULL) {
thrt->left=thrt;
} else {
thrt->left=t;
p=t;
pre=thrt;
do {
while(p!=NULL) {
s[top++]=p;
p=p->left;
}
if(top>0) {
p=s[--top];
if(p->left==NULL) {
p->ltag=1;
p->left=pre;
}
if(pre->right==NULL) {
pre->rtag=1;
pre->right=p;
}
pre=p;
p=p->right;
}
} while(top>0||p!=NULL);
pre->right=thrt;
pre->rtag=1;
thrt->right=pre;
}
return thrt;
}
void LDR(BiTree *t) {
BiTree *p;
p=t->left;
while(p!=t) {
while(p->ltag!=1) {
p=p->left;
}
printf("%c ",p->data);
while(p->rtag==1&&p->right!=t) {
p=p->right;
printf("%c ",p->data);
}
p=p->right;
}
}
BiTree *Next(BiTree *p) {
BiTree *q;
q=p->right;
if(p->rtag!=1){
while(q->ltag==0){
q=q->left;
}
}
return q;
}
void Seek(BiTree *t,ElemType x) {
BiTree *p;
p=t->left;
if(p!=t) {
while(p->ltag!=1&&p!=t) {
p=p->left;
}
while(p->data!=x&&p!=t) {
p=Next(p);
}
if(p==t) {
printf("Not Found!\n");
}else{
printf("%c",p->data);
}
} else {
printf("ERROR!\n");
}
}
int main() {
ElemType x;
BiTree *mytree;
mytree=CreateBiTree();
mytree=ThrtBiTree(mytree);
LDR(mytree);
printf("\n");
getchar();
scanf("%c",&x);
Seek(mytree,x);
}
若有问题欢迎提出!