Posted on 2006-08-16 20:47
Enjoy Life 阅读(429)
评论(0) 编辑 收藏 引用 所属分类:
DS study
#include <stdio.h>
#include <stdlib.h>
typedef struct bitnode{
char data;
struct bitnode *lchild, *rchild;
}bitnode, *bitree;
void createbitree(bitnode **t, int *n){
char x;
bitnode *q;
*n=*n+1;
printf("\n Input %d DATA:",*n);
x=getchar();
if(x!='\n') getchar();
if (x=='\n')
return;
q=(bitnode*)malloc(sizeof(bitnode));
q->data=x;
q->lchild=NULL;
q->rchild=NULL;
*t=q;
printf(" This Address is: %o, Data is: %c,\n Left Pointer is: %o, Right Pointer is: %o",q,q->data,q->lchild,q->rchild);
createbitree(&q->lchild,n);
createbitree(&q->rchild,n);
return;
}
void visit(bitnode *e)
{
printf(" Address: %o, Data: %c, Left Pointer: %o, Right Pointer: %o\n",e,e->data,e->lchild,e->rchild);
}
void preordertraverse(bitnode *t){
if(t){
visit(t);
preordertraverse(t->lchild);
preordertraverse(t->rchild);
return;
}
else
return;
}
void inordertraverse(bitnode *t){
if(t){
inordertraverse(t->lchild);
visit(t);
inordertraverse(t->rchild);
return;
}
else
return;
}
void postordertraverse(bitnode *t){
if(t){
postordertraverse(t->lchild);
postordertraverse(t->rchild);
visit(t);
}
}
main()
{
bitnode *t; int count=0;
int n=0;
printf("\n Please input TREE Data:\n");
createbitree(&t,&n);
preordertraverse(t);
inordertraverse(t);
postordertraverse(t);
}