【问题标题】:How can I read the data from the user and use them in the same function that I'll insert nodes in the BST我如何从用户那里读取数据并在我将在 BST 中插入节点的同一函数中使用它们
【发布时间】:2020-09-13 03:58:13
【问题描述】:

我有一个 C 编程课程评估。 他们要求创建一个函数来为 BST 插入新节点。在同一个函数中,我必须填写来自用户的 N 个数据(ID 和 Salary)。并且主要我会要求用户输入他/她想要输入的数据并调用插入函数。

我做了以下事情,100% 错了:)

#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 N) {
        int Key;
        float Salary;
        int i;
        for (i = 0; i < N; i++) {
            printF("Enter Employee ID: ");
            scanf_s("%d", &Key);
            printF("Enter Employee Salary: ");
            scanf_s("%f", &Salary);
            if (root = NULL) {
                root = (struct Node*)malloc(sizeof(struct Node));
                root->EmployeeID = Key;
                root->Salary = Salary;
                root->left = root->Right = NULL;
            }
            else if (Key < root->EmployeeID)
                root->left = insert(root->left, Key);
            else
                root->Right = insert(root->Right, Key);
        }
    }
    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 x;
        struct Node* root = NULL;
        struct Node*temp = (struct Node*)malloc(sizeof(struct Node));
        temp = root;
        printf("How many Employee would you like to enter? ");
        scanf_s("%d", &x);
        root= insert(root, x);
        PrePrint(root);
        return 0;
    }

【问题讨论】:

  • 请将问题中的代码作为文本发布。 IMO,您不应该将输入与插入功能混合在一起,但是您标记的那些行到底有什么不便?
  • 当然,我会这样做的。这很不方便,因为这是我第一次这样做。
  • @it'sM 如果您觉得答案对您有所帮助,请勾选绿色勾号,将答案标记为已接受。

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


【解决方案1】:

您必须在 BST 中循环并插入任何新节点作为离开。 BST 的标准插入函数如下 -

假设您将树根声明为binary_search_tree *t = new_binary_search_tree();

您可以将输入读取为 -

int EmployeeID; 
scanf("%d",&EmployeeID);
float salary;
scanf("%d",&salary);

然后你就可以新建一个节点了——

node* n = new_node(EmployeeID,salary); 

并将其传递给下面的插入函数。

    void insert(binary_search_tree *t, node *n) {
      node *y = NULL;
      node *temp = t->root;
      while(temp != NULL) {
        y = temp;
        if(n->data < temp->data)
          temp = temp->left;
        else
          temp = temp->right;
      }
      n->parent = y;

      if(y == NULL) //newly added node is root
        t->root = n;
      else if(n->data < y->data)
        y->left = n;
      else
        y->right = n;
    }

new_node 函数将是 -

node* new_node(int emp_id,float sal) {
  node *n = malloc(sizeof(node));
  n->EmployeeID= emp_id;
  n->salary= sal;
  n->left = NULL;
  n->right = NULL;
  n->parent = NULL;

  return n;
}

这会将新节点作为叶节点插入到您的 BST。您可以将此 sn-p 作为参考并进行相应修改。

【讨论】:

  • 让我澄清一下,他们要求这样做:编写一个在二叉搜索树中插入节点的函数,并使用 EmployeeID 作为键。您将要求用户填写此函数中的数据字段(EmployeeID 和 Salary),而不是在主函数中!主要是询问用户他/她需要输入多少员工并调用您在 b) 部分中编写的函数将节点插入 BST
  • @it'sM 执行此操作的标准方法是完全不同的插入函数,它只处理将节点插入 BST。您可以在 main() 函数中放置一个循环,也可以为它编写一个完全不同的函数。您在插入函数 intself 中包含输入的方法很幼稚,不应该那样执行
  • @it'sM 并进一步澄清,我的答案中 insert() 函数中 node->data 中使用的数据应替换为您要用作键的变量。在您的情况下,在插入函数中用 EmployeeID 替换数据。希望这可以帮助! ?
  • 当我发送给我的教授时,他告诉我扫描基本案例中的数据并正常工作:) 很难做到这一点。谢谢你:)
  • @it'sM 我已经按照您的要求发布了另一个答案,但我仍然建议您通过分离插入和输入功能来保持代码模块化。
【解决方案2】:

根据请求,我被要求在插入函数本身中输入。所以这里 -

void insert(binary_search_tree *t, int N) {//N is number of nodes to be added
      for(int i=0;i<N;i++){
          int EmployeeID; 
          scanf("%d",&EmployeeID);
          float salary;
          scanf("%d",&salary); 

          node* n = new_node(EmployeeID,salary); 
          node *y = NULL;
          node *temp = t->root;
          while(temp != NULL) {
            y = temp;
            if(n->data < temp->data)
              temp = temp->left;
            else
              temp = temp->right;
          }
          n->parent = y;

          if(y == NULL) //newly added node is root
            t->root = n;
          else if(n->data < y->data)
            y->left = n;
          else
            y->right = n;
      }
    }

这是我用来保留根节点的 binary_serach_tree 结构 -

typedef struct binary_search_tree {
  node *root;
}binary_search_tree;

节点结构和你的一样。但是如果你在结构节点定义中没有使用typedef,请记住使用关键字struct Node* 而不是我在代码中使用的Node *

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 2021-08-07
    • 2021-07-13
    • 1970-01-01
    相关资源
    最近更新 更多