PAT (Advanced Level) Practise 1066 Root of AVL Tree (25)

本文介绍了一种自我平衡的二叉搜索树——AVL树,并通过代码详细解释了如何实现AVL树的插入操作及保持其平衡性的方法。在一系列数值插入后,输出最终AVL树的根节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1066. Root of AVL Tree (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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


题意:将n个数插入AVL树中,输出树的根节点


#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <cmath>  
#include <map>   
#include <set>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <bitset>  
#include <functional>  

using namespace std;

#define LL long long  
const int INF = 0x3f3f3f3f;
const int N = 1e3 + 10;

int n, x;
int rt,sz; //根节点;总结点个数,用于建立新节点  
int son[N][2]; //记录左右儿子的标号  
int h[N],a[N];  //记录该节点向下最远的高度,用于维持avl数的平衡 ; 记录节点的值,维持插入需要  
int f[N];  //记录节点的父亲节点标号  

int node(int x, int fa)//数组模拟,建立新节点,父亲节点为fa,值为x,此时高度为1,没有子节点。 
{
	a[sz] = x; f[sz] = fa; h[sz] = 1; memset(son[sz], 0,sizeof son[sz]); return sz++;
}

void Count(int x)//重新计算该节点高度
{
	h[x] = max(h[son[x][0]], h[son[x][1]]) + 1;
}

void rotate(int x, int k)//旋转操作,x为节点编号,k为旋转类型,k=1时为右旋,k=0是为左旋  
{
	int y = f[x]; son[y][!k] = son[x][k];
	if (son[x][k]) f[son[x][k]] = y;
	if (f[y]) son[f[y]][y == son[f[y]][1]] = x;
	f[x] = f[y]; f[y] = x; son[x][k] = y;
	Count(y); Count(x);
}

/*
调整过程,根据avl树的性质,如果某个节点的左右高度差超过1,那么进行调整
需要注意的是,avl树的旋转都是三个节点之间的,x,F[x],F[F[x]]
并且分为两种,即x和F[x]都是同方向的,那么将F[x]向上旋转即可。
如果是不同方向,那么将x连续向上旋转两次。
*/
int change(int x)
{
	for (int i = f[f[x]]; i; i = f[f[x]])
	{
		Count(f[x]); Count(i);
		if (abs(h[son[i][0]] - h[son[i][1]]) > 1)
		{
			int y = x == son[f[x]][0], z = f[x] == son[i][0];
			y^z ? (rotate(x, y), rotate(x, z)) : rotate(f[x], z);
		}
		else x = f[x];
	}
	while (f[x]) { Count(x); x = f[x]; }
	return x;
}

void insert(int &rt, int x)//插入过程,像普通的排序二叉树一样插入,最后进行调整。  
{
	if (!rt) { rt = node(x, 0); return; }
	for (int i = rt;; i = son[i][a[i] < x])
	{
		if (son[i][a[i] < x]) continue;
		son[i][a[i] < x] = node(x, i);
		rt = change(son[i][a[i] < x]); return;
	}
}

int main()
{
	while (~scanf("%d",&n))
	{
		rt = 0; sz = 1;
		for(int i=1; i<=n; i++)
			scanf("%d",&x), insert(rt, x);
		printf("%d\n", a[rt]);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值