|
发表于 2023-12-6 20:36:55
|
显示全部楼层
代码中存在几个问题:
1. 在`#include`后缺少头文件名。
2. `initbitree()`函数中,初始化树时应该将`data.key`赋值为0,而不是`NULL`。
3. 在`creatbitree()`函数中,在插入结点时忽略了对当前结点的更新。
修改后的代码如下:
- #include <stdio.h>
- #include <stdlib.h>
- #define stack_init_size 100
- #define OK 1
- #define ERROR 0
- typedef int status;
- typedef int KeyType;
- typedef struct {
- KeyType key; // 关键字域
- } TElemType;
- typedef struct BiTNode { // 结点结构
- TElemType data;
- struct BiTNode *lchild, *rchild; // 左右孩子指针
- } BiTNode, *BiTree;
- void initbitree(BiTree &t)
- {
- t=(BiTNode*)malloc(sizeof(BiTNode));
- t->data.key=0;
- t->lchild=NULL;
- t->rchild=NULL;
- }
- status creatbitree(BiTree &t,int e)
- {
- if(!t){
- t=(BiTNode*)malloc(sizeof(BiTNode));
- t->data.key=e;
- t->lchild=NULL;
- t->rchild=NULL;
- }
- else
- {
- if(e>t->data.key){
- return creatbitree(t->rchild,e);
- }
- else{
- return creatbitree(t->lchild,e);
- }
- }
- return OK;
- }
- void InOrder(BiTree t)
- {
- if(t!=NULL)
- {
- InOrder(t->lchild);
- printf("%d ",t->data.key);
- InOrder(t->rchild);
- }
- }
- int main()
- {
- BiTree t;
- int e=1;
- initbitree(t);
- printf("请输入一串数字,以数字0代表结束:");
- scanf("%d",&e);
- while(e!=0){
- creatbitree(t,e);
- scanf("%d",&e);
- }
- InOrder(t);
- }
复制代码
请尝试运行代码,看是否解决了问题。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。 |
|