我有一个二叉搜索树,可以将用户输入放入其中,并在其中放入一个数组。我的问题是,当我调用删除节点函数时,什么也不会被删除。我在下面提供的是我的代码。我删除了所有不必要的代码。剩下的就是一个有效的输入功能和一个损坏的删除功能。我将delete函数和其他函数与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;
}
此处输出
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
在这里删除功能在做什么?抱歉,如果错误很明显。我是C语言的语法新手,但仍然渴望学习如何做到这一点!还要忽略进入阵列并花费ID小时并支付的不良数据,尽管其与数字相同,但不会有所不同。