鱼C论坛

 找回密码
 立即注册
查看: 848|回复: 3

关于创建一个二叉排序树

[复制链接]
发表于 2023-12-6 20:36:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
创建一颗二叉排序树后中序输出,可以帮我看看哪里出问题了吗?
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define stack_init_size 100
  4. #define OK 1
  5. #define ERROR 0
  6. typedef int status;
  7. typedef int KeyType;
  8. typedef struct {
  9.     KeyType  key;    // 关键字域
  10. } TElemType;
  11. typedef struct BiTNode { // 结点结构
  12.     TElemType      data;
  13.     struct BiTNode  *lchild, *rchild;    // 左右孩子指针
  14. } BiTNode, *BiTree;
  15. void initbitree(BiTree &t)
  16. {
  17.         t=(BiTNode*)malloc(sizeof(BiTNode));
  18.         t->data.key=NULL;
  19.         t->lchild=NULL;
  20.         t->rchild=NULL;
  21. }

  22. status creatbitree(BiTree &t,int e)
  23. {
  24.         BiTree s,p;
  25.         s=(BiTNode*)malloc(sizeof(BiTNode));
  26.         s->data.key=e;s->lchild=s->rchild=NULL;
  27.         if(!t){
  28.                 t=s;
  29.         }
  30.         else
  31.         {
  32.                 if(e>t->data.key){
  33.                         t->rchild=s;
  34.                 }
  35.                 else{
  36.                         t->lchild=s;
  37.                 }
  38.                 return OK;
  39.         }
  40.        
  41. }
  42. void InOrder(BiTree t)
  43. {
  44.         if(t!=NULL)
  45.         {
  46.                 InOrder(t->lchild);
  47.                 printf("%d ",t->data.key);
  48.                 InOrder(t->rchild);
  49.         }
  50. }
  51. int main()
  52. {
  53.         BiTree t;
  54.         int e=1;
  55.         initbitree(t);
  56.         printf("请输入一串数字,以数字0代表结束:");       
  57.         scanf("%d",&e);
  58.         while(e!=0){
  59.                 creatbitree(t,e);
  60.                 scanf("%d",&e);
  61.         }
  62.         InOrder(t);
  63.        
  64. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-12-6 20:36:55 | 显示全部楼层
代码中存在几个问题:

1. 在`#include`后缺少头文件名。

2. `initbitree()`函数中,初始化树时应该将`data.key`赋值为0,而不是`NULL`。

3. 在`creatbitree()`函数中,在插入结点时忽略了对当前结点的更新。

修改后的代码如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define stack_init_size 100
  4. #define OK 1
  5. #define ERROR 0
  6. typedef int status;
  7. typedef int KeyType;
  8. typedef struct {
  9.     KeyType  key;    // 关键字域
  10. } TElemType;
  11. typedef struct BiTNode { // 结点结构
  12.     TElemType      data;
  13.     struct BiTNode  *lchild, *rchild;    // 左右孩子指针
  14. } BiTNode, *BiTree;
  15. void initbitree(BiTree &t)
  16. {
  17.     t=(BiTNode*)malloc(sizeof(BiTNode));
  18.     t->data.key=0;
  19.     t->lchild=NULL;
  20.     t->rchild=NULL;
  21. }
  22. status creatbitree(BiTree &t,int e)
  23. {
  24.     if(!t){
  25.         t=(BiTNode*)malloc(sizeof(BiTNode));
  26.         t->data.key=e;
  27.         t->lchild=NULL;
  28.         t->rchild=NULL;
  29.     }
  30.     else
  31.     {
  32.         if(e>t->data.key){
  33.             return creatbitree(t->rchild,e);
  34.         }
  35.         else{
  36.             return creatbitree(t->lchild,e);
  37.         }
  38.     }
  39.     return OK;
  40. }
  41. void InOrder(BiTree t)
  42. {
  43.     if(t!=NULL)
  44.     {
  45.         InOrder(t->lchild);
  46.         printf("%d ",t->data.key);
  47.         InOrder(t->rchild);
  48.     }
  49. }
  50. int main()
  51. {
  52.     BiTree t;
  53.     int e=1;
  54.     initbitree(t);
  55.     printf("请输入一串数字,以数字0代表结束:");   
  56.     scanf("%d",&e);
  57.     while(e!=0){
  58.         creatbitree(t,e);
  59.         scanf("%d",&e);
  60.     }
  61.     InOrder(t);
  62. }
复制代码

请尝试运行代码,看是否解决了问题。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-6 20:55:26 | 显示全部楼层
FishC_GPT 发表于 2023-12-6 20:36
代码中存在几个问题:

1. 在`#include`后缺少头文件名。

这里的return creatbitree(t->rchild,e)是什么意思啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-6 20:58:13 | 显示全部楼层
乔治爱啃脚趾 发表于 2023-12-06 20:55
这里的return creatbitree(t->rchild,e)是什么意思啊

`return creatbitree(t->rchild,e)`的意思是在当前结点的右子树上继续递归调用`creatbitree()`函数,将插入节点的值和右子树作为参数传入。这样可以将新节点插入到正确的位置上。

在二叉排序树中,比当前结点值大的元素需要插入到右子树上,因此需要递归调用`creatbitree()`函数,并将当前节点的右子树和待插入元素作为参数传入。

同理,`return creatbitree(t->lchild,e)`的意思是在当前结点的左子树上继续递归调用`creatbitree()`函数,将插入节点的值和左子树作为参数传入。

通过不断地递归调用`creatbitree()`函数,并根据比较结果选择递归调用的方向,最终将新节点插入到正确的位置上,实现了二叉排序树的构建。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-9 21:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表