|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目如下:
算法设计题:已知二叉树采用二叉链表储存结构,且数据域均为正整数,写一个算法求数据域的最大值。算法描述函数的函数原型要求是int FindMaxValue (BiTree BT)。
问题如下:
想知道数据域为整形的二叉树怎么输入才能正确创建成功啊?求解答。十分感谢
代码如下:
//已知二叉树采用二叉链表储存结构,且数据域均为正整数,写一个算法求数据域的最大值。算法描述函数的函数原型要求是int FindMaxValue (BiTree BT)。
#include<stdio.h>
#include<stdlib.h>
#define OK 1
typedef int TElemType;
typedef struct BiNode
{
TElemType data;
struct BiNode *lchild,*rchild;
}BiNode,*BiTree;
int CreateBiTree(BiTree &BT)
{
TElemType ch;
scanf("%d",&ch);
if(ch=='#')
BT=NULL;
else
{
if(!(BT=(BiTree)malloc(sizeof(BiNode))))
{
printf("未知错误!");
exit(0);
}
BT->data=ch;
CreateBiTree(BT->lchild);
CreateBiTree(BT->rchild);
}
return OK;
}
void PreOrder(BiTree BT)
{
if(BT!=NULL)
{
printf("%d",BT->data);
PreOrder(BT->lchild);
PreOrder(BT->rchild);
}
}
int FindMaxValue(BiTree BT)
{
if(BT==NULL)
return 0;
int lmax=FindMaxValue(BT->lchild);
int rmax=FindMaxValue(BT->rchild);
int max=((lmax>rmax)?lmax:rmax);
return ((max>BT->data)?max:BT->data);
}
int main()
{
BiTree BT;
CreateBiTree(BT);
PreOrder(BT);
printf("\n");
//printf("%d\n",FindMaxValue(BT));
return 0;
} |
|