1066 Root of AVL Tree (25 分)C语言——平衡二叉树的旋转

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值