qq_51710175 2021-06-28 12:19 采纳率: 50%
浏览 45
已采纳

数据结构c语言二叉排序树的构建

谁能帮我把这个程序改好运行出来,谢谢。

include<stdio.h>#include<malloc.h>typedef int keytype;typedef struct node{ keytype key; struct node* lchild; struct node* rchild;}BSTNode, * Bstree;Bstree root;BSTNode* f;void InsertBST(Bstree root, keytype key){ if(!root) {BSTNode* p; p = (BSTNode*)malloc(sizeof(BSTNode)); p->key = key; p->lchild = p->rchild = NULL; root = p; } else if (key < root->key) InsertBST(root->lchild,key); else if (key > root->key) InsertBST(root->rchild, key);} Bstree CreatBST(Bstree root, keytype key){ Bstree T=NULL; while (key)    { InsertBST(T, key); scanf_s("%d", &key); }return T;}void InOrder(Bstree root){ if (root) { InOrder(root->lchild); printf("%d",root->key); InOrder(root->rchild); }}Bstree searchBST(Bstree T, keytype key){ if (T == NULL || key == T->key) return T; if (key < T->key) return searchBST(T->lchild,key); if (key > T->key) return searchBST(T->rchild,key);} void main(){ int x, key; printf("请输入结点创建二叉排序树:\n"); scanf_s("%d", &key); CreatBST(root,key); printf("中序遍历:\n"); InOrder(root); printf("请输入:1 插入 2 查找\n"); scanf_s("%d", &x); if (x == 1) { printf("请输入要插入的数:"); scanf_s("%d", &key); while (key) { InsertBST(root, key); scanf_s("%d", &key); }InOrder(root); } if (x == 2) { printf("请输入要查找的数:"); scanf_s("%d\n", &key); if (searchBST(root, key)) printf("查找成功\n"); else printf("查找失败"); }}

  • 写回答

1条回答 默认 最新

  • 关注

    输入以0作为结束 

    #include<stdio.h>
    #include<malloc.h>
    #define _CRT_SECURE_NO_WARNINGS
    typedef int keytype;
    typedef struct node
    {
        keytype key;
        struct node* lchild;
        struct node* rchild;
    } BSTNode, * Bstree;
    Bstree root;
    BSTNode* f;
    void InsertBST(Bstree root, keytype key)
    {
        if(!root)
        {
            BSTNode* p;
            p = (BSTNode*)malloc(sizeof(BSTNode));
            p->key = key;
            p->lchild = p->rchild = NULL;
            root = p;
        }
        else if (key < root->key)
            InsertBST(root->lchild,key);
        else if (key > root->key)
            InsertBST(root->rchild, key);
    }
    Bstree CreatBST(Bstree root, keytype key)
    {
        Bstree T=NULL;
        while (key)
        {
            InsertBST(T, key);
            scanf("%d", &key);
        }
        return T;
    }
    void InOrder(Bstree root)
    {
        if (root)
        {
            InOrder(root->lchild);
            printf("%d",root->key);
            InOrder(root->rchild);
        }
    }
    Bstree searchBST(Bstree T, keytype key)
    {
        if (T == NULL || key == T->key)
            return T;
        if (key < T->key)
            return searchBST(T->lchild,key);
        if (key > T->key)
            return searchBST(T->rchild,key);
    }
    void main()
    {
        int x, key;
        printf("请输入结点创建二叉排序树:\n");
        scanf("%d", &key);
        CreatBST(root,key);
        printf("中序遍历:\n");
        InOrder(root);
        printf("请输入:1 插入 2 查找\n");
        scanf("%d", &x);
        if (x == 1)
        {
            printf("请输入要插入的数:");
            scanf("%d", &key);
            while (key)
            {
                InsertBST(root, key);
                scanf("%d", &key);
            }
            InOrder(root);
        }
        if (x == 2)
        {
            printf("请输入要查找的数:");
            scanf("%d\n", &key);
            if (searchBST(root, key))
                printf("查找成功\n");
            else
                printf("查找失败");
        }
    }
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿