骨骼清奇 天赋异禀功夫:高分悬赏——帮忙看看程序问题

来源:百度文库 编辑:神马品牌网 时间:2024/05/06 00:19:05
要求:按先序序列输入一些字母,建立一个二叉树,然后按中序输出个结点的字母,再求出树的高度和叶子结点数并打印叶子结点。
如输入:abc##de#g##f###
中序输出为cbegdfa
请按本人的程序进行修改,无须自己编写。急用,保证给分。

#include<stdio.h>
#include<malloc.h>
typedef int Status;
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode;//结点定义
//BiTree root;//根结点定义

//构造二叉树
Status BiTree CreateBiTree(BiTree &T){
//按先序输入个接点的值,空格字符#表示空树
//构造二叉链表表示的二叉树T
char ch;
printf("输入接点字符");
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;
}//CreateBinTree

//中序访问并输出各接点字符
Status INORDER(BiTree *T){
if(T)
{INORDER(T->lchild); //中序遍历左子树
printf("\t%c\n",T->data); //访问接点*t
INORDER(T->rchild); //中序遍历右子树
}
}

//计算树的高度
int depth(BiTree T){
int dep1,dep2;
if(T==NULL)return(0);
else
{dep1=depth(T->lchild);
dep2=depth(T->lchild);
if(dep1>dep2)return(dep1+1);
else return(dep2+1);}
}

int LeafCount_BiTree(Bitree T)//求二叉树中叶子结点的数目
{
if(!T) return 0; //空树没有叶子
else if(!T->lchild&&!T->rchild) return 1; //叶子结点
else return Leaf_Count(T->lchild)+Leaf_Count(T->rchild);//左子树的叶子数加上右子树的叶子数
}//LeafCount_BiTree

void main(){
BiTree root;/*根结点指针定义*/
CreateBiTree(&root);/*传入根结点指针的地址在函数中指向二叉树根*/
INORDER(root);
depth(BiTree T)
LeafCount_BiTree(Bitree T)
}

#include<stdio.h>
#include<malloc.h>
#include"stdlib.h"

typedef int Status;
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;//结点定义
//BiTree root;//根结点定义

//构造二叉树
BiTree CreateBiTree(BiTree T)
{
//按先序输入个接点的值,空格字符 表示空树
//构造二叉链表表示的二叉树T
char ch;

scanf("%c",&ch);
if(ch!=EOF&&ch!='\n')
{if(ch==' ') T=NULL;
else
{
T=(BiTree)malloc(sizeof(struct BiTNode));
if(!T) exit(0);
T->data=ch; //生成根接点
T->lchild=CreateBiTree(T->lchild);//构造左子树
T->rchild=CreateBiTree(T->rchild);//构造右子树
} }

return(T);
}

//中序访问并输出各接点字符
void INORDER(BiTree T){
if(T)
{INORDER(T->lchild); //中序遍历左子树
printf("%c\t",T->data); //访问接点*t
INORDER(T->rchild); //中序遍历右子树
}
}

//计算树的高度
int depth(BiTree T){
int dep1,dep2;
if(T==NULL)return(0);
else
{dep1=depth(T->lchild);
dep2=depth(T->lchild);
if(dep1>dep2)return(dep1+1);
else return(dep2+1);}
}

int LeafCount_BiTree(BiTree T)//求二叉树中叶子结点的数目
{
if(!T) return 0; //空树没有叶子
else if(!T->lchild&&!T->rchild) return 1; //叶子结点
else return LeafCount_BiTree(T->lchild)+LeafCount_BiTree(T->rchild);//左子树的叶子数加上右子树的叶子数
}//LeafCount_BiTree

void main(){
struct BiTNode root,*p;/*根结点指针定义*/
printf("\nplease input the treestring you want create :\n");
p=CreateBiTree(&root);/*传入根结点指针的地址在函数中指向二叉树根*/
INORDER(p);
depth(p);
LeafCount_BiTree(p);
}

测试通过:
运行结果为:// 这里输入时按照你的abc##de#g##f### ,注意空格的输入

/*
please input the treestring you want create :
abc de g f
c b e g d f a
*/

1、BiTree类型没有定义
2、CreateBiTree的返回类型写了两个(Status和BiTree)
3、CreateBiTree的参数定义的是BiTree的引用类型,但main里面传的是BiTree的指针类型。
4、既然CreateBiTree返回的是Status类型,你最后一句return T;就不对了。
5、LeafCount_BiTree里两个递归调用把函数名字写错了。