I have a Binary Search Tree that I can put user input into and put an array into it. My problem is when I call my delete node function, nothing gets deleted. What I provided below is my code. I deleted all unnecessary code. All that is left is a working input function and a broken delete function. I put the delete function and the other functions with it on top before main so it is easier to see. The Input is below the main
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
#define MAX 10
char* nameArray[] = { "Bob", "Troy", "Luna", "Glen", "Tat",
"Hut", "Tree", "Troy", "Steve", "Tucker", NULL };
char* number[] = { "610-11-1212", "508-123-1000", "617-555-1212",
"818-919-8100", "710-777-1170", "310-333-1300", "510-555-1001","333-310-3201", "445-440-0044", "220-210-2210", NULL };
char* ID[] = { "610-11-1212", "508-123-1000", "617-555-1212",
"818-919-8100", "710-777-1170", "310-333-1300", "510-555-1001","333-310-3201", "445-440-0044", "220-210-2210", NULL };
char* hours[] = { "610-11-1212", "508-123-1000", "617-555-1212",
"818-919-8100", "710-777-1170", "310-333-1300", "510-555-1001","333-310-3201", "445-440-0044", "220-210-2210", NULL };
char* pay[] = { "610-11-1212", "508-123-1000", "617-555-1212",
"818-919-8100", "710-777-1170", "310-333-1300", "510-555-1001","333-310-3201", "445-440-0044", "220-210-2210", NULL };
int flag;
typedef struct node
{
char* name;
char* phoneNum;
char* ID;
char* hours;
char* pay;
struct node * left;
struct node * right;
} node_t;
struct node * minValueNode(struct node* node)
{
struct node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
struct node* deleteNode(struct node* root, char* name)
{
if (root == NULL) return root;
if (name < root->name)
root->left = deleteNode(root->left, name);
else if (name > root->name)
root->right = deleteNode(root->right, name);
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->right);
root->name = temp->name;
root->right = deleteNode(root->right, temp->name);
}
return root;
}
int main()
{
node_t * test_list = malloc(sizeof(node_t));
test_list->name = "";
test_list->phoneNum = "";
test_list->ID = "";
test_list->hours = "";
test_list->pay = "";
test_list->left = NULL;
test_list->right = NULL;
for (int i = 0; i < MAX; i++) {
insert(test_list, nameArray[i], number[i], ID[i], hours[i], pay[i]);
}
print_tree_inorder(test_list);
int choice;
bool x = true;
while (x != false) {
printf("Please chose one of the following options: \n1: Print list\n5:Delete someone\n9:END\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
print_tree_inorder(test_list);
break;
case 5:
;//this has to be here so I can scan. Not errors
char* deleteName = malloc(51);
printf("Please enter in Name to delete entry: ");
scanf("%50s", deleteName);
deleteNode(test_list, deleteName);
printf("\n\nNEW LIST\n\n");
print_tree_inorder(test_list);
break;
case 9:
x = false;
break;
default:
printf("NOT FOUND");
break;
}
}
return 0;
}
Here is output
Bob 610-11-1212
Glen 818-919-8100
Hut 310-333-1300
Luna 617-555-1212
Steve 445-440-0044
Tat 710-777-1170
Tree 510-555-1001
Troy 508-123-1000
Troy 333-310-3201
Tucker 220-210-2210
Please chose one of the following options:
1: Print list
4:Input New Person
5:Delete someone
9:END
5
Please enter in Name to delete entry: Tucker
NEW LIST
Bob 610-11-1212
Glen 818-919-8100
Hut 310-333-1300
Luna 617-555-1212
Steve 445-440-0044
Tat 710-777-1170
Tree 510-555-1001
Troy 508-123-1000
Troy 333-310-3201
Tucker 220-210-2210
Please chose one of the following options:
1: Print list
4:Input New Person
5:Delete someone
9:END
1
Bob 610-11-1212
Glen 818-919-8100
Hut 310-333-1300
Luna 617-555-1212
Steve 445-440-0044
Tat 710-777-1170
Tree 510-555-1001
Troy 508-123-1000
Troy 333-310-3201
Tucker 220-210-2210
Please chose one of the following options:
1: Print list
4:Input New Person
5:Delete someone
9:END
What am I doing wrong here with the delete function? Sorry if the error is obvious. I am new to the syntax of C but still eager to learn how to do this! Also ignore the bad data going into the arrays for ID hours and pay, while its the same as the number, it shouldn't make a difference.