数据结构与算法 第10部分:平衡二叉树

1:平衡二叉树定义

typedef struct AVLNode{
   int data;
   int height;
   struct AVLNode *lchild;
   struct AVLNode *rchild;
}*AVLTree;

2:删除树

AVLTree Empty(AVLTree &T)//删除树
{
	if (nullptr == T)
	{
		return nullptr;
	}
    Empty(T->lchild);
    Empty(T->rchild);
    delete T;
    return nullptr;
}

3:计算高度

int Height(AVLTree T)//计算高度
{
	if (nullptr == T)
	{
		return 0;
	}
    return T->height;
}

4:更新高度

void updateHeight(AVLTree &T)
{
	if (nullptr == T)
	{
		return;
	}
    T->height = max(Height(T->lchild),Height(T->rchild))+1;
}

5:LL旋转

AVLTree LL_Rotation(AVLTree &T)//LL旋转
{
    AVLTree temp = T->lchild;
    T->lchild = temp->rchild;
    temp->rchild = T;
    updateHeight(T);//更新高度
    updateHeight(temp);
    return temp;
}

6:RR旋转

AVLTree RR_Rotation(AVLTree &T)//RR旋转
{
    AVLTree temp = T->rchild;
    T->rchild = temp->lchild;
    temp->lchild = T;
    updateHeight(T);//更新高度
    updateHeight(temp);
    return temp;
}

7:LR旋转

AVLTree LR_Rotation(AVLTree &T)//LR旋转
{
     T->lchild = RR_Rotation(T->lchild);
     return LL_Rotation(T);
}

8:RL旋转

AVLTree RL_Rotation(AVLTree &T)//RL旋转
{
    T->rchild = LL_Rotation(T->rchild);
    return RR_Rotation(T);
}

9:插入结点

AVLTree Insert(AVLTree &T,int x)
{
    if(nullptr == T) //如果为空,创建新结点
    {
        T = new AVLNode;
        T->lchild = T->rchild = NULL;
        T->data = x;
        T->height = 1;
        return T;
     }

	if (T->data == x)//查找成功,什么也不做,查找失败时才插入
	{
		return T;
	}

    if(x < T->data)//插入到左子树
    {
        T->lchild = Insert(T->lchild,x);//注意插入后饭后结果挂接到T->lchild
        if(Height(T->lchild)-Height(T->rchild) == 2)//插入后看是否平衡,如果不平衡显然是插入的那一边高度大
        {                                         //沿着高度大的那条路径判断
			//判断是LL还是LR,即插入的是lchild节点的lchild 还是rchild
			T = (x < T->lchild->data) ? LL_Rotation(T) : LR_Rotation(T);
        }
    }
    else//插入到右子树
    {
        T->rchild = Insert(T->rchild,x);
        if(Height(T->rchild)-Height(T->lchild) == 2)
        {
			T = (x > T->rchild->data) ? RR_Rotation(T) : RL_Rotation(T);
        }
    }
    updateHeight(T);
    return T;
}

10:平衡调整

AVLTree adjust(AVLTree &T)//删除结点后,需要判断是否还是平衡,如果不平衡,就要调整
{
    if (nullptr == T)
    {
        return nullptr;
    }

    if(Height(T->lchild)-Height(T->rchild) == 2)//沿着高度大的那条路径判断
    {
		T = (Height(T->lchild->lchild) >= Height(T->lchild->rchild)) ? LL_Rotation(T) : LR_Rotation(T);
    }
    if(Height(T->rchild)-Height(T->lchild)==2)//沿着高度大的那条路径判断
    {
		T = (Height(T->rchild->rchild) >= Height(T->rchild->lchild)) ? RR_Rotation(T) : RL_Rotation(T);
    }
    updateHeight(T);
    return T;
}

11:删除结点

AVLTree Delete(AVLTree &T,int x)
{
    if (nullptr == T)
    {
        return nullptr;
    }

    if(T->data == x)//如果找到删除节点
    {
        if(nullptr == T->rchild)//如果该节点的右孩子为NULL,那么直接删除
        {
            AVLTree temp = T;
            T = T->lchild;
            delete temp;
        }
        else//否则,将其右子树的最左孩子作为这个节点,并且递归删除这个节点的值
        {
           AVLTree temp;
           temp = T->rchild;
		   while (nullptr != temp->lchild)
		   {
			   temp = temp->lchild;
		   }          
           T->data = temp->data;
           T->rchild = Delete(T->rchild,T->data);
           updateHeight(T);
        }
        return T;
    }

    if (T->data > x)//调节删除节点后可能涉及的节点
    {
        T->lchild = Delete(T->lchild, x);
    }
    else if(T->data < x)
    {
        T->rchild = Delete(T->rchild, x);
    }     
    updateHeight(T);

    if (nullptr != T->lchild)
    {
        T->lchild = adjust(T->lchild);
    }     
    if (nullptr != T->rchild)
    {
        T->rchild = adjust(T->rchild);
    }
    if (nullptr != T)
    {
        T = adjust(T);
    }
    return T;
}

12:创建平衡二叉树

AVLTree CreateAVL(AVLTree &T)
{
    int n,x;
    cin >> n;
    for(int i = 0;i < n; i++)
    {
        cin>>x;
        T = Insert(T,x);
    }
    return T;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员的资料库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值