In the following code snippet p is a node in a binary search tree, if the left child of p is not null I want to change p to point to its left child, but java is pass-by-value, when the function returns the structure of the tree remains unchanged.
void remove(BSTNode p)
{
if(p.ch[0]==null)
p=p.ch[0];
}
Actually I want to implement something like the following C++ code:
void remove(BSTNode* &p)
{
if(p->ch[0]==NULL)
p=p->ch[0];
}
For other reasons I don’t want to use the following way to return p.ch[0] and set p after every call to remove.
BSTNode remove(BSTNode p)
{
if(p.ch[0]==null)
return p.ch[0];
}
How can it be done?
You can save a pointer to the nodes parent ant then change it using that reference