0

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.

  • 1
    Please post a [mcve]. – melpomene Dec 09 '18 at 18:46
  • 1
    What makes you think that "when I call my delete node function, nothing gets deleted"? Is there any output demonstrating that? For which input does it not work? Is there any input for which it works (e.g. when you try to delete anything but the root node)? – Yunnosch Dec 09 '18 at 18:50
  • The `insert` function is wrong, when you create a new node you don't initialize the `left` and `right` pointers of the newsly created node. There are most likely more errors elsewhere. Also the `insert` function is very awkward and convoluted. – Jabberwocky Dec 09 '18 at 18:51
  • @Yunnosch I just provided input / output of the code :) sorry about that – Glenville Pecor Dec 09 '18 at 18:53
  • @jabberwocky Thank you. I will look into that – Glenville Pecor Dec 09 '18 at 18:54
  • Hoes does the input output you provided answer my questions? Especially the one about deleting something else than root node. – Yunnosch Dec 09 '18 at 18:54
  • Did you mean any output as in like printf in that function or did you mean output when I run the code @yunnosch – Glenville Pecor Dec 09 '18 at 18:56
  • You should post a self-contained [mcve]. That means we shouldn't have to enter input interactively. It also means there should be an obvious way to tell whether the code is working or not; i.e. you should tell us 1) what actually happens and 2) what you expected to happen instead. Also, https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – melpomene Dec 09 '18 at 18:58
  • @yunnosch if you mean putting a debugger line to show if the function actually went through, i did, i did not want to put it in the post because I have been scolded for having unneeded stuff in my code – Glenville Pecor Dec 09 '18 at 18:58
  • In your delete function, you have `if (name < root->name)` — that is not how you (meaningfully) compare strings in C! You could use `int rc = strcmp(name, root->name);` and then test for `rc < 0` (for name less than root name), or `rc > 0` (name greater than root name) or `rc == 0` (for name equal to root name — probably as an `else` clause). – Jonathan Leffler Dec 09 '18 at 18:59
  • @JonathanLeffler damn, thank you. Like i said im really bad at C syntax. Would it be correct to use strcmp? – Glenville Pecor Dec 09 '18 at 19:01
  • @JonathanLeffler I just did that and it made no difference (as in still not deleting) sadly :( – Glenville Pecor Dec 09 '18 at 19:10
  • There are other problems — but fixing the comparison will make it easier to delete nodes. Add printing to your delete code. Use a 'dump node' function that writes a tag (identifying string) and the node information (node pointer, name, left child, right child): `static void dump_node(const char *tag, const node_t *node) { printf("%s: %p [%s] L %p, R %p\n", tag, (void *)node, node->name, (void *)node->left, (void *)node->right); }`. Use it copiously to identify what you're working with. (See [C `#define` macro for debug printing](https://stackoverflow.com/questions/1644868/) for more info.) – Jonathan Leffler Dec 09 '18 at 19:13

0 Answers0