PTA AVL Insertion

116 阅读1分钟

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 height(AVLTree T) 
{
    AVLTree l = T->Left;
    AVLTree r = T->Right;
    if (r == NULL&&l == NULL)
        return 1;
    else if (r == NULL&&l != NULL) 
        return l->Height + 1;
    else if (r != NULL&&l == NULL) 
        return r->Height + 1;
    else 
    {
        if (r->Height > l->Height) 
            return r->Height + 1;
        else 
            return l->Height + 1;
    }
}
int judge(AVLTree T)
{
    int r = 0, l = 0;
    if (T->Left != NULL) 
        l = T->Left->Height;
    if (T->Right != NULL) 
        r = T->Right->Height;
    return l - r;
}
AVLTree Insert(AVLTree T, int Key) 
{
    if (T == NULL) 
    {
        T = (AVLTree)malloc(sizeof(struct AVLNode));
        T->Left = T->Right = NULL; 
        T->Key = Key; 
        T->Height = 1;
    }
    else 
    {
        if (T->Key > Key)  
            T->Left = Insert(T->Left, Key);
        else if (T->Key < Key)
            T->Right = Insert(T->Right,Key);
        T->Height = height(T);
        if (judge(T) > 1) 
        {
            AVLTree l = T->Left;
            if (judge(l) >0) 
            {  
                T->Left = l->Right;
                l->Right = T;
                T = l;
                T->Right->Height = height(T->Right);
            }
            else if(judge(l) <0)
            {
                AVLTree r = l->Right;
                l->Right = r->Left;
                r->Left = l;
                l = r;
                T->Left = l->Right;
                l->Right = T;
                T = l;
                T->Left->Height = height(T->Left);
                T->Right->Height = height(T->Right);
            }  
            T->Height = height(T);
        }
        else if (judge(T)<-1) 
        { 
            AVLTree r = T->Right;
            if (judge(r) >0) 
            { 
                AVLTree l = r->Left;
                r->Left = l->Right;
                l->Right = r;
                r = l;
                T->Right = r->Left;
                r->Left = T;
                T = r;
                T->Left->Height = height(T->Left);
                T->Right->Height = height(T->Right);
            }
            else if (judge(r) <0) 
            {
                T->Right = r->Left;
                r->Left = T;
                T = r;
                T->Left->Height = height(T->Left);
            }
            T->Height = height(T);
        }
    }
    return T;
} 

提交结果:

3.png