Root of AVL Tree

不多说,先粘题目:

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

    

    

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print ythe root of the resulting AVL tree in one line.

Sample Input 1:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88


解释题目:

这道题的意思说白了就是构建AVL(二叉平衡树),有四种旋转情况,左左,右右,左右,右左

这里粘一个大神的链接,可以很好的帮助理解AVL树

http://www.icourse163.org/learn/zju-93001#/learn/forumdetail?pid=533230(未经允许,擅自粘链接,是不是不太好呀,嘻嘻)


这道题也没什么好分析的啦,也就是构建一个ALV树,然后输出根结点就够了,直接上代码吧,其实这倒题的代码直接粘了好多老师的代码,(就是懒,就是任性,哈哈)

#include<iostream>
using namespace std;

typedef int ElemType;

typedef struct AVLTreeNode *AVLTree;
struct AVLTreeNode {
	ElemType data;
	AVLTree left;
	AVLTree right;
	int height;
};

int GetHeight(AVLTreeNode *tree)
{
	if (tree == NULL)
		return -1;                     //空树返回-1
	else
		return tree->height;
}

int Max(int a,int b)
{
	if (a > b)
		return a;
	else
		return b;
}
AVLTree SingleLeftRotation(AVLTree A)
{   /* 注意:A 必须有一个左子结点 B */
	/* 将 A 与 B 做如图 4.35 所示的左单旋,更新 A 与 B 的高度,返回新的根结点 B */
	AVLTree B = A->left;
	A->left = B->right;
	B->right = A;
	A->height = Max(GetHeight(A->left), GetHeight(A->right)) + 1;
	B->height = Max(GetHeight(B->left), A->height) + 1;
	return B;
}

AVLTree SingleRightRotation(AVLTree A)
{   /* 注意:A 必须有一个左子结点 B */
	/* 将 A 与 B 做如图 4.35 所示的右单旋,更新 A 与 B 的高度,返回新的根结点 B */
	AVLTree B = A->right;
	A->right = B->left;
	B->left = A;
	A->height = Max(GetHeight(A->right), GetHeight(A->left)) + 1;
	B->height = Max(GetHeight(B->right), A->height) + 1;
	return B;
}

AVLTree DoubleLeftRightRotation(AVLTree A) 
{	/* 注意:A 必须有一个左子结点 B,且 B 必须有一个右子结点 C */   
	/* 将 A、B 与 C 做如图 4.38 所示的两次单旋,返回新的根结点 C */          
	A->left = SingleRightRotation(A->left); /*将 B 与 C 做右单旋,C 被返回*/
	return SingleLeftRotation(A); /*将 A 与 C 做左单旋,C 被返回*/
}

AVLTree DoubleRightLeftRotation(AVLTree A)
{	/* 注意:A 必须有一个左子结点 B,且 B 必须有一个右子结点 C */
	/* 将 A、B 与 C 做如图 4.38 所示的两次单旋,返回新的根结点 C */
	A->right = SingleLeftRotation(A->right); /*将 B 与 C 做右单旋,C 被返回*/
	return SingleRightRotation(A); /*将 A 与 C 做左单旋,C 被返回*/
}

AVLTree AVL_Insertion(ElemType X, AVLTree T) 
{ 
	/* 将 X 插入 AVL 树 T 中,并且返回调整后的 AVL 树 */  
	if (!T) 
	{ 
		/* 若插入空树,则新建包含一个结点的树 */   
		T = (AVLTree)malloc(sizeof(struct AVLTreeNode));   
		T->data = X;   
		T->height = 0;   
		T->left = T->right = NULL; 
	} 
	/* if (插入空树) 结束 */
	else if (X < T->data) 
	{ 
		/* 插入 T 的左子树 */   
		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 (插入左子树) 结束 */       
	else if (X > T->data) 
	{ /* 插入 T 的右子树 */   
		T->right = AVL_Insertion(X, T->right);         
		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;
}

int main()
{
	int n;
	cin >> n;
	AVLTree root = NULL;

	int x;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		root = AVL_Insertion(x, root);
	}

	cout << root->data;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值