Exemplo n.º 1
0
void RLrotation(struct Avlnode *t)
{
	struct Avlnode *Aptr,*pptr;
	pptr=t;
	Aptr=pptr->lchild;
	LLrotation(Aptr);
	RRrotation(pptr);
}
Exemplo n.º 2
0
	//##############################################################################
	//# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	//##############################################################################
	xAvlMemTrace::Node* xAvlMemTrace::insert(void* compKey,const char* file, int line,size_t size,Node* root)
	{
		if(root == NULL) 
			root = new Node(file,line,size,compKey);
		else
		{
			//insert in left child
			if(compKey < root->key) 
			{
				//recursive pass
				root->left = insert(compKey,file,line,size,root->left);
				
				//if after insertion the tree is not balanced
				if((getHeightOf(root->left) - getHeightOf(root->right)) == 2)
				{
					if(compKey < root->left->key)
						root = LLrotation(root); //LL
					else
						root = LRrotation(root); //LR
				}
			}
			//insert in right child
			else if(compKey > root->key) 
			{
				//recursive pass
				root->right = insert(compKey,file,line,size,root->right);
				
				//if after insertion the tree is not balanced
				if((getHeightOf(root->right) - getHeightOf(root->left)) == 2)
				{
					if(compKey > root->right->key)
						root = RRrotation(root); //RR
					else
						root = RLrotation(root); //RL
				}
			}
			//if duplicate key
			else
			{	
				//substitute the value of current root
				root->file = file;
				root->line = line;
				root->size = size;
				root->key = compKey;
			}	
		}
		root->height = MAX(getHeightOf(root->left),getHeightOf(root->right)) + 1;
		
		return root;
	}
Exemplo n.º 3
0
	//##############################################################################
	//# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	//##############################################################################
	xAvlMemTrace::Node* xAvlMemTrace::remove(void* key,Node* root)
	{
		if(root != NULL)
		{
			//begin search for the element to delete
			if(key < root->key)
			{
				root->left = remove(key,root->left);
			}
			else if(key > root->key)
			{
				root->right = remove(key,root->right);
			}
			else//we found it
			{
				//Case 1: node is a leaf, just delete it
				if(root->left == NULL && root->right == NULL)
				{
					delete root;
					root = NULL;
					return root;
				}
				//case 2.1 node with only left child
				else if(root->left != NULL && root->right == NULL)
				{
					Node* tmp = root->left;
					delete root;
					
					root = tmp;
				}
				//case 2.2 node with only right child
				else if(root->left == NULL && root->right != NULL)
				{
					Node* tmp = root->right;
					delete root;
					
					root = tmp;
				}
				//case 3 node with two child
				else
				{
					//return to root node and delete the predecessor
					nodeToDeleteWith2Child = root;
					if(root != m_pRootNode)
						return root;
				}
			}
			
			if((root == m_pRootNode) && nodeToDeleteWith2Child != NULL)
			{
				//if we are here we are in the root node and we should delete
				//the predecessor of the node
				
				//find predecessor
				Node* pred = predecessor(nodeToDeleteWith2Child->key);
				Node* temp = nodeToDeleteWith2Child;
				
				//copy predecessor data at place of root
				nodeToDeleteWith2Child->file = pred->file;
				nodeToDeleteWith2Child->line = pred->line;
				nodeToDeleteWith2Child->size = pred->size;
				void* tmpKey = pred->key;
						
				//delete predecessor
				nodeToDeleteWith2Child = NULL;
				m_pRootNode = remove(pred->key,m_pRootNode);	
				temp->key = tmpKey;
				return m_pRootNode;
			}
	
			//now balance if necessary
			int balFact = getHeightOf(root->left) - getHeightOf(root->right);
			//right imbalanced
			if(balFact == -2)
			{
				//root->right MUST be != NULL
				if(getHeightOf(root->right->right) > getHeightOf(root->right->left))
					root = RRrotation(root); //RR
				else
					root = RLrotation(root); //RL
			}
			//left imbalanced
			else if(balFact == 2)
			{
				//root->left MUST be != NULL
				if(getHeightOf(root->left->left) > getHeightOf(root->left->right))
					root = LLrotation(root); //LL
				else
					root = LRrotation(root); //LR
			}
			//else is balanced
			
			//now update height
			root->height = MAX(getHeightOf(root->left),getHeightOf(root->right)) + 1;
		}
		else //if key not found
		{
			//WHAT TO DO?
		}
		
		return root;
	}
Exemplo n.º 4
0
	//##############################################################################
	//# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	//##############################################################################
	xAvlMemTrace::Node* xAvlMemTrace::RLrotation(Node* root)
	{
		root->right = LLrotation(root->right);
		return RRrotation(root);
	}
Exemplo n.º 5
0
	//##############################################################################
	//# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	//##############################################################################
	xAvlMemTrace::Node* xAvlMemTrace::LRrotation(Node* root)
	{
		root->left = RRrotation(root->left);
		return LLrotation(root);
	}
Exemplo n.º 6
0
struct Avlnode *AddNode(struct Avlnode **root,struct Avlnode *Nptr)
{
struct Avlnode *ptr,*lptr,*rptr;
int h1,h2,bf;
ptr=*root;
	if(ptr==NULL)
	{
		ptr=Nptr;
		Nptr->lchild=NULL;
		Nptr->rchild=NULL;
		ptr->h=1;
		return ptr;
	}
	else
	{
		if(Nptr->data<ptr->data)
		{
			AddNode(&ptr->lchild,Nptr);
			lptr=ptr->lchild;
			rptr=ptr->rchild;
			if(rptr==NULL)
			{
				h2=0;
			}
			else
			{
				h2=rptr->h;
				h1=lptr->h;
				bf=h1-h2;
				if(bf==2)
				{
					if(Nptr->data<lptr->data)
					{
						LLrotation(ptr);
					}
					else
					{
						LRrotation(ptr);
					}
					ptr->h=findheight(ptr);
				}
			}
		}
		else
		{
			if(Nptr->data>ptr->data)
			{
				AddNode(&ptr->rchild,Nptr);
				rptr=ptr->rchild;
				lptr=ptr->lchild;
				if(lptr==NULL)
				{
					h1=0;
				}
				else
				{
					h2=rptr->h;
					h1=lptr->h;
					bf=h1-h2;
					if(bf==-2)
					{
					if(Nptr->data>rptr->data)
					{
						RRrotation(ptr);
					}
					else
					{
						RLrotation(ptr);
					}
					ptr->h=findheight(ptr);
				}
				
			}
		}
	}
}
return ptr;
}