Home > Back-end >  Delete C binary tree subtree code. Is where there is a problem? crying
Delete C binary tree subtree code. Is where there is a problem? crying

Time:11-29

Save the children, why delete binary tree subtree function code cannot achieve??
Delet () function, the input to create the root node of the binary tree, using recursive find child is to be deleted after the root node, the other is equal to NULL, the equivalent of cutting (values are kept in memory), and then return to end the current layer recursion, but in the main function call, why delete after binary tree traversal results with no delete is the same as before? Where is there a problem?
If only add in the main function, b - & gt; Left - & gt; Left=NULL; Manual assignment is NULL, then traverse, can remove succeed, what is wrong?

CodePudding user response:

The delete function of t - & gt; The name there, need a loop to find d here, t - directly & gt; The name is a little problem,

CodePudding user response:

Logic has a problem, recursion, should be divided into two functions, a function is a recursive search, find conforms to the condition node, a function is recursive delete,

CodePudding user response:

See
Void del (int * x)
{
X=NULL;
}
Int * p=new int (10);
Del (p);
After this p didn't become NULL??????

CodePudding user response:

And you just put the variable assignment is NULL, it points to the memory is still being used, so it is a mistake of
Should
Void del (int * & amp; X)
{
Delete the x;
X=NULL;
}
Int * p=new int (10);
Del (p);

CodePudding user response:

If written in c
About the following
Void del (int * * x)
{
Free (* x);
* x=NULL;
}
Int * p=(int *) malloc...
Del (& amp; P);
  • Related