问题
I know how to search a node with a particular key into an AVL tree . But I want to know how to search in an AVL tree with a balance factor of -2
Here is the code that I have tried.
void searchForUnrequiredBalanceFactor(avlnode *n , avlnode *r)
{
avlnode *ptr ;
ptr = n ;
if (ptr==NULL)
return;
else
{
if (ptr ->balFact == -2)
{
r = ptr ;
return ;
}
else
{
searchForUnrequiredBalanceFactor(ptr->left,r);
searchForUnrequiredBalanceFactor(ptr->right,r);
}
}
}
But the code isn't working as required , whats the problem in it ??
Output :
balance factor of node 3 : 0
balance factor of node 5 : 0
balance factor of node 10 : 0
balance factor of node 30 : 0
balance factor of node 25 : -1
balance factor of node 20 : -2
balance factor of node 15 : -1
*searchForUnrequiredBalanceFactor called and printf*
node with data : 0 have balance factor : 0
回答1:
Assuming that avlnode *r
is an output parameter where you want to store the found node, you need to change this line:
r = ptr ;
to this:
*r = *ptr ;
来源:https://stackoverflow.com/questions/16705777/searching-a-node-with-balance-factor-of-2-in-an-avl-tree