1

I've a C programming course Assessment. They asked to create a function to insert new nodes for BST. In the same function I have to fill N data from the user (ID and Salary). and In the main I'll ask the user to enter how many data he/she want's to input and call the insertion function.

I did the following and it's 100% wrong :)

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

2 Answers2

0

You have to loop and insert any new node as leave in a BST. A standard insertion function to a BST would be as follows -

suppose you declare your tree root as binary_search_tree *t = new_binary_search_tree();

you can read your input as -

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

Then you can make a new node -

node* n = new_node(EmployeeID,salary); 

and pass this to below insert function.

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

The new_node function will be as -

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

This will insert the new node as a leaf node to your BST. You can take this snippet as a reference nd modify accordingly.

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • let me clarify it, They asked to do that: Write a function that inserts nodes in the binary search tree, and use EmployeeID as key. You will ask the user to fill the data fields (EmployeeID and Salary) in this function not in the main! In the main, ask the user how many employees he/she needs to enter and call the function you wrote in part b) to insert the nodes to the BST –  May 26 '20 at 11:26
  • @it'sM The standard way to do it is have a different insert function altogether that only deals with insertion of a node into the BST. You could either put a loop in main() function or write a different function for it altogether. Your method of including taking input in the insert function intself is naive and shouldn't be performed that way – Abhishek Bhagate May 26 '20 at 11:29
  • @it'sM and to clarify further, the data used in node->data in insert() function in my answer should be replaced with the variable that you want to use as key. In your case replace data with EmployeeID in the insert function. Hope this helps! – Abhishek Bhagate May 26 '20 at 11:31
  • When I sent to my professor, he told me to scan the data in the base case and work normally :) it's way way to difficult to do that . and Thank you:) –  May 26 '20 at 11:41
  • @it'sM I have posted another answer as you asked but I would still recommend you to keep your code modular by separating the insertion and input functionalities. – Abhishek Bhagate May 26 '20 at 11:53
0

As per the request, I was asked to take input in the insert function itself. So here goes -

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

Here is the binary_serach_tree struct that i used to keep the root node -

typedef struct binary_search_tree {
  node *root;
}binary_search_tree;

The node structure is same as yours. But remember to use the keyword struct Node* instead of just Node * as I used in my code if you are not using typedef in your struct Node definition.

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32