本文共 934 字,大约阅读时间需要 3 分钟。
#include#include typedef struct btnode /*定义结点类型*/ { char data; struct btnode *lchild,*rchild; }bitnode,*bitree;bitree createbitree() { bitree t; char ch; scanf("%c",&ch); if(ch==' ') t=NULL; else { t=malloc (sizeof(bitnode)); /*申请生成新的结点*/ t->data=ch; t->lchild=createbitree(); /*递归构造左子树*/ t->rchild=createbitree(); /*递归构造右子树*/ } return t; }int binodecount(bitree bt) { if(bt==NULL) return 0; else return(binodecount(bt->lchild)+binodecount(bt->rchild)+1); }int bileafcount(bitree bt) { if(bt==NULL) return 0; else if(bt->lchild==NULL || bt->rchild==NULL) return 1; /*若是叶子结点,则返回1*/ else return(bileafcount(bt->lchild)+bileafcount(bt->rchild));}void main(){ bitree bt1; printf("创建二叉树:\n"); bt1=createbitree(); printf("二叉树结点的个数为:"); printf("%d",binodecount(bt1)); printf("\n"); printf("二叉树叶子结点的个数为:"); printf("%d",bileafcount(bt1)); printf("\n");}
转载地址:http://djyki.baihongyu.com/