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