Esempio n. 1
0
File: avl.c Progetto: Yu-Yan/C
Tree Delete(int x, Tree T)
{
	if (T == NULL)
	{
		printf("%d not found, can not delete\n\n", x);
		return T;
	}
	if (x < T->element)
	{
		T->left = Delete(x, T->left);
		T->height = (Height(T->left) > Height(T->right) ? Height(T->left) : Height(T->right)) + 1;
		if (Height(T->left) - Height(T->right) == -2)
		{
			if (T->right->right)
				T = SingleRotateRight(T);
			else
				T = DoubleRotateRight(T);
		}
	}
	else if (x > T->element)
	{
		T->right = Delete(x, T->right); 
		T->height = (Height(T->left) > Height(T->right) ? Height(T->left) : Height(T->right)) + 1;
		if (Height(T->left) - Height(T->right) == 2)
		{
			if (T->left->left)
				T = SingleRotateLeft(T);
			else
				T = DoubleRotateLeft(T);
		}
	}
	else if (x == T->element)
	{
		Tree tmp;
		if (T->left && T->right)
		{
			tmp = T->right;
			while (tmp->left != NULL)
				tmp = tmp->left;
			T->element = tmp->element;
			T->right = Delete(tmp->element, T->right);
			T->height = (Height(T->left) > Height(T->right) ? Height(T->left) : Height(T->right)) + 1;
			if (Height(T->left) - Height(T->right) == 2)
				T = SingleRotateLeft(T);
		}
		else if (T->left == NULL)
		{
			tmp = T;
			T = T->right;
			free(tmp);
		}
		else if (T->right == NULL)
		{
			tmp = T;
			T = T->left;
			free(tmp);
		}
	}
	return T;
}
Esempio n. 2
0
File: avl.c Progetto: Yu-Yan/C
Tree Insert(int x, Tree T)
{
	if (T == NULL)
		T = CreateTree(x);
	else if (x < T->element)
	{
		T->left = Insert(x, T->left);
		if (Height(T->left) - Height(T->right) == 2)
		{
			if (x < T->left->element)
				T = SingleRotateLeft(T);
			else
				T = DoubleRotateLeft(T);
		}
	}
	else if (x > T->element)
	{
		T->right = Insert(x, T->right);
		if (Height(T->right) - Height(T->left) == 2)
		{
			if (x > T->right->element)
				T = SingleRotateRight(T);
			else
				T = DoubleRotateRight(T);
		}
	}
	T->height = (Height(T->left) > Height(T->right) ? Height(T->left) : Height(T->right)) + 1;
	return T;
}
Esempio n. 3
0
SearchTree Insert(int X,SearchTree T)//插入
{
	int i=0,j=0;
	if(T==null)//当找到最后那个可以插入的叶子、或叶子的上一级即可完成插入 
	{
		T=malloc(sizeof(struct TreeNode));
		if(T==null)
			printf("致命错误,内存溢出!!!");
		else
		{
			T->Element=X;
			T->Left=T->Right=null;
			T->Height = 0; 
		}			
	}
	else
	 if(X<T->Element)//递归实现 
	{
		T->Left = Insert(X,T->Left);
		if(Height(T->Left)-Height(T->Right)==2)
			if(X<T->Left->Element)
				T=SingleRotateLeft(T);
			else
				T=DoubleRotateLeft(T);
	}		
	else if(X>T->Element)//递归实现 
	{
		T->Right = Insert(X,T->Right);
	   // i=Height(T->Right);
		
	//	j=Height(T->Left);
		if(Height(T->Right)-Height(T->Left)==2)
			if(X>T->Right->Element)
				T=SingleRotateRight(T);
			else
				T=DoubleRotateRight(T);
	}	
	
	T->Height=Max(Height(T->Left),Height(T->Right))+1;
	return T;		 	
}
Esempio n. 4
0
File: avl.c Progetto: Yu-Yan/C
Tree DoubleRotateRight(Tree T)
{
	T->right = SingleRotateLeft(T->right);
	T = SingleRotateRight(T);
	return T;
}
Esempio n. 5
0
Position DoubleRotateLeft(Position K3)//左双螺旋 
{
	K3->Left= SingleRotateRight(K3->Left);
	
	return  SingleRotateLeft(K3);
}