《建立一个二叉树》由会员分享,可在线阅读,更多相关《建立一个二叉树(2页珍藏版)》请在金锄头文库上搜索。
1、建立一个二叉树 程序: #include #include #include typedef struct nodechar data;struct node *lchild; struct node *rchild;BTNode;BTNode *CreateTree(BTNode *L,char str100); void DispLeaf(BTNode *L);int i=0; void main()BTNode *L;char str100;L=(BTNode *)malloc(sizeof(BTNode); scanf(“%s“,str);L=CreateTree(L,str);Dis
2、pLeaf(L);BTNode *CreateTree(BTNode *L,char str100)BTNode *k=NULL; if(stri=”0”) L=NULL;else L=(BTNode *)malloc(sizeof(BTNode);L-data=stri;L-lchild=L-rchild=NULL;i+; L-lchild=CreateTree(k,str); i+; L-rchild=CreateTree(k,str);return(L);void DispLeaf(BTNode *L)if(L!=NULL)if(L-lchild=NULL&L-rchild=NULL) printf(“%c“,L-data);elseprintf(“%c“, L-data);DispLeaf(L-lchild);DispLeaf(L-rchild);调试程序程序结果