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.
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
rootin 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.Invoke it like this:
And get used to passing the root in by address, as you will do plenty of it when you start writing balancing algorithms.