6-3 AVL Insertion (30 分)

本文详细介绍了如何在AVL树中实现插入操作,并保持树的平衡。通过具体代码示例,展示了单旋转和双旋转调整过程,确保树的高度平衡,以维持O(logn)的搜索效率。

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

You are supposed to implement the Insert function, which inserts an integer Key into an AVL tree T. The resulting tree must be returned.

Format of function:

AVLTree Insert ( AVLTree T, int Key );

where AVLTree is defined as the following:

typedef struct AVLNode *PtrToAVLNode;
struct AVLNode{
    int Key;
    PtrToAVLNode Left;
    PtrToAVLNode Right;
    int Height;
};
typedef PtrToAVLNode AVLTree;

Sample program of judge:

#include <stdio.h>
#include <stdlib.h>

typedef struct AVLNode *PtrToAVLNode;
struct AVLNode{
    int Key;
    PtrToAVLNode Left;
    PtrToAVLNode Right;
    int Height;
};
typedef PtrToAVLNode AVLTree;

AVLTree Insert ( AVLTree T, int Key );
void PostOrderPrint( AVLTree T ); /* details omitted */
void InOrderPrint( AVLTree T );   /* details omitted */

int main()
{
    int N, Key, i;
    AVLTree T = NULL;

    scanf("%d", &N);
    for ( i=0; i<N; i++ ) {
        scanf("%d", &Key);
        T = Insert( T, Key );
    }
    PostOrderPrint( T );
    InOrderPrint( T );

    return 0;
}
/* Your function will be put here */

Sample Input:

7
88 70 61 96 120 90 65

Sample Output:

Post-order: 61 70 65 90 120 96 88
In-order: 61 65 70 88 90 96 120
int Max(int a, int b){
    return a>b?a:b;
}

int GetHeight(AVLTree A){
    if(A==NULL)
        return 0;
    return A->Height;
}

AVLTree SingleLeftRotation(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 SingleRightRotation(AVLTree A){
    AVLTree B=A->Left;
    A->Left=B->Right;
    B->Right=A;
    A->Height=Max(GetHeight(A->Left), GetHeight(A->Right))+1;
    B->Height=Max(GetHeight(B->Left),A->Height)+1;
    return B;
}

AVLTree DoubleLeftRightRotation(AVLTree A){
    A->Right = SingleRightRotation(A->Right);
    return SingleLeftRotation(A);
}


AVLTree DoubleRightLeftRotation(AVLTree A){
    A->Left = SingleLeftRotation(A->Left);
    return SingleRightRotation(A);
}


AVLTree Insert(AVLTree T,int X){
    if(!T){
        T=(AVLTree)malloc(sizeof(struct AVLNode));
        T->Key=X;
        T->Height=1;
        T->Left=T->Right=NULL;
    }

    else if(X<T->Key){ //插入左子树
        T->Left=Insert(T->Left, X);
        if(GetHeight(T->Left)-GetHeight(T->Right)==2){
            if (X<T->Left->Key) {
                T=SingleRightRotation(T);
            }
            else
                T=DoubleRightLeftRotation(T);
        }
    }

    else if(X>T->Key){
        T->Right=Insert(T->Right, X);
        if(GetHeight(T->Left)-GetHeight(T->Right)==-2){
            if (X>T->Right->Key)
                T=SingleLeftRotation(T);
            else
                T=DoubleLeftRightRotation(T);

        }
    }
    T->Height=Max(GetHeight(T->Left), GetHeight(T->Right))+1;
    return T;
}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值