0

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
Subbu
  • 2,063
  • 4
  • 29
  • 42
  • There are at least a few pieces of information that I feel you should provide in your question: What is the actual result you're getting? Have you computed the balance factors for the nodes in the tree? Is there any node in the tree that has a balance factor of -2? – Michael May 23 '13 at 04:49
  • yes.. I have computed for balanced factors for nodes and there is also a node with balance factor -2 . – Subbu May 23 '13 at 05:03

1 Answers1

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 ;
Michael
  • 57,169
  • 9
  • 80
  • 125
  • By the statement `*r = *ptr` , you are not getting the exact location of the node , you are just getting the key of the node containing balance factor -2 . So , why not `r = ptr` – Subbu May 23 '13 at 06:59
  • With `*r = *ptr` you're copying the entire node from the address pointed to by `ptr` to the address pointed to by `r`. If you do `r = ptr` then you're only making that change visible to `searchForUnrequiredBalanceFactor`, not to the calling function (the scope of `r` is `searchForUnrequiredBalanceFactor`, while the "scope" of the memory pointed to by `r` might be the entire program depending on where you allocate/free that chunk of memory). – Michael May 23 '13 at 07:06
  • ok , but the `r` is passed by call by reference . So when i make it point to `ptr` , it should also work . – Subbu May 23 '13 at 09:58
  • No. Changes made to `r` will be visible only to `searchForUnrequiredBalanceFactor`. Changes made to `*r` on the other hand will be visible anywhere where the memory `r` points to is visible. – Michael May 23 '13 at 10:02
  • Again, you're modifying a pointer whose lifetime ends when the function returns. You're _not_ modifying the memory that the pointer _points to_. The example you linked to also has the problem that you seem to be trying to return the address of a local variable (which won't exist anymore after the function returns). – Michael May 23 '13 at 10:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30479/discussion-between-codejack-and-michael) – Subbu May 23 '13 at 11:00