![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Organizing my file upload system and BBS.. im lost. | TCStyle | PHP | 0 | Jan 14th, 2007 2:12 PM |
| Checking source codes of image, audio and video files | on_auc | C++ | 3 | Feb 21st, 2005 8:36 PM |
| I'm new and Im lost with programming | GM Man | Visual Basic .NET | 4 | Feb 7th, 2005 1:42 PM |