Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2007, 3:23 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 trees

for the anointed only:

why do i have problems with rotations? i understand the question is crap, but sometimes a hero will emerge that has the time to answer the question.

here is the avl class:

#include <iostream>
using namespace std;

template <class T>
class avl
{
private:
	struct node
	{
		node * left;
		node * right;
		T key;
		int skew;
	};

	node * head;
	node * insert(node *, T);
	void inorderPrint(node *);
	node * leftRotation(node *);
	node * rightRotation(node *);


public:
	
	avl();
	void insert(T);
	void inorderPrint();
	
};


template <class T>
avl<T>::avl()
{
	head = NULL;
}

template <class T>
void avl<T>::insert(T val)
{
	head = insert(head, val);
}

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

	temp1 = thead->right;
	temp2 = temp1->right;
	temp1->left = thead;
	thead->right = temp2;
	return temp1;
}

template <class T>
typename avl<T>::node * avl<T>::rightRotation(node * thead)
{
	node * temp1, * temp2;

	temp1 = thead->left;
	temp2 = temp1->left;
	temp1->right = thead;
	thead->left = temp2;
	return temp1;
}

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->skew == 1)
		//{
		//	rightRotation(p);
		//	//throw "";
		//}

		//looker = p->right;
		//if(p->skew == -2 && looker->skew == 1)
		//{
		//	rightRotation(p);
		//	//throw "";
		//}

		//looker = p->left;
		//if(p->skew == -2 && looker->skew == -1)
		//{
		//	leftRotation(looker);
		//	rightRotation(p);
		//	//throw "";
		//}

		//looker = p->right;
		//if(p->skew == -2 && looker->skew == -1)
		//{
		//	leftRotation(looker);
		//	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->left;
		//if(p->skew == 2 && looker->skew == 1)
		//{
		//	leftRotation(p);
		//	//throw "";
		//}

		/*looker = p->right;
		if(p->skew == 2 && looker->skew == 1)
		{
			leftRotation(p);
			throw "";
		}*/

		//looker = p->left;
		//if(p->skew == 2 && looker->skew == -1)
		//{
		//	rightRotation(looker);
		//	leftRotation(p);
		//	//throw "";
		//}

		//looker = p->right;
		//if(p->skew == 2 && looker->skew == -1)
		//{
		//	rightRotation(looker);
		//	leftRotation(p);
		//	//throw "";
		//}
	}
	//eventually return
	return p;
}

template <class T>
void avl<T>::inorderPrint()
{
	inorderPrint(head);
}

template <class T>
void avl<T>::inorderPrint(node * head)
{
	//as long as we point at a valid memory address...
	//we perform a preorder traversal
	if(head != NULL)
	{
		//go to the left
		//uncomment for pre-order
		cout<<head->key<<"  skew "<<head->skew<<endl;
		inorderPrint(head->left);
		//insert crap
		//uncomment for in-order
		//cout<<head->key<<"  skew "<<head->skew<<endl;
		//go to the right
		inorderPrint(head->right);
		//uncomment for post-order
		//cout<<head->key<<"  skew "<<head->skew<<endl;
	}
}

the insert works fine until we hit a skew of two. any glaring errors would be appreciated. much of the code is commented out, that is on purpose. the uncommented code is a binary search tree that holds correct skew values. is the rotation code wrong? is it in the wrong place? i do not assume anyone will answer this post unless they have time. whatever i can get is awesome.

much love,

--the bl00dninja
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Apr 19th, 2007, 10:57 AM   #2
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3 Seif is on a distinguished road
sorry, I don't really have time to go through your code at the mo, this sort of subject would prob take me a while knowing how much of a pain in the arse balancing trees are.

I do remember Narue having some good tutorials on binary trees, and has one on AVL trees. Sure you could find the code samples helpful to see whats wrong?

http://www.eternallyconfuzzled.com/jsw_home.aspx
Seif 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
Drawing Trees in with Swing hoffmandirt Java 1 Feb 22nd, 2007 3:36 PM
trees programmingnoob C++ 15 Nov 2nd, 2006 10:37 PM
Moving directory trees via .bat files Mansooj Bash / Shell Scripting 11 Jan 17th, 2006 2:45 AM
Flattening Trees Into Arrays Will Piovano C 2 Nov 11th, 2005 8:00 AM
Trees & Checkboxes quants Delphi 0 Apr 18th, 2005 10:18 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:34 PM.

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