主题:[讨论]我是新手,帮忙找错
#include "malloc.h"
#include "stdlib.h"
typedef char telemtype;
typedef struct bitnode {
telemtype data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
bitree create()
{
char ch;
bitree t;
scanf("%c",&ch);
if(ch=='#') t=NULL;
else
{
if(!(t=new BiTNode))
exit(OVERFLOW);
t->data=ch;
CreateBiTree(t->lchild);
CreateBiTree(t->rchild);
}
return t;
}
void preorder(bitree t)
{
if(t)
{ printf("%3c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void inorder(bitree t)
{
if(bitree)
{
visit(t->lchild);
Inorder(t,visit);
Inorder(t->rchild,visit);
}
}
int depth1(bitree t)
//后序遍历
{
if(!t) depthval=0;
else
{
depthLeft=depth(t->lchild);
depthRight=depth(t->rchild);
depthval=1+(depthLeft>depthRight?depthLeft:depthRight);
}
}
void depth2(bitree t,int h,int *depth)
//先序遍历
{
if(t)
{ if(h>*depth) *depth=h;
bitreedepth(t->lchild,h+1,depth);
bitreedepth(t->rchild,h+1,depth);
}
}
int countleaf(bitree t)
{
if(t)
{
if((!t->lchild)&&(!t->rchild))
count++;
countleaf(t->lchild,count);
}
}
main()
{
bitree t;
int dep=0;
t=create();
preorder(t);
printf("\n");
inorder(t);
printf("\nheight of tree is:%d\n",depth1(t));
depth2(t,1,&dep);
printf("height of tree is:%d\n",dep);
printf("leaf number is:%d\n",countleaf(t));
}
#include "stdlib.h"
typedef char telemtype;
typedef struct bitnode {
telemtype data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
bitree create()
{
char ch;
bitree t;
scanf("%c",&ch);
if(ch=='#') t=NULL;
else
{
if(!(t=new BiTNode))
exit(OVERFLOW);
t->data=ch;
CreateBiTree(t->lchild);
CreateBiTree(t->rchild);
}
return t;
}
void preorder(bitree t)
{
if(t)
{ printf("%3c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void inorder(bitree t)
{
if(bitree)
{
visit(t->lchild);
Inorder(t,visit);
Inorder(t->rchild,visit);
}
}
int depth1(bitree t)
//后序遍历
{
if(!t) depthval=0;
else
{
depthLeft=depth(t->lchild);
depthRight=depth(t->rchild);
depthval=1+(depthLeft>depthRight?depthLeft:depthRight);
}
}
void depth2(bitree t,int h,int *depth)
//先序遍历
{
if(t)
{ if(h>*depth) *depth=h;
bitreedepth(t->lchild,h+1,depth);
bitreedepth(t->rchild,h+1,depth);
}
}
int countleaf(bitree t)
{
if(t)
{
if((!t->lchild)&&(!t->rchild))
count++;
countleaf(t->lchild,count);
}
}
main()
{
bitree t;
int dep=0;
t=create();
preorder(t);
printf("\n");
inorder(t);
printf("\nheight of tree is:%d\n",depth1(t));
depth2(t,1,&dep);
printf("height of tree is:%d\n",dep);
printf("leaf number is:%d\n",countleaf(t));
}