首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

二叉树深度的C程序 帮忙看一下正误 本人看了好久都找不出原因,该怎么解决

2012-03-13 
二叉树深度的C程序帮忙看一下正误本人看了好久都找不出原因/*输入ABC##DE#G##F###回车后得不到5,是不是输

二叉树深度的C程序 帮忙看一下正误 本人看了好久都找不出原因
/*输入ABC##DE#G##F###回车后得不到5,是不是输入有问题。*/
typedef   struct   BiTNode{
char   data;
struct   BiTNode   *lchild,*rchild;
}BiTNode,*BiTree;
#define   ERROR   0
#include <stdio.h>
BiTree   CreateBiTree(BiTree   T)
{char   ch;
scanf( "%c ",&ch);
if(ch== '   ')   T=NULL;
else
{if(!(T=(BiTNode   *)malloc(sizeof(BiTNode))))   return   ERROR;
T-> data=ch;
CreateBiTree(T-> lchild);
CreateBiTree(T-> rchild);
}
return   T;
}

  int   High(BiTree   T)
{   int   n,nl,nr;
if(T)
{nl=High(T-> lchild);
nr=High(T-> rchild);
if(nl> =nr)
n=nl+1;
else
n=nr+1;
}
else
n=0;
return(n);
}

main()
{BiTree   T;int   h;
T=CreateBiTree(T);
  h=High(T);
  printf( "%d ",h);
}



[解决办法]
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

#define ERROR 0

BiTree CreateBiTree()// <--------
{
BiTree T;
char ch;
ch = getch();// <--------
printf( "%c ",ch);// <--------
if(ch== ' ') T=0;
else
{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
return ERROR;
T-> data=ch;
T-> lchild = CreateBiTree();// <--------
T-> rchild = CreateBiTree();// <--------
}
return T;
}

int High(BiTree T)
{
int n,nl,nr;
if(T)
{
nl=High(T-> lchild);
nr=High(T-> rchild);
if(nl> =nr)
n=nl+1;
else
n=nr+1;
}
else
n=0;
return(n);
}

int _tmain(int argc, _TCHAR* argv[])
{
BiTree T = 0;int h = 0;
T=CreateBiTree();// <--------
h=High(T);
printf( "%d ",h);
//DeleteTree();
getch();
}

热点排行