1066 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
分析
做旋转的手工图,一步步来
参考https://blog.csdn.net/galesaur_wcy/article/details/82492540
代码
#include<stdio.h>
struct node{
int data;
struct node *left,*right;
};
int getheight(struct node *tree){
int left,right;
if(tree==NULL) return 0;
left=getheight(tree->left)+1;
right=getheight(tree->right)+1;
if(left>right) return left;
else return right;
}
struct node* leftRotate(struct node* tree){//figure 1
struct node *p=tree;
// printf("%d left rotate to ",p->data);//delete
tree=tree->left;
p->left=tree->right;
tree->right=p;
// printf("%d\n",tree->data);//delete
return tree;
}
struct node* leftRightRotate(struct node* tree){//figure 4
struct node *p=tree->left->right;
///set 61 right child to 65 left child
tree->left->right=p->left;
///set 65 left child to 61
p->left=tree->left;
///set 70 left child to 61
tree->left=p;
// printf("%d left right rotate to %d\n",p->data,tree->data);//delete
tree=leftRotate(tree);
return tree;
}
struct node* rightRotate(struct node* tree){//figure 2
struct node *p=tree;
tree=tree->right;
p->right=tree->left;
tree->left=p;
// printf("%d right rotate to %d\n",p->data,tree->data);//delete
return tree;
}
struct node* rightLeftRotate(struct node *tree){
struct node *p=tree->right->left;
///set 96 left child to 90 (88's right child)
tree->right->left=p->right;
///set 88 right child to 96
p->right=tree->right;
///set 70 right child to 88
tree->right=p;
// printf("%d right left rotate to %d\n",p->data,p->right->data);//delete
tree=rightRotate(tree);
return tree;
}
struct node* insert(struct node *tree, int x){
int left,right;
if(tree==NULL){
tree=(struct node *)malloc(sizeof(struct node *));
tree->data=x;
tree->left=tree->right=NULL;
}
if(x<tree->data){
tree->left=insert(tree->left,x);
left=getheight(tree->left);
right=getheight(tree->right);
if(left-right>=2){
if(x<tree->left->data) tree=leftRotate(tree);//figure 1
else tree=leftRightRotate(tree);//figure 4
}
}
else if(x>tree->data){
tree->right=insert(tree->right,x);
left=getheight(tree->left);
right=getheight(tree->right);
if(right-left>=2)
if(x>tree->right->data) tree=rightRotate(tree);//figure 2
else tree=rightLeftRotate(tree);//figure 3
}
return tree;
}
int main(){
int N,i,x;
struct node *tree=NULL;
scanf("%d",&N);
for(i=0;i<N;i++){
scanf("%d",&x);
tree=insert(tree,x);
// printf("tree height %d\n", getheight(tree));//delete
}
printf("%d",tree->data);
}