CH5_5.C
上传用户:lgb298
上传日期:2013-03-22
资源大小:1025k
文件大小:1k
源码类别:

软件工程

开发平台:

C/C++

  1. #include <alloc.h>
  2. #include <stdio.h>
  3. typedef struct node
  4. {   char data;
  5.     struct node *lchild,*rchild;
  6. }JD;
  7. void countleaf(JD *bt,int *count)
  8. {   if(bt!=NULL)
  9.     {  if((bt->lchild==NULL)&&(bt->rchild==NULL))
  10.        {   (*count)++;
  11.    return;
  12.        }
  13.        countleaf(bt->lchild,count);
  14.        countleaf(bt->rchild,count);
  15.     }
  16. }
  17. JD *crt_bt_pre(JD *bt)
  18. {  char ch;
  19.    printf("ch=");
  20.    scanf("%c",&ch);
  21.    getchar();
  22.    if(ch==' ')  bt=NULL;
  23.    else
  24.    {   bt=(JD *)malloc(sizeof(JD));
  25.        bt->data=ch;
  26.        bt->lchild=crt_bt_pre(bt->lchild);
  27.        bt->rchild=crt_bt_pre(bt->rchild);
  28.    }
  29.    return(bt);
  30. }
  31. void main()
  32. {  /* ABC00DE0G00F000  */
  33.     JD *head=NULL;
  34.     int count=0;
  35.    head=crt_bt_pre(head);
  36.    countleaf(head,&count);
  37.    printf("count of leaf node is %dn",count);
  38. }