AVL Insertion(30分)

AVL Insertion(30分)

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

作者:Evinci

出处:https://www.cnblogs.com/evinci/p/17123984.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   EEvinci  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示