|
发表于 2005-10-31 02:30:19
|
显示全部楼层
<>C语言版的如下:</P>
<><FONT color=#2b4dd5>#include"stdio.h"<BR>#include"stdlib.h"</FONT></P>
<><FONT color=#2b4dd5>typedef struct BiTNode<BR>{<BR> int data;<BR> struct BiTNode *lchild;<BR> struct BiTNode *rchild;<BR>}BiTNode,BiTree;</FONT></P>
<P><FONT color=#2b4dd5>BiTree *root;<BR>//int n=0;</FONT></P>
<P><FONT color=#2b4dd5>BiTree *creat()<BR>{<BR> BiTree *bt,*p,*s[30];<BR> int i,j,x;<BR> printf("i,x=");scanf("%d%d",&i,&x);<BR> while((i!=0)&&(x!=0))<BR> {<BR>// if((p=(BiTNode *)malloc(sizeof(BiTNode)))==NULL) return NULL;<BR> p=(BiTNode *)malloc(sizeof(BiTNode));<BR> p->data=x;p->lchild=NULL;p->rchild=NULL;<BR> s=p;<BR> if(i==1) bt=p;<BR> else<BR> {<BR> j=i/2;<BR> if((i%2)==0) s[j]->lchild=p;<BR> else s[j]->rchild=p;<BR> }<BR> printf("i,x=");scanf("%d%d",&i,&x);<BR> }<BR> return (bt);<BR>}</FONT></P>
<P><FONT color=#2b4dd5>void inorder(BiTree *bt)<BR>{<BR> if(bt==NULL) return;<BR> inorder(bt->lchild);<BR> printf("%d\t",bt->data);<BR> inorder(bt->rchild);<BR>}</FONT></P>
<P><FONT color=#2b4dd5>void main()<BR>{<BR> printf("create a tree:");<BR> root=creat();<BR> printf("travel:");<BR> inorder(root);<BR>}</FONT></P>
|
|