Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8899615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:54:01+00:00 2026-06-15T00:54:01+00:00

I have found some code online for red black trees, and am trying to

  • 0

I have found some code online for red black trees, and am trying to implement it.

I do not want to use the assert function though which the original code has located here

I am getting a seg fault on the line n->color = child->color; just before the delete fix. After debugging I discovered that the child did not exist in this case, and so the reason for the assert statement in the original code. I decided to add what I thought was appropriate with the additional if clause around everything from where child is dealt with downward.

However, now the program does not actually delete, because if the child does not exist, it never makes it into the loop. After trial and error I still cannot find where to close the if clause in order to take the place of the assert statement properly.

Please let me know your ideas!

Here is my “translated” code without the assert, and using an if statement instead.

void delete_node(int key)
{
    node* child;
    node* n ;

   n = searchTree(key);

    if(n == NULL)return;

    if(n->left != NULL && n->right != NULL)
    {
        node* pred = n->left;
        while(pred->right != NULL)
            pred = pred->right;

        n->value = pred->value;
        n = pred;
    }
if(n->right != NULL || n->left != NULL){

    child = n->right == NULL ? n->left : n->right;
    if(n->color == 'b')
    {
        n->color = child->color;
        delete_fix1(n);
    }

    swap_nodes(n, child);
    if(n->parent == NULL && child != NULL)
        child->color = 'b';
    free(n);

}

}

Test data (Seg Fault occurs when attempting to delete 4):
i stand for insert (insert occurs flawlessly as far as I can tell)
d stands for delete

i 7
i 8
i 1
d 8
i 4
i 10
d 4
i 11
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:54:03+00:00Added an answer on June 15, 2026 at 12:54 am

    This:

    assert(n->left == NULL || n->right == NULL)
    

    Is nowhere near this:

    if (n->right != NULL || n->left != NULL)
    

    Recheck your translation. The assert states that one of them must be NULL. your if-expr evals true if either are not NULL. Your if-expr passes if both are non-null, where the assert would fail. Likewise, your if-expr fails if both are NULL, while the assert would pass.

    Don’t take shortcuts when doing this kinda of thing. First. keep the asserts regardless of your added checks. Second, until it is up and working, copy the assert clauses verbatim in your if (expr): or (!(expr)) for bailout conditions.

    verbatim check:

    assert(n->left == NULL || n->right == NULL)
    if (n->left == NULL || n->right == NULL)...
    

    bailout check:

    assert(n->left == NULL || n->right == NULL)
    if (!(n->left == NULL || n->right == NULL))
        bailout loud.
    

    EDIT Translation of linked code with integrated if-expr

    void rbtree_delete(rbtree t, void* key, compare_func compare)
    {
        node child;
        node n = lookup_node(t, key, compare);
        if (n == NULL) return;  /* Key not found, do nothing */
        if (n->left != NULL && n->right != NULL) {
            /* Copy key/value from predecessor and then delete it instead */
            node pred = maximum_node(n->left);
            n->key   = pred->key;
            n->value = pred->value;
            n = pred;
        }
    
        assert(n->left == NULL || n->right == NULL);
        if (n->left == NULL || n->right == NULL) // << note: SAME as above
        {
            child = n->right == NULL ? n->left  : n->right;
            if (node_color(n) == BLACK) {
                n->color = node_color(child);
                delete_case1(t, n);
            }
            replace_node(t, n, child);
            if (n->parent == NULL && child != NULL)
                child->color = BLACK;
            free(n);
        }
    
        verify_properties(t);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to adapt some code that I have found online that uses this
I am now trying to understand some code and I have found a pattern,
I'm trying to modify some code I found online to my needs. It is
Im trying to get some help with XML. I have found some good code
I have a series of values from some VB6 code I found online .
I have an error getting passed by reference to some code I found online.
I have found some code which recognize circles in particular image and I was
I have found some code snippet in the internet. But it is missing some
I have found some code on measuring execution time here http://www.dreamincode.net/forums/index.php?showtopic=24685 However, it does
I have adapted some code from a great article I found about circle drawing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.