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);
}