I am making a Binary Search Tree that stores Multiple things in one instance, I have two errors That I ran into. The first one that happens in the function insert
use to work in than randomly it stopped, the second one is in print_tree_inorder
and I have no idea why that would be broken.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
char* nameArray[50] = {};
char* number[50] = {};
char* ID[50] = {};
char* hours[50] = {};
char* pPH[50] = {};
int flag;
typedef struct node
{
char* name;
char* phoneNum;
char* ID;
char* hours;
char* pPH;
struct node * left;
struct node * right;
} node_t;
void insert(node_t * tree, char* name, char* phoneNum, char* hours, char* pPH);
void print_tree_inorder(node_t * current);
int main()
{
char* n,p,id,h,pph;
int numberOfTimes = 0;
node_t * test_list = malloc(sizeof(node_t));
/* set values explicitly, alternative would be calloc() */
test_list->name = "";
test_list->phoneNum = "";
//test_list->ID = "";
test_list->hours = "";
test_list->pPH = "";
test_list->left = NULL;
test_list->right = NULL;
printf("Please enter in the amount of people you want: ");
scanf("%d",&numberOfTimes);
printf("\n");
for(int i = 0; i<numberOfTimes; i++){
printf("Please enter in name: ");
scanf("%s", &n);
nameArray[i] = n;
printf("Please enter in PhoneNumber: ");
scanf("%s", &p);
number[i] = p;
printf("Please enter in Hours: ");
scanf("%s", &h);
hours[i] = h;
//printf("\n");
printf("Please enter in pay per hour: ");
scanf("%s", &pph);
pPH[i] = pph;
insert(test_list,nameArray[i],number[i],hours[i], pPH[i] );
}
printf("\n In order\n");
print_tree_inorder(test_list);
}
void insert(node_t * tree, char* name, char* phoneNum, char* hours, char* pPH)
{
//unsigned int number = (unsigned int)ptr
if (tree->name == 0)
{
/* insert on current (empty) position */
tree->name = name;
tree->phoneNum = phoneNum;
//tree->ID = ID;
tree->hours = hours;
tree->pPH = pPH;
}
else
{
if ( strcmp(tree->name, name) > 0)
{
/* insert left */
if (tree->left != NULL)
{
insert(tree->left, name, phoneNum, hours, pPH);
}
else /* no left nodes*/
{
tree->left = malloc(sizeof(node_t));
/* set values explicitly, alternative would be calloc() */
tree->left->name = name;
tree->left->phoneNum = phoneNum;
tree->left->hours = hours;
tree->left->pPH = pPH;
tree->left->left = NULL;
tree->left->right = NULL;
}
}
else /*add node to right */
{
if ( strcmp(tree->name, name) <= 0)
{
/* insert right */
if (tree->right != NULL)
{
insert(tree->right, name, phoneNum, hours, pPH);
}
else
{
tree->right = malloc(sizeof(node_t));
/* set values explicitly, alternative would be calloc() */
tree->right->name = name;
tree->right->phoneNum = phoneNum;
tree->right->hours = hours;
tree->right->pPH = pPH;
tree->right->left = NULL;
tree->right->right = NULL;
}
}
}
}
}
void print_tree_inorder(node_t * current) {
if (current == NULL) return;
print_tree_inorder(current->left);
printf(" %s %s %s %s\n", current->name, current->phoneNum,current->hours, current->pPH);
print_tree_inorder(current->right);
}