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 8842689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:01:25+00:00 2026-06-14T11:01:25+00:00

I am trying to implement a Binary Search Tree data structure in C, and

  • 0

I am trying to implement a Binary Search Tree data structure in C, and I have run into a bug. My pointer value changes for a reason I do not understand. (Please see bottom of post for strange output [Delete function and main functions clarify where output comes from] )
My Test function below:

int main(void)
{
    Bst *bst = ( Bst* ) calloc( 1, sizeof( Bst ) );
    BstInsert( bst, 7 );
    BstInsert( bst, 8 );
    BstInsert( bst, 2 );
    BstInsert( bst, 1 );
    BstTraverse( bst );
    BstRemove( bst, 7); 
    printf("=========================\n");
    printf("Root Key: %d\n", bst->key );
    printf("Left Key: %d\n", bst->left->key );
    printf("Right Key: %d\n", bst->right->key );
    printf("Location: %p\n", &bst);
    BstTraverse( bst );

    return 0;
}

My Delete Node function below:

void BstRemove( Bst *root, int key ){
    //Seems like recursive algorithm would need doubly linked bst implementation
    Bst *temp_node = BstFind( root, key );
    Bst *parent_node = BstGetParent( root, key );
    Bst *replacement_node = ( Bst* ) calloc( 1, sizeof( Bst ) );
    if ( temp_node->key == root->key )
    {   
        if (root->left) replacement_node = BstMax( root->left );
        else if ( root->right ) replacement_node = BstMin( root->right );
        else replacement_node = NULL;
    }
    else if ( temp_node->left )
    {
        replacement_node = BstMax( temp_node );
        Bst *parent_replacement_node = BstGetParent( root, replacement_node->key );
        parent_replacement_node->right = NULL;
    }
    else if ( temp_node->right )
    {
        replacement_node = BstMin( temp_node );
        Bst *parent_replacement_node = BstGetParent( root, replacement_node->key );
        parent_replacement_node->left = NULL;
    }
    else
        replacement_node = NULL;

    if ( parent_node && key < parent_node->key )
        parent_node->left = replacement_node;
    else if ( parent_node )
        parent_node->right = replacement_node;

    if ( replacement_node )
    {
        if ( root->left->key != replacement_node->key ) replacement_node->left = temp_node->left;
        if ( root->right->key != replacement_node->key ) replacement_node->right = temp_node->right;
    }
    root = replacement_node;
    printf("Root Key: %d\n", root->key );
    printf("Left Key: %d\n", root->left->key );
    printf("Right Key: %d\n", root->right->key );
    printf("Location: %p\n", &root);
    free(temp_node);
}

Output Below:

1
2
7
8
Root Key: 2
Left Key: 1
Right Key: 8
Location: 0x7fffc5cf52e8
=========================
Root Key: 0
Left Key: 2
Right Key: 8
Location: 0x7fffc5cf5338
1
2
8
0
8

The reason this confuses me so much is because I am using a pointer. I see no reason for the root->key value to change when it is 2 within the delete function, and once it is processed
root->key becomes 0. I am grateful for anybody who can point out my problem or help me in the right direction. You can see my current BST implementation at https://github.com/PuffNotes/C/blob/master/data_structures/binary_tree.c if necessary. I recently started trying to program everyday to gain some skills, and consider myself to be a beginner in C ( for reference ). Thank you.

  • 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-14T11:01:26+00:00Added an answer on June 14, 2026 at 11:01 am

    You’re not changing your root node pointer. It is passed by value to the remove function, and since it is certainly a viable target of the delete, it should be passed by address since it might change to a different node. Note: if I missed a root in there somewhere I apologize, but your compile should catch it).

    Note: I made no validation pass on whether this code is correct or even works; but the real hint something was wrong was the root = at the bottom, followed by the print-out, then the caller (main()) doing the same print-out and showing a different root pointer value.

    void BstRemove( Bst **root, int key )
    {
        //Seems like recursive algorithm would need doubly linked bst implementation
        Bst *temp_node = BstFind( *root, key );
        Bst *parent_node = BstGetParent( *root, key );
        Bst *replacement_node = ( Bst* ) calloc( 1, sizeof( Bst ) );
        if ( temp_node->key == (*root)->key )
        {   
            if ((*root)->left) replacement_node = BstMax( (*root)->left );
            else if ( (*root)->right ) replacement_node = BstMin( (*root)->right );
            else replacement_node = NULL;
        }
        else if ( temp_node->left )
        {
            replacement_node = BstMax( temp_node );
            Bst *parent_replacement_node = BstGetParent( (*root), replacement_node->key );
            parent_replacement_node->right = NULL;
        }
        else if ( temp_node->right )
        {
            replacement_node = BstMin( temp_node );
            Bst *parent_replacement_node = BstGetParent( (*root), replacement_node->key );
            parent_replacement_node->left = NULL;
        }
        else
            replacement_node = NULL;
    
        if ( parent_node && key < parent_node->key )
            parent_node->left = replacement_node;
        else if ( parent_node )
            parent_node->right = replacement_node;
    
        if ( replacement_node )
        {
            if ( (*root)->left->key != replacement_node->key ) replacement_node->left = temp_node->left;
            if ( (*root)->right->key != replacement_node->key ) replacement_node->right = temp_node->right;
        }
        *root = replacement_node;
    
        printf("Root Key: %d\n", (*root)->key );
        printf("Left Key: %d\n", (*root)->left->key );
        printf("Right Key: %d\n", (*root)->right->key );
        printf("Location: %p\n", root);
        free(temp_node);
    }
    

    Invoke it like this:

    BstRemove( &bst, 7); 
    

    And get used to passing the root in by address, as you will do plenty of it when you start writing balancing algorithms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to implement binary search tree but I think I have made
I've been trying to implement a delete function for a Binary Search Tree but
I am trying to implement binary search tree operations and got stuck at deletion.
Alright, so I've been trying to implement a simple binary search tree that uses
I am trying to implement the binary search in python and have written it
I am trying to implement a binary search tree. Here is the code I
I am trying to implement a custom binary search to run over a vector
I'm trying to implement a binary search tree class, but the compiler is throwing
I was trying to implement a simple Binary Search Tree for practice. I tried
I am trying to implement a binary search tree by reading lines from a

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.