PTA_2019春_045_ Root of AVL Tree

这篇博客介绍了AVL树的概念,一种自我平衡的二叉搜索树。当节点的两个子树高度差超过1时,需要进行旋转以保持平衡。内容包含输入输出规格说明以及样例,任务是给出插入序列后AVL树的根节点。

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

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

 

/*Root of AVL Tree */
/*思路:直接暴力解题,将输入生成AVL,然后输出根节点
        对于数字相同,但是序列不同的一串数字序列,其AVL树是可以不同的
		递归实现插入操作*/
/*小结:定义一个指针可以不用初始化,但是在使用指针时一定要确定它是否被初始化即是否指向了某一个确定的可访问的内存位置。*/
#include<stdio.h>
#include<stdlib.h>
typedef struct treenode {
	int data;        /*数据*/
	struct treenode* left;  /*左子树*/
	struct treenode* right;  /*右子树*/
	int height;              /*树的高度*/
}Tree;
typedef Tree* avltree;

int max(int a, int b);
avltree insert(avltree T, int x);
int getheight(avltree T);
avltree LL(avltree A);
avltree RR(avltree A);
avltree LR(avltree A);
avltree RL(avltree A);
int main() {
	int N; /*节点个数*/
	scanf("%d", &N);
	int* array = (int*)malloc(N * sizeof(int));
	for (int i = 0; i < N; i++) {
		scanf("%d", &array[i]);
	}/*输入完成*/
	 
	 /*下面AVL树生成*/
	avltree T ;     
	T = NULL;      /*之前一直错误是因为指针T没有初始化,此处要注意!!!*/
                   /*定义一个指针可以不用初始化,但是在使用指针时一定要确定
				   它是否被初始化即是否指向了某一个确定的可访问的内存位置。*/
	for (int i = 0; i < N; i++) {
		T = insert(T, array[i]);
	}/*树建成完毕*/
	
	printf("%d", T->data);
	return 0;
}
int max(int a, int b) {
	if (a > b) return a;
	else return b;
}
avltree insert(avltree T, int x) {
	if (!T) {
		T = (avltree)malloc(sizeof(struct treenode));
		T->data = x;
		T->left = NULL;
		T->right = NULL;
		T->height = 1;
	}
	else if (x < T->data) {  /*x插在左子树*/
		T->left = insert(T->left, x);  /*插入完毕*/
		/*下面这个调整,其实对于一个树来说只做一次,即插入后发生问题的那个节点
		   调整后,往上的父节点均不用在调整。于是这个调整语句,只作用在第一个发生
		   问题的节点。
		   以此帮助理解这个递归*/
		if (getheight(T->left) - getheight(T->right) == 2) {  /*插入的点不满足AVL,需调整*/
			if (x < T->left->data) {
				T = LL(T);
			}
			else  T = LR(T);
		}
	}
	else if (x > T->data) {
		T->right = insert(T->right, x);
		if (getheight(T->right) - getheight(T->left) == 2) {
			if (x > T->right->data) {
				T = RR(T);
			}
			else T = RL(T);
		}
	}
	T->height = max(getheight(T->left), getheight(T->right)) + 1;
	return T;
}
int getheight(avltree T) {
	int height;
	if (T) {
		return (max(getheight(T->left), getheight(T->right))+1);
	}
	else return 0; /*空树为0*/
}
avltree LL(avltree A) {
	avltree B = A->left;
	A->left = B->right;
	B->right = A;                /*之前没过的原因是,我把此处的right写成left导致LL不成功*/
	A->height = max(getheight(A->left), getheight(A->right) )+ 1;
	B->height = max(getheight(B->left), A->height) + 1;
	return B;
}
avltree RR(avltree A) {
	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), A->height) + 1;
	return B;
}
avltree LR(avltree A) {
	A->left = RR(A->left);    
	return LL(A);
}
avltree RL(avltree A) {
	A->right = LL(A->right);
	return RR(A);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值