Zia ur Rahman
Zia ur Rahman

Reputation: 1431

Insert method of AVL tree?

I am posting you the code for using an AVL tree that I developed. The method for insert, avlinsert method is listed below. I developed this code on paper and it's not tested but I hope this will work. The main problem I want to discuss is the balance factor of the nodes first look at the code. In this way the idea will become clear what I am trying to ask. So here is code:

    treeNode* avlinsert(treeNode* tree, int info)
    {
        treeNode* newNode=new treeNode;
        newNode->setinfo(info);
        newNode->setbalance(0);
        treeNode* p,*q;
        bool duplicate=false;
        p=q=tree;
        stack s; //I have made this stack already and now I am using it here.
        //Now the the while loop block will check for duplicate nodes, this block prevents the duplicate insertion.
        while (q!=NULL)
        {
            p=q;
            if (info < p -> getinfo())
                q=p->getleft();
            else if (info>p->getinfo())
                q=p->getright();
            else
            {
                duplicate=true;
                cout<<"Trying to insert duplicate";
                break;
            }
        }//while loop ended.
        //Now checking for duplicates.
        if (duplicate)
            return tree;
        p=q=tree;
        //Now below is the main block of while loop which calculates the balance factors of nodes and helps in inserting nodes at proper positions.
        while (q!=NULL)
        {
            p=q;
            if (info < p -> getinfo())
            {
                p->setbalance(p -> getbalance()+1);
                s.push(p);//pushing into stack
                q=p->getleft();
            }

            else if (info > p -> getinfo())
            {

                p->setbalance(p->getbalance()-1);
                q=p->getright();
            }

        }//while loop ended
        //Now the below code block will actually inserts nodes.
        if (info < p -> getinfo())
            p->setleft(newNode);
        else if (info > p -> getinfo())
            p->setright(newNode);
        //After this insertion we need to check the balance factor of the nodesand perform the approprite rotations.

        while (!s.isempty())
        {
            treeNode node;
            node=s.pop();
            int balance;
            balance=node.getbalance();
            if (balance==2)
            {
                s.Makeempty();   // We have found the node whoes balance factor is violating avl condition so we don't need other nodes in the stack, therefor we are making stack empty.
                treeNode* k1,*k3;
                k1=&node; //This is the node whoes balance factor is violating AVL condition.
                k3=&(k1 -> getleft()); //Root of the left subtree of k1.
                //Identifying the cases of insertion
                if (info < k3 -> getinfo())  //This is the case of insertion in left subtree of  left child of k1 here we need single right rotation.
                    root=Rightrotation(k1);  //Here root is the private data member.

                //Next case is the insertion in right subtree of left child of k1.
                if (info > k3 ->getinfo())
                    root=LeftRightrotation(k1);
            }//end of if statement.
        }//while loop ended

This is not the whole code but it's enough to show you what I am trying to do. You have seen in this code that I am setting the balance factor of nodes during insertion (second while loop block). OK, this is fine. But after this insertion I need to perform rotations. I have the code of rotations as well but the main problem is when the nodes are rotated then I need to reset the balance factors of the nodes in the rotation's code. This is the problem with me. How I can do it? And what would a code snippet be?

Upvotes: 0

Views: 2598

Answers (1)

Alex Ntousias
Alex Ntousias

Reputation: 9132

"...I need to reset the balance factors of the nodes in the rotation's code."

If you want to add something inside the rotation's code, then maybe you should also post the code of the rotation functions in order to get help.

Otherwise, if you just want to update each node's balance factors after the rotation, then this recursive function might help you (just call it and pass the root node of your tree):

int updateBalanceFactors(treeNode *node)
{
    if(node == NULL)
        return 0;
    node->setbalance(updateBalanceFactors(node->getright()) - updateBalanceFactors(node->getleft()));
    return ((node->getbalance() < 0 ? -node->getbalance() : node->getbalance()) + 1);
}

Also, I spotted some errors in your code, but I'm pretty sure you'll find them when you'll try to run your program. Some things to bear in mind:

  1. I'm not sure how your implementation of stack works, but I see that you push into the stack a pointer and then you pop an object:

    s.push(p);

    treenode node = s.pop();

  2. You push nodes into the stack only when you traverse the left subtree of your AVL tree, and not when you go right. Why is that?

  3. When you insert the newNode into the tree, remember to set newNode's left and right children to NULL (maybe you already do that in your constructor, but I'm not sure).

  4. Usually, in AVL trees, when the left subtree is higher than the right subtree of a node, then the balance factor of that node is negative. You might want to change that in your code. If you want to leave it like that, you 'll have to change my recursive function.

  5. Last but not least, you check to see if the balance factor is equal to 2 (if your tree needs rotation). Remember that the balance factor can take negative values as well (if the left subtree is higher than the right subtree).

Happy new year to everyone :-)

Upvotes: 1

Related Questions