C 如何从用户处读取数据,并在与我';将在BST中插入节点

C 如何从用户处读取数据,并在与我';将在BST中插入节点,c,data-structures,binary-search-tree,insertion,C,Data Structures,Binary Search Tree,Insertion,我有一个C编程课程评估。 他们要求创建一个函数来为BST插入新节点。在同一个函数中,我必须从用户(ID和薪水)中填充N个数据。我将要求用户输入他/她想要输入的数据量,并调用插入函数 void insert(binary_search_tree *t, node *n) { node *y = NULL; node *temp = t->root; while(temp != NULL) { y = temp; if

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

    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;
    }
我做了以下几件事,但完全错了:)

#包括
#包括
结构节点{
国际雇员ID;
浮动工资;
结构节点*左;
结构节点*右;
};
结构节点*插入(结构节点*根,int N){
int键;
浮动工资;
int i;
对于(i=0;iEmployeeID=Key;
根->薪资=薪资;
根->左=根->右=空;
}
else if(键EmployeeID)
根->左=插入(根->左,键);
其他的
根->右=插入(根->右,键);
}
}
作废预印本(结构节点*根){
if(root==NULL)
返回;
printf(“%d%.2f\n”,root->EmployeeID,root->Salary);
预印本(根->左);
预印本(根->右);
返回;
}
int main()
{
int x;
结构节点*root=NULL;
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
温度=根;
printf(“您希望输入多少名员工?”);
扫描频率(“%d”和“x”);
根=插入(根,x);
预印本(根);
返回0;
}

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

假设您将树根声明为
binary\u search\u tree*t=new\u binary\u search\u 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;
    }

这会将新节点作为叶节点插入BST。您可以将此片段作为参考并进行相应修改。

您必须循环并将任何新节点作为叶节点插入BST。BST的标准插入函数如下所示-

假设您将树根声明为
binary\u search\u tree*t=new\u binary\u search\u 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;
    }

这会将新节点作为叶节点插入BST。您可以将此代码段作为参考并进行相应修改。

根据请求,我被要求在插入函数本身中进行输入。这就来了-

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

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

根据请求,我被要求在插入函数本身中进行输入。这就来了-

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

节点结构与您的相同。但是,如果在结构节点定义中未使用
typedef
,请记住使用关键字
struct Node*
,而不是我在代码中使用的
Node*

请将问题中的代码作为文本发布。依我看,你不应该把输入和插入函数混在一起,但是你标记的那些行到底有什么不方便的地方呢?当然,我会这样做的。这很不方便,因为这是我第一次这样做。@如果您觉得答案对您有帮助,请勾选绿色勾号,将答案标记为已接受。请将问题中的代码作为文本发布。依我看,你不应该把输入和插入函数混在一起,但是你标记的那些行到底有什么不方便的地方呢?当然,我会这样做的。这很不方便,因为这是我第一次这样做。@如果您觉得答案对您有帮助,请勾选绿色勾号,将答案标记为已接受。让我澄清一下,他们要求这样做:编写一个函数,在二元搜索树中插入节点,并使用EmployeeID作为键。您将要求用户在此函数中填写数据字段(EmployeeID和Salary),而不是在main中!在main中,询问用户需要输入多少员工,并调用您在第b)部分中编写的函数以将节点插入到BST@it'sM执行此操作的标准方法是使用一个完全不同的insert函数,该函数只处理将节点插入BST的操作。您可以在main()中放置一个循环函数或为其编写一个不同的函数。在insert函数intself中包含输入的方法是幼稚的,不应该这样执行way@it'sM,为了进一步澄清,我的答案中的node->data in insert()函数中使用的数据应该替换为要用作键的变量。在您的情况下,在insert函数中将数据替换为EmployeeID。希望这有帮助!当我给我的教授写信时,他告诉我扫描基本案例中的数据,然后正常工作:)这是很难做到的。谢谢:)@it'sM我已经按照您的要求发布了另一个答案,但我仍然建议您通过分离插入和输入功能来保持代码模块化。让我澄清一下,他们要求这样做:编写一个函数,在二进制搜索树中插入节点,并使用EmployeeID作为键。您将要求用户在此函数中填写数据字段(EmployeeID和Salary),而不是在main中!在main中,询问用户需要输入多少员工,并调用您在第b)部分中编写的函数以将节点插入到BST@it'sM执行此操作的标准方法是使用不同的插入函数a