主题:自己写的作业题2,不知道哪里错了……请帮忙看看
二 二叉树
#include<stdio.h>
#include<stdlib.h>
#define M 100
typedef struct BTnode
{int data;
struct BTNode *lchild;
struct BTNode *rchild;
}BTNode,*BiTree;
void Create(BiTree T)
{
int a;
scanf("%d",&a);
if(a==0) T=NULL;
else{T=(BiTree)malloc(sizeof(BTNode));
T->data=a;
Create(T->lchild);
Create(T->rchild);
}
}
/*Create*/
int Count(BiTree T)
{ if(t==NULL)
return 0;
else
if(T->lchild==NULL&&T->rchild==NULL)
return 1;
else
return (Count(t->lchild)+Count(t->rchild)+1);
}
/*Count*/
int Depth(Bitree T)
{int dep1,dep2;
if(t==NULL)
return 0;
else
{dep1=Depth(t->lchild);
dep2=Depth(t->rchild);
if(dep1>dep2)
return (dep1+1);
else return (dep2+1);
}
}
/*Depth*/
Bitree *que[M];
int front=0;rear=0;
Bitree * creat()
{Bitree *T;
int x;
scanf("%d",&x);
if(x==0)
T=NULL;
else
{T=malloc(sizeof(Bitree));
T->lchild=creat();
T->rchild=creat();
}
return T;
}
/*creat*/
void inorder(T)
Bitree *t;
{
if(T=NULL)
{inorder(T->lchild);
printf("%4d",T->data);
inorder(T->rchild);
}
}
/*inorder*/
void enqueue(T)
Bitree *T;
{
if(front!=(rear+1)%M)
{rear=(rear+1)%M;
que[rear]=T;
}
}
/*enqueue*/
Bitree *delqueue()
{if(front==rear)
return NULL;
front=(front+1)%M;
return(que[front]);
}
/*delqueue*/
void levorder(T)
Bitree *T;
{Bitree *p;
if(T!=NULL)
{enqueue(t);
while(front!=rear)
{p=delqueue();
printf("%4d",p->data);
if(p->lchild!=NULL)
enqueue(p->lchild);
if(p->rchild!=NULL)
enqueue(p->rchild);
}
}
}
/*levorder*/
void main()
{
BiTree T;
Create(T);
printf("%d",Count(T));
printf("\n");
printf("%d",Depth(T));
printf("\n");
levorder(T);
}
谢谢!
#include<stdio.h>
#include<stdlib.h>
#define M 100
typedef struct BTnode
{int data;
struct BTNode *lchild;
struct BTNode *rchild;
}BTNode,*BiTree;
void Create(BiTree T)
{
int a;
scanf("%d",&a);
if(a==0) T=NULL;
else{T=(BiTree)malloc(sizeof(BTNode));
T->data=a;
Create(T->lchild);
Create(T->rchild);
}
}
/*Create*/
int Count(BiTree T)
{ if(t==NULL)
return 0;
else
if(T->lchild==NULL&&T->rchild==NULL)
return 1;
else
return (Count(t->lchild)+Count(t->rchild)+1);
}
/*Count*/
int Depth(Bitree T)
{int dep1,dep2;
if(t==NULL)
return 0;
else
{dep1=Depth(t->lchild);
dep2=Depth(t->rchild);
if(dep1>dep2)
return (dep1+1);
else return (dep2+1);
}
}
/*Depth*/
Bitree *que[M];
int front=0;rear=0;
Bitree * creat()
{Bitree *T;
int x;
scanf("%d",&x);
if(x==0)
T=NULL;
else
{T=malloc(sizeof(Bitree));
T->lchild=creat();
T->rchild=creat();
}
return T;
}
/*creat*/
void inorder(T)
Bitree *t;
{
if(T=NULL)
{inorder(T->lchild);
printf("%4d",T->data);
inorder(T->rchild);
}
}
/*inorder*/
void enqueue(T)
Bitree *T;
{
if(front!=(rear+1)%M)
{rear=(rear+1)%M;
que[rear]=T;
}
}
/*enqueue*/
Bitree *delqueue()
{if(front==rear)
return NULL;
front=(front+1)%M;
return(que[front]);
}
/*delqueue*/
void levorder(T)
Bitree *T;
{Bitree *p;
if(T!=NULL)
{enqueue(t);
while(front!=rear)
{p=delqueue();
printf("%4d",p->data);
if(p->lchild!=NULL)
enqueue(p->lchild);
if(p->rchild!=NULL)
enqueue(p->rchild);
}
}
}
/*levorder*/
void main()
{
BiTree T;
Create(T);
printf("%d",Count(T));
printf("\n");
printf("%d",Depth(T));
printf("\n");
levorder(T);
}
谢谢!