04-树5 Root of AVL Tree(25 分)

题该参照主要点击打开链接

代码内,singleLeft针对的是题目中如图一的情况,singleRight针对的是题目中Figure2的情况

DoubleLeft针对的是题目中Figure4的情况,可以转化为先针对61结点进行singleRight,然后变成了SingleLeft的情况

DoubleRight针对的是题目中的Figure3的情况,同样可以转化为先针对88结点进行singleLeft,然后就变成了SingleRight的情况。

#include <iostream>
#include <string>

typedef struct AVLTreeNode {
	int Data;
	AVLTreeNode *Left;
	AVLTreeNode *Right;
	int Height;
}nAVLTree, *pAVLTree;

pAVLTree AVLInsertion(int nodeValue, pAVLTree pAvl);
int GetALVHeight(pAVLTree);
pAVLTree SingleLeftRotation(pAVLTree);
pAVLTree DoubleLeftRotation(pAVLTree);
pAVLTree SingleRightRotation(pAVLTree);
pAVLTree DoubleRightRotation(pAVLTree);
int Max(int hight1, int hight2);
using namespace std;

int main()
{
	int num;
	int i;
	int value;
	pAVLTree pAvl;
	pAvl = NULL;
	cin >> num;
	for (i = 0; i < num; i++)
	{
		cin >> value;
		pAvl = AVLInsertion(value, pAvl);
	}
	cout << pAvl->Data;
}

pAVLTree AVLInsertion(int nodeValue, pAVLTree pAvl)
{
	if (pAvl == NULL)
	{
		pAvl = (pAVLTree)malloc(sizeof(nAVLTree));
		pAvl->Left = pAvl->Right = NULL;
		pAvl->Data = nodeValue;
		pAvl->Height = 0;
	}
	else if (nodeValue < pAvl->Data)
	{
		pAvl->Left = AVLInsertion(nodeValue, pAvl->Left);
		if (GetALVHeight(pAvl->Left) - GetALVHeight(pAvl->Right) == 2)
		{
			if (nodeValue < pAvl->Left->Data)
			{
				pAvl = SingleLeftRotation(pAvl);
			}
			else
			{
				pAvl = DoubleLeftRotation(pAvl);
			}
		}
	}
	else if (nodeValue > pAvl->Data)
	{
		pAvl->Right = AVLInsertion(nodeValue, pAvl->Right);
		if (GetALVHeight(pAvl->Right) - GetALVHeight(pAvl->Left) == 2)
		{
			if (nodeValue > pAvl->Right->Data)
			{
				pAvl = SingleRightRotation(pAvl);
			}
			else
			{
				pAvl = DoubleRightRotation(pAvl);
			}
		}
	}
	pAvl->Height = Max(GetALVHeight(pAvl->Left), GetALVHeight(pAvl->Right)) + 1;
	return pAvl;
}

pAVLTree SingleLeftRotation(pAVLTree A)
{
	pAVLTree B = A->Left;
	A->Left = B->Right;
	B->Right = A;
	A->Height = Max(GetALVHeight(A->Left), GetALVHeight(A->Right)) + 1;
	B->Height = Max(GetALVHeight(B->Left), A->Height) + 1;
	return B;
}

pAVLTree DoubleLeftRotation(pAVLTree A)
{
	A->Left = SingleRightRotation(A->Left);
	return SingleLeftRotation(A);
}

pAVLTree SingleRightRotation(pAVLTree A)
{
	pAVLTree B = A->Right;
	A->Right = B->Left;
	B->Left = A;
	A->Height = Max(GetALVHeight(A->Left), GetALVHeight(A->Right)) + 1;
	B->Height = Max(GetALVHeight(B->Right), A->Height) + 1;
	return B;
}

pAVLTree DoubleRightRotation(pAVLTree A)
{
	A->Right = SingleLeftRotation(A->Right);
	return SingleRightRotation(A);
}

int GetALVHeight(pAVLTree pAvl)
{
	if (pAvl == NULL)
	{
		return 0;
	}
	else
	{
		return pAvl->Height;
	}
}

int Max(int hight1, int hight2)
{
	return hight1 > hight2 ? hight1 : hight2;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值