Ejemplo n.º 1
0
Archivo: avl.c Proyecto: 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;
}
Ejemplo n.º 2
0
Archivo: avl.c Proyecto: 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;
}
Ejemplo n.º 3
0
/*
 *  向AVL树插入可以通过如同它是未平衡的二叉查找树一样把给定的值插入树中,
 *  接着自底向上向根节点折回,于在插入期间成为不平衡的所有节点上进行旋转来完成。
 *  因为折回到根节点的路途上最多有1.5乘log n个节点,而每次AVL旋转都耗费恒定的时间,
 *  插入处理在整体上耗费O(log n) 时间。
 */
AvlTree  Insert(ElementType x,AvlTree T)
{
    //如果T不存在,则创建一个节点树
    if(T == NULL)
    {
        T = (AvlTree)malloc(sizeof(struct AvlNode));
        {
            T->data = x;
            T->Height = 0;
            T->left = T->right = NULL;
        }
    }
    // 如果要插入的元素小于当前元素
    else if(x < T->data)
    {
        //递归插入
        T->left=Insert(x,T->left);
        //插入元素之后,若 T 的左子树比右子树高度 之差是 2,即不满足 AVL平衡特性,需要调整
        if(Height(T->left) - Height(T->right) == 2)
        {
            //把x插入到了T->left的左侧,只需 左侧单旋转
            if(x < T->left->data)
                T = SingleRotateWithLeft(T);       // LL旋转
            else
                // x 插入到了T->left的右侧,需要左侧双旋转
                T =  DoubleRotateLeft(T);          // LR旋转
        }
    }
    // 如果要插入的元素大于当前元素
    else if(x > T->data)
    {
        T->right=Insert(x,T->right);
        if(Height(T->right) - Height(T->left) == 2)
        {
            if(x > T->right->data)
                T = SingleRotateWithRight(T);     //RR 旋转
            else
                T =  DoubleRotateRight(T);        //RL旋转
        }
    }
    T->Height=Max(Height(T->left),Height(T->right)) + 1;
    return T;
}
Ejemplo n.º 4
0
/*
 *  对单个节点进行的AVL调整
 */
AvlTree Rotate(AvlTree T)
{

    if(Height(T->left) - Height(T->right) == 2)
    {
        if(Height(T->left->left) >= Height(T->left->right))
            T = SingleRotateWithLeft(T);  // LL旋转
        else
            T =  DoubleRotateLeft(T);     // LR旋转
    }
    if(Height(T->right) - Height(T->left) == 2)
    {
        if(Height(T->right->right) >= Height(T->right->left))
            T = SingleRotateWithRight(T);  // RR旋转
        else
            T =  DoubleRotateRight(T);     // RL旋转
    }
    return T;
}
Ejemplo n.º 5
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;		 	
}