04-树5 Root of AVL Tree (25 point(s))

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 the 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



思路如下:
二叉搜索树查找键值的时间复杂度是通过查找过程中的比较次数来衡量的,比较是从根节点到叶节点的路径进行的,取决于树的深度。同时,搜索树节点不同插入次序,将导致不同的深度和平均查找长度ASL(Average Search Length)。在二叉搜索树的特例——”完美二叉树(complete Binary Tree)”下,为O(logN);当二叉树为单枝树时,为线性O(N)。
ASL = Σ(ElementCnt(i) * Depth(i))
其中ElementCnt(i)为第i层的键值个数,Depth(i)为第i层对应的深度
为了使得树的深/高度尽量低,引入了平衡二叉树(AVL)的概念。
此题的考察的是AVL树的调整,涉及到键值的插入,插入后又涉及到保持AVL特性的问题,会有LL,LR,RL,RR型不平衡,所以相应地细化考察了单旋调整(LL,RR)和双旋调整(LR,RL)。


代码如下:

#include <stdio.h>
#include <iostream>

using namespace std;
typedef int ElementType;
typedef struct AVLTreeNode *AVLTree;
struct AVLTreeNode
{
	ElementType Data;
	AVLTree	Left;
	AVLTree Right;
	int Height;
};

AVLTree DoubleRightLeftRotation(AVLTree A);
AVLTree DoubleLeftRightRotation(AVLTree A);
AVLTree SingleLeftRotation(AVLTree A);
AVLTree SingleRightRotation(AVLTree A);
AVLTree AVL_Insertion(ElementType X, AVLTree T);
int max(int a, int b);
int getHeight(AVLTree A);

int main() {
	int cnt;
	AVLTree bt = NULL;
	cin >> cnt;
	for (int i = 0; i < cnt; i++) {
		int tmp;
		cin >> tmp;
		bt = AVL_Insertion(tmp, bt); //注意赋值bt :=
	}
	printf("%d\n", bt->Data);
}

AVLTree AVL_Insertion(ElementType X, AVLTree T) {
	/*将X插入AVL树T中,并返回调整后的AVL树*/
	if (!T) { /*若插入空树,则新建一个包含一个节点的树*/
		T = (AVLTree)malloc(sizeof(struct AVLTreeNode));
		T->Data = X;
		T->Left = T->Right = NULL;
		T->Height = 0;
	}/*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->Right) - getHeight(T->Left) == 2) {
			/*需要右旋*/
			if (X > T->Right->Data)
				T = SingleRightRotation(T);/*右单旋*/
			else
				T = DoubleRightLeftRotation(T);/*右左双旋*/
		}
	}/*else if(插入右子树)结束*/
	/*else if(X == T->Data),无需插入*/
	T->Height = max(getHeight(T->Left), getHeight(T->Right)) + 1;/*更新树高*/
	return  T;
}

AVLTree SingleLeftRotation(AVLTree A) {
	/*注意:A必须有一个左子节点B*/
	/*将A与B做左单旋,更新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), getHeight(A)) + 1;
	return B;
}

AVLTree SingleRightRotation(AVLTree A) {
	/*注意:A必须有一个右子节点B*/
	/*将A与B做右单旋,更新A与B的高度,返回新的根节点B*/
	AVLTree B = A->Right;
	A->Right = B->Left;
	B->Left = A;
	A->Height = max(getHeight(A->Left), getHeight(A->Right)) + 1;
	B->Height = max(getHeight(B->Right), getHeight(A)) + 1;
	return B;
}

AVLTree DoubleLeftRightRotation(AVLTree A) {
	/*注意:A必须有一个左节点B,且B必须有个一右子节点C,即B = A->Left, C = A->Left->Right*/
	/*先将B和C做一次右单旋,再将A和C做一次左单旋*/
	A->Left = SingleRightRotation(A->Left);
	return SingleLeftRotation(A);/*C被返回*/
}

AVLTree DoubleRightLeftRotation(AVLTree A) {
	/*注意:A必须有一个右节点B,且B必须有个一左子节点C,即B = A-Right, C = A->Right->Left*/
	/*先将B和C做一次左单旋,再将A和C做一次右单旋*/
	A->Right = SingleLeftRotation(A->Right);
	return SingleRightRotation(A);/*C被返回*/
}

int max(int a, int b) {
	return a > b ? a : b;
}

int getHeight(AVLTree A) {
	if (A == NULL)
		return -1;
	else
		return A->Height;
}

测试结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值