**04-树5 Root of AVL Tree (25 分)** 初识二叉平衡树

04-树5 Root of AVL Tree (25 分)
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

#define _CRT_SECURE_NO_WARNINGS
#include
#include
struct tree
{
	int value;
	struct tree* left;
	struct tree* right;
	int height;
	int bf;
};
typedef struct tree AVL;
int minemax(int a, int b)
{
	return ((a > b) ? a : b);
}
int getheight(AVL* node)/////获取以node为根节点的树的高度值,规定空树的高度值为-1
{
	if (node == NULL) return -1;
	else
		node->height= minemax(getheight(node->left),getheight(node->right))+1;
	return node->height;
}
void resethigh(AVL* node)///////////////重置以node为根节点的高度值  规定空树的高度值为-1
{
	if (node == NULL) return;///////////////空树不用重置
	else
	{
		node->height = getheight(node);
		resethigh(node->left);
		resethigh(node->right);
	}
			
}
int putbf(AVL* node)////得到某个节点的平衡值   等于其左子树高度减去右子树高度
{
	node->bf = getheight(node->left) - getheight(node->right);
	return node->bf;
}
AVL* RR_rotation(AVL* node)
{
	AVL* temp;
	temp = node->right;
	node->right = temp->left;
	temp->left = node;
	node = temp;
	//rr旋转完成后需要重新获取node的高度
	//
	resethigh(node);
	//resethigh(node->left);
	return node;
}
AVL* LL_rotation(AVL* node)
{
	AVL* temp;
	temp = node->left;
	node->left = temp->right;
	temp->right = node;
	node = temp;
	resethigh(node);
	return node;
}
AVL* LR_rotation(AVL* node)
{
	node->left=RR_rotation(node->left);
	node=LL_rotation(node);
	return node;
}
AVL* RL_rotation(AVL* node)
{
	node->right = LL_rotation(node->right);
	node = RR_rotation(node);
	return node;
}
AVL* insert(int newvalue, AVL* oldp)///////////将一个新的数插入oldAVL中
{
	if (oldp == NULL)///////////几点为空时
	{
		AVL* newnode;
		newnode = (AVL*)malloc(sizeof(AVL));
		newnode->value = newvalue;
		newnode->left = NULL;
		newnode->right = NULL;
		newnode->height = -1;
		return newnode;
	}
	if (oldp->value < newvalue)////插到右子树
	{
		oldp->right = insert(newvalue, oldp->right);
		resethigh(oldp);
		if (putbf(oldp) < -1)////////////平衡值bf小于-1需要调整
		{
			if (newvalue > oldp->right->value)//////大于节点右儿子的值就为RR旋转
				oldp = RR_rotation(oldp);///////////意味插到了右儿子的右儿子
			else if (newvalue < oldp->right->value)//////////////小于就为RL旋转
				oldp = RL_rotation(oldp);		//////////////插到了右儿子的左儿子
		}
		return oldp;
	}
	else if (oldp->value > newvalue)//插到左子树
	{
		oldp->left = insert(newvalue, oldp->left);
		resethigh(oldp);
		if (putbf(oldp) > 1)///////////平衡值bf大于1需要调整
		{
			if (newvalue < oldp->left->value)///////////小于节点左儿子的值就为LL旋转
				oldp = LL_rotation(oldp);			///////意味着插到了左儿子的左儿子
			else if (newvalue > oldp->left->value)//////大于节点左儿子的值就为LR旋转
				oldp = LR_rotation(oldp);/////////////意味着插到了左儿子的右儿子
		}
		return oldp;
	}
	else return oldp;////如果插入值等于该节点值则不进行任何操作
}
int main()
{
	int a[20],count;
	AVL* p;
	p = NULL;
	scanf("%d", &count);
	
	for (int i = 0; i < count; i++)
	{
		scanf("%d", &a[i]);
		p = insert(a[i], p);
	}
	printf("%d", p->value);
	return 0;
}

你可能感兴趣的:(数据结构与算法,初级)