我怎样才能从用户那里读取数据,并在BST中插入节点的同一个函数中使用它们呢?

问题描述 投票:0回答:1

我有一门C语言编程课程Assessment.他们要求创建一个函数来插入BST的新节点。他们要求创建一个函数来为BST插入新的节点,在同一个函数中,我必须填写用户的N个数据(ID和Salary)。在同一个函数中,我必须从用户那里填写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;
    }
c data-structures binary-search-tree insertion
1个回答
0
投票

你必须循环和插入任何新的节点作为离开在一个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中。你可以把这个代码段作为参考,并进行相应的修改。


0
投票

按照要求,我被要求在插入函数本身中进行输入。所以,这里是 -

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;
      }
    }

下面是我用来保存根节点的二进制_serach_tree结构--。

typedef struct binary_search_tree {
  node *root;
}binary_search_tree;

节点结构和你的一样。但记得要用关键字 struct Node* 而不是仅仅 Node * 如果你不使用我的代码,就像我在我的代码中使用的那样 typedef 在您的Node结构定义中。

© www.soinside.com 2019 - 2024. All rights reserved.