码迷,mamicode.com
首页 > 其他好文 > 详细

Root of AVL Tree

时间:2019-08-31 19:40:32      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:main   高度   use   crt   语言   ++   war   system   i++   

  • 题目描述:
    技术图片

技术图片

技术图片

  • 题目思路:

这道题主要为了考察对AVL树的插入与旋转等操作。

  • C语言实现
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

struct AVLNode
{
    int Data;  //节点数据
    struct AVLNode* Left;  //指向左子树
    struct AVLNode* Right;  //指向右子树
    int Height;  //树高
};

int Max(int a,int b)
{
    return a > b ? a : b;
}

//返回树的高度
int GetH(struct AVLNode* T)
{
    if (T)
    {
        return T->Height;
    }
    else
    {
        return -1;
    }
}

//右单旋 RR
struct AVLNode* RR(struct AVLNode* A)
{
    struct AVLNode* B;  //旋转后的根节点
    B = A->Right;
    A->Right = B->Left;
    B->Left = A;
    //更新树的高度
    A->Height = Max(GetH(A->Left),GetH(A->Right)) + 1;
    B->Height = Max(GetH(B->Left),A->Height) + 1;
    return B;
}

struct AVLNode* LL(struct AVLNode* A)
{
    struct AVLNode* B;
    B = A->Left;
    A->Left = B->Right;
    B->Right = A;
    A->Height = Max(GetH(A->Left),GetH(A->Right)) + 1;
    B->Height = Max(GetH(B->Left), A->Height) + 1;
    return B;
}

//LR双旋直接实现
struct AVLNode* LR(struct AVLNode* A)
{
    struct AVLNode* B, *C;
    B = A->Left;
    C = B->Right;
    B->Right = C->Left;
    A->Left = C->Right;
    C->Left = B;
    C->Right = A;

    B->Height = Max(GetH(B->Left), GetH(B->Right)) + 1;
    A->Height = Max(GetH(A->Left),GetH(A->Right)) + 1;
    C->Height = Max(B->Height,A->Height) + 1;
    return C;
}

//RL双旋直接实现
struct AVLNode* RL(struct AVLNode* A)
{
    struct AVLNode* B, *C;
    B = A->Right;
    C = B->Left;
    B->Left = C->Right;
    A->Right = C->Left;
    C->Left = A;
    C->Right = B;

    B->Height = Max(GetH(B->Left), GetH(B->Right)) + 1;
    A->Height = Max(GetH(A->Left), GetH(A->Right)) + 1;
    C->Height = Max(A->Height, B->Height) + 1;
    return C;
}

//作用:将x插入AVL树T中,并且返回调整后的AVL树
struct AVLNode* Insert(struct AVLNode* T, int x)
{
    if (!T)
    {
        //若插入空树 则新建包含一个节点的树
        T = (struct AVLNode*)malloc(sizeof(struct AVLNode));
        T->Data = x;
        T->Left = NULL;
        T->Right = NULL;
        T->Height = 0;
    }
    else if (x < T->Data)
    {
        //插入T的左子树
        T->Left = Insert(T->Left, x);
        
        //如果需要左旋
        if (GetH(T->Left) - GetH(T->Right)  == 2)
        {
            if (x < T->Left->Data)
            {
                //左单旋
                T = LL(T);
            }
            else
            {
                //左右双旋
                T = LR(T);
            }
        }
    }
    else if (x > T->Data)
    {
        //插入T的右子树
                //插入T的左子树
        T->Right = Insert(T->Right, x);

        //如果需要左旋
        if (GetH(T->Left) - GetH(T->Right) == -2)
        {
            if (x > T->Right->Data)
            {
                //右单旋
                T = RR(T);
            }
            else
            {
                //右左双旋
                T = RL(T);
            }
        }

    }//else if 插入右子树结束

    //更新树高
    T->Height = Max(GetH(T->Left),GetH(T->Right)) + 1;
    return T;
}

int main()
{
    int N;
    int K;
    struct AVLNode* T = NULL;
    scanf("%d",&N);
    for (int i = 0; i< N;i++)
    {
        scanf("%d",&K);
        T = Insert(T, K);
    }
    printf("%d",T->Data);
    //system("pause");
    return 0;
}

Root of AVL Tree

标签:main   高度   use   crt   语言   ++   war   system   i++   

原文地址:https://www.cnblogs.com/Manual-Linux/p/11439770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!