C语言:建立二叉树,在main方法里写代码调试?

#include "stdlib.h"
#include "stdio.h"
typedef struct btreenode{
char data;
struct btreenode *lchild, *rchild;
}bitnode;
bitnode *creabittree(bitnode *T) //建立二叉树函数
{ char c, x;
T=(bitnode *)malloc(sizeof(bitnode)); //①
T->lchild=NULL;
T->rchild=NULL;
printf( "Input The Node_Data :");
scanf("%c%c", &T->data, &x);//②
printf( "Is There %c's LeftChild(Y/N)",T->data);
scanf("%c%c", &c,&x);
if (c=='y'||c=='Y') T->lchild=creabittree(T->lchild);
printf( "Is There %c's RightChild(Y/N)",T->data);
scanf("%c%c", &c, &x);
if (c=='y'||c=='Y') T->rchild=creabittree(T->rchild);
return (T);
}
main方法要自己写,主要是调试这个程序?谢谢!

#include<stdlib.h>
typedef struct node/*二叉链表结构声明*/
{
struct node *lchild;
char data;
struct node *rchild;
}bitnode,*bitree;/*bitnode、bitree为该结构体类型*/
bitree CreatTree()/*创建二叉链表*/
{
char a;
bitree new;
scanf("%c",&a);
if(a=='#')
return NULL;
else
{
new=(bitree)malloc(sizeof(bitnode));
new->data=a;
new->lchild=CreatTree();/*递归创建左子树*/
new->rchild=CreatTree();/*递归创建右子树*/
}
return new;
}
int btreedepth(bitree bt)/*自定义函数btreedepth()求二叉树的深度*/
{
int ldepth,rdepth;
if(bt==NULL)
return 0;
else
{
ldepth=btreedepth(bt->lchild);
rdepth=btreedepth(bt->rchild);
return (ldepth>rdepth?ldepth+1:rdepth+1);
}
}
int ncount(bitree bt)/*自定义函数ncount求结点的个数*/
{
if(bt==NULL)
return 0;
else return(ncount(bt->lchild)+ncount(bt->rchild)+1);
}
int lcount(bitree bt)/*自定义函数lcount求叶子结点的个数*/
{
if(bt==NULL)
return 0;
else if(bt->lchild==NULL&&bt->rchild==NULL)
return 1;
else return(lcount(bt->lchild)+lcount(bt->rchild));
}
void print(bitree bt)/*自定义函数print用中序遍历的方式输出二叉树结点内容*/
{
if(bt!=NULL)
{
print(bt->lchild);
printf("%c",bt->data);
print(bt->rchild);
}
}
void main()
{
bitree root;
root=CreatTree();/*调用函数创建二叉链表*/
printf("contents of binary tree:\n");
print(root);/*调用函数输出结点内容*/
printf("\ndepth of binary tree:%d\n",btreedepth(root));/*调用函数输出树的深度*/
printf("the number of the nodes:%d\n",ncount(root));/*调用函数输出树中结点个数*/
printf("the number of the leaf nodes:%d\n",lcount(root));/*调用函数输出树中叶子结点个数*/
}

追问

谢谢你的回答,但是不是我想要的结果。

。。。。。里面的调试代码是我想要的,谢谢。

温馨提示:答案为网友推荐,仅供参考