Problems with Binary Tree Search Tree Removal in C

Asked

Viewed 54 times

1

I’d like your help with a binary search tree code. I already have all the code, but I’m having trouble with the removal of a knot with a child. I’ve looked at the analysis of that same code that I based myself on and what you’re talking about, it should work, but it doesn’t work here. The problem is as follows: assuming that I want to remove the node that has the number 9 and has only one child to its right of value 10 and that its father is 8. The representation would look something like this: 8->9->10. To remove 9, we have to make 8 point to 10 and free the memory of 9. However, I tried to rewrite the code a few times and had two different situations: 1ª) Nothing happens. When printing the tree after the removal of the 9, it continues there: 8->9->10; 2nd) The 9 is removed but two numbers appear, 0 and 1, in its place. Thus: 8->1->0->10.

For convenience, I consider that "aux = *root", the root being the root of the tree itself and the "ant" receives the value of aux in the position we want to remove, in this case, the 9.

Here is the version of the code for the first situation:

                aux = aux->dir;
                aux->dir = NULL;

I know that in this case the 9 will continue to occupy space in the memory, but I believe that despite this it should no longer appear when printing the tree.

Here is the version of the code for the second situation:

                aux = aux->dir;
                aux->dir = NULL;
                free(ant); 
                ant = NULL;

I don’t know if this affects anything, but I write the code in Sublime and compile using GCC on the Ubuntu 20.04 command line

If you find it necessary, put the rest of the code. But I believe it is working in the other parts of it.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.