注册 登录
编程论坛 数据结构与算法

统计二叉树结点函数的问题 ,求大神看看错哪了

邪风揽明月 发布于 2013-11-14 22:40, 573 次点击
#include<stdio.h>
#include<stdlib.h>
int m,n,k;
typedef struct node
{
    int data;
    struct node*lchild,*rchild;
}Bonde;
Bonde *creat()
{
    int i,x,j;Bonde *q,*s[20];
    Bonde*t=NULL;printf("\n i,x=");scanf("%d%d",&i,&x);
    while((i!=0)&&(x!=0))
    {
        q=(struct node*)malloc(sizeof(struct node));
        q->data=x;q->lchild=NULL;q->rchild=NULL;
        s[i]=q;
        if(i==1) t=q;
        else{
            j=i/2;
            if((i%2)==0) s[j]->lchild=q;
            else s[j]->rchild=q;
        }
        printf("\n i,x=");scanf("%d%d",&i,&x);
    }
    return t;
}
void middleorder(Bonde *p)
{
    if(p!=NULL)
    {
        middleorder(p->lchild);
        printf("%6d",p->data);
        middleorder(p->rchild);
    }
}
void lastorder(Bonde *p)
{
    if(p!=NULL)
    {
        lastorder(p->lchild);
        lastorder(p->rchild);
        printf("%6d",p->data);
    }
}
void lastorderz(Bonde *p)
{
    Bonde*s[10],*q;int s2[20];
    int top=0,boo=1;
    q=p;
    printf("后根遍历");
    do{while(q!=NULL)
    {top++;s[top]=q;
    s2[top]=1;
    q=q->lchild;
}
if(top==0) boo=0;
else{
    if(s2[top]==1)
    {
        s2[top]=2;
        q=s[top];
        q=q->rchild;
    }
    else{
        q=s[top];
        s2[top]=0;top--;
        printf("%6d",q->data);
        q=NULL;}
    }
}while(boo==1);
printf("\n");
}
void levelorder(Bonde*p)
{
    Bonde*q[20];
    int front=0,rear=0;
    if(p!=NULL){rear++;q[rear]=p;}
    while(front!=rear)
    {
        front++;p=q[front];printf("%6d",p->data);
        if(p->lchild!=NULL){rear++;q[rear]=p->lchild;}
        if(p->rchild!=NULL){rear++;q[rear]=p->rchild;}
    }
}
void numofleaf(Bonde*p,int &m)
{
    if(p!=NULL)
    {if(p->lchild==NULL&&p->rchild==NULL)
    m++;
    numofleaf(p->lchild,m);
    numofleaf(p->rchild,m);
}
}
int fstdepth(Bonde*p)
{
    if(p==NULL) return 0;
    else{
        int dl=fstdepth(p->lchild);
        int dr=fstdepth(p->rchild);
        return 1+(dl>dr? dl:dr);
    }
}
main()
{char ch; int k,n,dep;
do{
    printf("\n\n\n");
    printf("\n\n       1.建立二叉树方法 ");
    printf("\n\n       2.先序,中序,后序递归遍历二叉树");
    printf("\n\n       3.计算树中叶子结点个数");
    printf("\n\n       4.求树的深度");
    printf("\n\n       5.结束程序运行");
    printf("\n==============================================");
    printf("\n请输入您的选择(1,2,3,4)");scanf("%d",&k);
    switch(k)
    {case 1: t=creat();break;
     case 2:{printf("先序遍历二叉树结果\n");
             fstdepth(t);
             printf("中序遍历二叉树结果\n");
             middleorder(t);
             printf("后序遍历二叉树结果\n");
             lastorder(t);
             printf("\n\n 按Enter键,继续。");ch=getch();
}break;
     case 3:{ numofleaf(t,n);
     printf("\n 二叉树叶子结点数num=%d",n);
     printf("\n\n 按Enter键,继续。");ch=getch();getch()
     }break;
     case 4:{ dep=fstdepth(t);
              printf("\n二叉树的深度为dep=%d",dep);
              printf("\n\n按Enter键,继续。");ch=getch();
           }
           case 5:exit(0);
  }
  printf("\n---------");
  }while(k>=1&&k<5);
  printf("\n      再见!");
  printf("\n      按Enter键,返回。");ch=getch();getchar
  }
3 回复
#2
邪风揽明月2013-11-14 22:40
{case 1: t=creat();break;


C-free提示这行错误
#3
qunxingw2013-11-15 11:44
switch(k)
    {case 1: t=creat();break//此t非彼t
#4
邪风揽明月2013-11-15 12:47
回复 3楼 qunxingw
那要怎么修改
1