【问题标题】:How can I insert new nodes in Binary search tree?如何在二叉搜索树中插入新节点?
【发布时间】:2020-05-27 10:30:19
【问题描述】:

我试图插入 BST N 次,我必须要求用户在 insert 函数中输入数据。这是我的代码。我尝试使用预订方法打印树,但它只打印最后一个输入。我必须使用 ID 作为键,并使用 PreOrder 打印所有数据 Salary 和 ID。


    #include <stdio.h>
    #include <stdlib.h>
    struct Node{

        int EmployeeID;
        float Salary;
        struct Node* left;
        struct Node* right;
    };

    struct Node* insert(struct Node* root,int ID, float Salary){



        printf("Enter Employee ID: ");
        scanf("%d", &ID);

        printf("Enter Employee Salary: ");
        scanf("%f", &Salary);

        if(root == NULL){

            root = (struct Node*)malloc(sizeof(struct Node));
            root->EmployeeID = ID;
            root->Salary = Salary;
            root->left=root->right= NULL;
        }

        else if(ID < root->EmployeeID)
            root->left = insert(root->left, ID, Salary);
        else
            root->right = insert(root->right, ID, Salary);

    return root;

    };

    void PrePrint(struct Node* root){

        if(root == NULL)
            return;

        printf("%d   %.2f", root->EmployeeID, root->Salary);
        PrePrint(root->left);
        PrePrint(root->right);

        return;
    }
    int main()
    {
        int N, i;
        struct Node* root;
        int ID;
        int Sal;
        root = NULL;

        printf("How many Employee you would like to enter? \n");
        scanf("%d", &N);

        for(i=0; i<N; i++){

            root = insert(root, ID, Sal);
            printf("\n");

        }
            PrePrint(root);
        }

【问题讨论】:

    标签: c data-structures binary-search-tree


    【解决方案1】:

    删除插入函数中的第一条语句root = NULL;

    就插入功能而言,可以修改为:

    struct Node* insert(struct Node* root, int ID, float Salary){
            printf("Enter Employee ID: ");
            scanf("%d", &ID);
    
            printf("Enter Employee Salary: ");
            scanf("%f", &Salary);
    
            struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
            newNode->EmployeeID = ID;
            newNode->Salary = Salary;
            newNode->left = NULL;
            newNode->right = NULL;
    
            struct Node* cp_of_root = root;
            if(root==NULL)
                 return newNode;
            struct Node* parent = NULL;
            while(root!=NULL){
                 parent = root;
                 if (newNode->val > root->val)
                      root = root->right;
                 else
                      root = root->left;
            }
            if(newNode->val > parent->val)
                  parent->right = newNode;
            else
                  parent->left = newNode;
            return cp_of_root;
    
    }
    

    我希望这段代码能解决你的疑问。

    【讨论】:

    • 我从函数中删除了它,但它仍然不会打印:)
    • root-&gt;left=root-&gt;right=NULL更改为两个不同的语句root-&gt;left = NULL; root-&gt;right = NULL
    • 另外,不要在插入函数中询问 ID 和薪水,而是在主函数本身中询问。
    • 我很想问主要的 :) 但这就是他们要求我做的 :(
    • 这不是需要输入相同的值直到到达叶节点吗?在这种情况下,您可以做的是让用户输入值,然后不要递归调用函数,只需创建根的副本,然后遍历 BST 直到到达叶节点。在那里插入新节点,现在返回根(记住你创建了它的副本)
    【解决方案2】:

    要将 scanf 保留在 insert 函数中,您想制作一个标志以了解这是您第一次初始化变量(id 和薪水)还是您已经有值并且不想再次 scanf。

    #include <stdio.h>
    #include <stdlib.h>
    struct Node{
    
        int EmployeeID;
        float Salary;
        struct Node* left;
        struct Node* right;
    };
    
    struct Node* insert(struct Node* root,int ID, float Salary){
        if(ID==-1 && Salary==-1)
        {
            printf("Enter Employee ID: ");
            scanf("%d", &ID);
    
            printf("Enter Employee Salary: ");
            scanf("%f", &Salary);
    
        }
        if(root == NULL){
    
            root = (struct Node*)malloc(sizeof(struct Node));
            root->EmployeeID = ID;
            root->Salary = Salary;
            root->left=NULL;
            root->right= NULL;
        }
    
        else if(ID < root->EmployeeID)
            root->left = insert(root->left, ID, Salary);
        else
            root->right = insert(root->right, ID, Salary);
    
    return root;
    
    };
    
    void PrePrint(struct Node* root){
    
        if(root == NULL)
            return;
    
        printf("%d   %.2f\n", root->EmployeeID, root->Salary);
        PrePrint(root->left);
        PrePrint(root->right);
    
    return;
    }
    int main()
    {
        int N, i;
        struct Node* root;
        int ID;
        float Salary;
        root = NULL;
    
        printf("How many Employee you would like to enter? \n");
        scanf("%d", &N);
    
        for(i=0; i<N; i++){
            root = insert(root, ID=-1, Salary=-1);
            printf("\n");
    
        }
            PrePrint(root);
        }
    

    【讨论】:

    • 谢谢你这样做,但他们要求我在插入函数中填写 ID 和 Salary,而不是在 main 中:/
    • 我会解决的
    • 如果salary 和id 等于-1,这意味着到目前为止我没有从用户scanf 值,所以我进行scanf 否则我们使用变量id 和salary 中的值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    相关资源
    最近更新 更多