Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   avl rotations...i lost my mommy!!! (http://www.programmingforums.org/showthread.php?t=13066)

bl00dninja Apr 26th, 2007 2:11 AM

avl rotations...i lost my mommy!!!
 
in my avl tree, i'm losing the connection to the parent when doing rotations.

for example: in a left roation on this tree:

.....6
..../ \
...5 7
........\
.........8
..........\
..........10

i'm getting back:

...6
../ \
.5 7

here is the code for insert, the call is made withthe head pointer and then a value. note a bunch of ****** right before leftRotation is called.
:

template <class T>
typename avl<T>::node * avl<T>::insert(node * p, T key)
{
        node * looker;

        //when we hit a NULL (bottom of tree
        if(p == NULL)
        {
                //make a new node and set its shit up
                node * temp = new node;
                temp->left = NULL;
                temp->right = NULL;
                temp->key = key;
                temp->skew = 0;
                return temp;
        }

        //if the new key is less than the one we're looking at
        //we go to the left
        if(key < p->key)
        {
                p->left = insert(p->left, key);
                --(p->skew);
                if(p->skew == 0) throw "";
/////////////////////////////////////////////////////////////////
                looker = p->left;
                if(p->skew == -2 && looker != NULL && looker->skew == -1)
                {
                        p = rightRotation(p);
                        throw "";
                }
////////////////////////////////////////////////////////////////////
                if(p->skew == -2 && looker != NULL && looker->skew == 1)
                {
                        p->left = leftRotation(p->left);
                        p = rightRotation(p);
                        throw "";
                }
        }
        //otherwise if it's greater than or equal to the current one
        //we go to the right
        else
        {
                p->right = insert(p->right, key);
                ++(p->skew);
                if(p->skew == 0) throw "";
/////////////////////////////////////////////////////////////
                looker = p->right;
                if(p->skew == 2 && looker->skew == 1)
                {//********************************************
//*****************************************************
//**************************************************
//************************************************
                        p = leftRotation(p);
                        throw "";
                }
////////////////////////////////////////////////////////////////////
                if(p->skew == 2 && looker != NULL && looker->skew == -1)
                {
                        p->right = rightRotation(p->right);
                        p = leftRotation(p);
                        throw "";
                }
        }
        //eventually return
        return p;
}


here is the code for leftRotation()
:

template <class T>
typename avl<T>::node * avl<T>::leftRotation(node * p)
{
        node * temp1, * temp2, * temp3;

        temp1 = p;
        temp2 = temp1->right;
        if(head == temp1) head = temp2;
        temp3 = temp2->left;
        temp2->left = temp1;
        temp1->right = temp3;
        return temp2;
}


ask for any other relevant code...head is node * to the root element, in this case 6.


All times are GMT -5. The time now is 2:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC