void PreOrder(BiTree T){
if(T!=NULL){
printf("%c",T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
void PostOrder(BiTree T){
if(T!=NULL){
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%c",T->data);
}
}
void InOrder(BiTree T){
if(T!=NULL){
InOrder(T->lchild);
printf("%c",T->data);
InOrder(T->rchild);
}
}
void LevelOrder(BiTree T){
LinkQueue Q;
InitLinkQueue(Q);
BiTree t;
EnLinkQueue(Q,T);
while (!IsEmpty(Q)){
DeLinkQueue(Q,t);
putchar(t->data);
if(t->lchild!=NULL){
EnLinkQueue(Q,t->lchild);
}
if(t->rchild!=NULL){
EnLinkQueue(Q,t->rchild);
}
}
}
int main() {
BiTree pnew;
BiTree tree=NULL;
BiElemType c;
ptag_t phead=NULL,ptail=NULL,list_pnew=NULL,pcur;
while (scanf("%c",&c)){
if(c=='\n'){
break;
}
pnew=(BiTree) calloc(1,sizeof (BiTNode));
pnew->data=c;
list_pnew=(ptag_t) calloc(1,sizeof (tag_t));
list_pnew->p=pnew;
if(tree==NULL){
tree=pnew;
phead=list_pnew;
ptail=list_pnew;
pcur=list_pnew;
} else{
ptail->pnext=list_pnew;
ptail=list_pnew;
if(NULL==pcur->p->lchild){
pcur->p->lchild=pnew;
} else if(NULL==pcur->p->rchild){
pcur->p->rchild=pnew;
pcur=pcur->pnext;
}
}
}
PreOrder(tree);
printf("----------PreOrder\n");
PostOrder(tree);
printf("----------PostOrder\n");
InOrder(tree);
printf("----------InOrder\n");
LevelOrder(tree);
printf("----------LevelOrder\n");
return 0;
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
