Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2007, 1:11 AM   #1
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:20 AM.

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