1

i'm having a problem in this code. its function is to go through a BST of strings and delete the nodes that satisfy some conditions (if in the word in the node there's an instance of the banned character, the node needs to be deleted). the problem is that it seg-faults in scrematurabanned2 because in the last recursive call it tries to access a node that is not existant and seg-faults. how can i fix this? sorry in advance if there's not a lot of code in this question but this is a huge program and these are the parts that i think are responsible for the problem

struct node {
    char *value;
    struct node *p_left;
    struct node *p_right;
};

int CmpStr(const char *a, const char *b)
{
    return (strcmp (a, b));
}

void inserisciInAlbero2(char* key, struct node** leaf, Compare cmp) {
    int res;
    if( *leaf == NULL ) {
        *leaf = (struct node*) malloc(sizeof( struct node ) );
        (*leaf)->value = malloc( strlen (key) +1 );
        strcpy ((*leaf)->value, key);
        (*leaf)->p_left = NULL;
        (*leaf)->p_right = NULL;
    } else {
        res = cmp (key, (*leaf)->value);
        if( res < 0)
            inserisciInAlbero2( key, &(*leaf)->p_left, cmp);
        else if( res > 0)
            inserisciInAlbero2( key, &(*leaf)->p_right, cmp);
    }
}


void scrematurabanned2(struct node *head, const char *bannedchar, int n){
    if( head != NULL ) {
        scrematurabanned2(head->p_left, bannedchar, n);
        if(strpbrk(head->value,bannedchar)!=NULL)
        {
            head = deleteNode(head, head->value);
        }
        scrematurabanned2(head->p_right, bannedchar, n);
    }
}

struct node * minValueNode(struct node* node) {
    struct node* current = node;
    while (current->p_left != NULL)
        current = current->p_left;
    return current;
}

struct node* deleteNode(struct node* root, char *key) {
    if (root == NULL)
        return root;
    int cmp_result = strcmp(key, root->value);
    if (cmp_result < 0)
        root->p_left= deleteNode(root->p_left, key);
    else if (cmp_result>0)
        root->p_right= deleteNode(root->p_right, key);
    else{
        if (root->p_left==NULL) {
            struct node *temp = root->p_right;
            free(root);
            return temp;
        } else if(root->p_right==NULL){
            struct node *temp = root->p_left;
            free(root);
            return temp;
        }
        struct node* temp = minValueNode(root->p_right);
        strcpy(root->value, temp->value);
    }
    return root;
}

int main() {
struct node *BST = NULL;
inserisciInAlbero2("2rj9R", &BST, (Compare) CmpStr);
inserisciInAlbero2("2rF9d", &BST, (Compare) CmpStr);
inserisciInAlbero2("2rq9R", &BST, (Compare) CmpStr);
inserisciInAlbero2("2ft9R", &BST, (Compare) CmpStr);
scrematurabanned2(BST, 'r', 5));
Boazzone66
  • 11
  • 4
  • Edit the question to provide a [mre]. – Eric Postpischil Aug 27 '22 at 10:39
  • As shown, the code contains quite a few logical errors, memory leaks, and possible buffer overflows. – Some programmer dude Aug 27 '22 at 10:45
  • This also seems like a good time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your applications. For example to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through the code statement by statement while monitoring variables and their values. I also recommend you use pencil and paper to "draw" all the operations you do. Draw arrows for links, boxes for nodes, and erase/redraw arrows as you modify links and pointers. – Some programmer dude Aug 27 '22 at 10:48
  • You are missing required headers, prototypes and also some functions are completele missing. Please read the link above regarding minimal examples. Hint: If you cannot simpley copy the code from the question, paste it into a file and compile it successfully, the example is not complete – Gerhardh Aug 27 '22 at 10:49
  • the debugging points me up until in the second recursive call of scrematurabanned2 where it seg faults and i don't know how to fix it – Boazzone66 Aug 27 '22 at 10:50
  • You forgot to delete anything in the case where you call `minValueNode`. – n. m. could be an AI Aug 27 '22 at 10:54
  • Insert 3 distinct nodes. Don't insert 4th because it is a duplicate. ... ??? `minValueNode()` doesn't bother to check if it has received a null pointer... This is a mess... "Copy data" instead of rearranging the nodes... Sorry... – Fe2O3 Aug 27 '22 at 11:08
  • sorry, i put in the wrong input, they are supposed to be all different, that input is an example. what do you mean with "copy data"? sorry but i'm still a beginner – Boazzone66 Aug 27 '22 at 11:17

0 Answers0