Example #1
0
AVLTree AVL_Insertion(ElementType x, AVLTree T)
{
	if(!T)
	{
		T = (AVLTree)malloc(sizeof(AVLTreeNode));
		T->Data = x;
		T->Height = 0;
		T->Left = T->Right = NULL;
	}
	else if(x < T->Data)
	{
		T->Left = AVL_Insertion(x, T->Left);
		if(GetHeight(T->Left) - GetHeight(T->Right) == 2)
		{
			if(x < T->Left->Data)
				T = SingleLeftRotation(T);
			else
				T = DoubleLeftRightRotation(T);
		}
	}
	else if(x > T->Data)
	{
		T->Right = AVL_Insertion(x, T->Right);
		if(GetHeight(T->Right) - GetHeight(T->Left) == 2)
		{
			if(x > T->Right->Data)
				T = SingRightRotation(T);
			else
				T = DoubleRightLeftRotation(T);
		}
	}
	T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + 1;
	return T;
}
Example #2
0
avltree insert(int data, avltree T){
	if(!T){
		T=(avltree)malloc(sizeof(struct avltreeNode));
		T->data=data;
		T->height=0;
		T->left=T->right=NULL;
	//	printf("%d\n",T->data);
	}
	
	else if(data < T->data){
		T->left=insert(data,T->left);
		if(GetHeight(T->left)-GetHeight(T->right)==2)
			if(data < T->left->data)
				T=SingleLeftRotation(T);
			else
				T=DoubleLeftRightRotation(T);
	//	printf("%d\n",T->data);
	}
	else if(data > T->data){
		T->right=insert(data,T->right);
		if(GetHeight(T->left)-GetHeight(T->right)==-2)
			if(data > T->right->data)
				T=SingleRightRotation(T);
			else
				T=DoubleRightLeftRotation(T);
	//	printf("%d\n",T->data);
	}

	T->height=Max(GetHeight(T->left),GetHeight(T->right))+1;
//	printf("height:%d\n",T->height);
	return T;
}
Example #3
0
/*******************************************************************************
  Função que faz o reequilíbrio do nó pretendido da AVL.
  
  Function that balances the tree if necessary in each node that does not respect
  the equilibrium rule, after insertion and deletion operations.
*******************************************************************************/
static void Balance (PtAVLNode *proot)
{
	unsigned int LeftH, RightH;

	if (*proot == NULL) return;	/* subárvore vazia - empty tree */
	LeftH = AVLHeight ((*proot)->PtLeft);	/* altura subárvore esquerda - left subtree height */
	RightH = AVLHeight ((*proot)->PtRight);	/* altura subárvore direita - right subtree height */

	if (LeftH - RightH == 2)	/* subárvore esquerda desequilibrada? - left subtree unbalanced? */
	{
		LeftH = AVLHeight ((*proot)->PtLeft->PtLeft);
		RightH = AVLHeight ((*proot)->PtLeft->PtRight);
		if (LeftH >= RightH) SingleRightRotation (proot);
		else DoubleLeftRightRotation (proot);
	}
	else	if (RightH - LeftH == 2)	/* subárvore direita desequilibrada? - right subtree unbalanced? */
			{
				RightH = AVLHeight ((*proot)->PtRight->PtRight);
				LeftH = AVLHeight ((*proot)->PtRight->PtLeft);
				if (RightH >= LeftH) SingleLeftRotation (proot);
				else DoubleRightLeftRotation (proot);
			}
			else (*proot)->Height = LeftH > RightH ? LeftH + 1 : RightH + 1;
				/* atualizar a altura do nó - updationg the node height */
}
 AVLTree Insert( AVLTree T, ElementType X )
 { /* 将X插入AVL树T中,并且返回调整后的AVL树 */
     if ( !T ) { /* 若插入空树,则新建包含一个结点的树 */
         T = (AVLTree)malloc(sizeof(struct AVLNode));
         T->Data = X;
         T->Height = 0;
         T->Left = T->Right = NULL;
     } /* if (插入空树) 结束 */
  
     else if ( X < T->Data ) {
         /* 插入T的左子树 */
         T->Left = Insert( T->Left, X);
         /* 如果需要左旋 */
         if ( GetHeight(T->Left)-GetHeight(T->Right) == 2 )
             if ( X < T->Left->Data ) 
                T = SingleLeftRotation(T);      /* 左单旋 */
             else 
                T = DoubleLeftRightRotation(T); /* 左-右双旋 */
     } /* else if (插入左子树) 结束 */
      
     else if ( X > T->Data ) {
         /* 插入T的右子树 */
         T->Right = Insert( T->Right, X );
         /* 如果需要右旋 */
         if ( GetHeight(T->Left)-GetHeight(T->Right) == -2 )
             if ( X > T->Right->Data ) 
                T = SingleRightRotation(T);     /* 右单旋 */
             else 
                T = DoubleRightLeftRotation(T); /* 右-左双旋 */
     } /* else if (插入右子树) 结束 */
  
     /* else X == T->Data,无须插入 */
  
     /* 别忘了更新树高 */
     T->Height = Max( GetHeight(T->Left), GetHeight(T->Right) ) + 1;
      
     return T;
 }