Searching a node with balance factor of -2 in an AVL tree

大城市里の小女人 提交于 2019-12-11 14:15:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!