小心爱 发表于 2014-11-28 16:55:33

大神们谁给我看看啊啊

#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *lchild,*rchild;
} BTroot;
int sum=0,m=sizeof(BTroot);
BTroot *root;
voidCR_bt(int x)//构建而擦排序书
{BTroot *s,*p,*q;
   s=(BTroot *)malloc(m);
   s->data=x;
   s->rchild=NULL;s->rchild=NULL;
   if(!root) {root=s;return;}
   p=root;

   while(p!=NULL){ q=p;
   if(p->data==x) {printf("the number is hava\n");return ;}
   else if(p->data>x) p=p->lchild;
         else p=p->rchild;
   }
if(x>q->data)q->rchild=s;
else q->lchild=s;
}

void Nchild_count(BTroot *root)
{if(root!=NULL){
        if((root->lchild==NULL)&&(root->rchild==NULL)){
       sum++; printf("这个叶子结点的值为%d",root->data);}
   Nchild_count(root->lchild);
   Nchild_count(root->rchild);}


}
int depth( BTroot*root)
{ int d,p;
p=0;
if(root==NULL)return p;
else {d=depth(root->lchild);
         if(d>p)p=d;
      d=depth(root->rchild);
                  if(d>p)p=d;
}
p=p+1;
return p;
}


main()
{int x,p;
   root=NULL;
do {printf("输入要建立二叉树结点值\n");
      scanf("%d",&x);
      CR_bt(x);
}
          while(x!=99);

Nchild_count(root);
printf("叶子结点的总数为=%d",sum);
p=depth(root);
printf("树的深度为=%d",p);
}

页: [1]
查看完整版本: 大神们谁给我看看啊啊