04-树5 Root of AVL Tree

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了04-树5 Root of AVL Tree相关的知识,希望对你有一定的参考价值。

题目

技术分享图片
技术分享图片
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

基本思路

考差了AVL树的插入操作,插入可以用一个递归函数完成,比较大小然后向左或右递归,递归边界为遇到空结点,返回时,检查每个结点的平衡因子是否为2或-2,是的话则进行旋转。

代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define AVL 
typedef struct Node* link;
struct Node
{
    int val;
    int height;
    link left;
    link right;
};

link insert(link & T, int val);
void L_Rotate(link & root);
void R_Rotate(link & root);
void freeTree(link T);
int getHeight(link T);
void updateHeight(link T);
int getBalanceFactor(link T);

int main()
{
    int N, temp;
    scanf("%d", &N);
    link root=NULL;
    for (int i = 0; i < N; i++)
    {
        scanf("%d", &temp);
        insert(root, temp);
    }
    printf("%d", root->val);
    return 0;
}



void R_Rotate(link & root)
{
    link A, B, C;
    A = root;
    B = A->left;
    C = B->right;
    A->left = C;
    B->right = A;
    updateHeight(A);
    updateHeight(B);
    root = B;
}

void L_Rotate(link & root)
{
    link A, B, C;
    A = root;
    B = A->right;
    C = B->left;
    A->right = C;
    B->left = A;
    updateHeight(A);
    updateHeight(B);
    root = B;
}
link insert(link & T, int val)
{
    
    if (T == NULL)          //如果该结点为空则创立新结点,结束递归
    {
        T = new Node;
        T->val = val;
        T->left = NULL;
        T->right = NULL;
        T->height = 1;             //插入的新结点高度为1
        return T;

    }

    
    if (val > T->val)                                 //插入右边  
    {
        T->right = insert(T->right, val);
        updateHeight(T);
#if defined(AVL)              //如果是AVL树,完成插入操作后要检查平衡因子
        if (getBalanceFactor(T) == -2)
        {
            if (getBalanceFactor(T->right) == -1)    //RR
            {
                L_Rotate(T);
            }
            else if (getBalanceFactor(T->right) == 1)  //LR
            {
                R_Rotate(T->right);
                L_Rotate(T);
            }

        }
#endif
    }
    else
    {
        T->left = insert(T->left, val);
        updateHeight(T);
#if defined(AVL)              //如果是AVL树,完成插入操作后要检查平衡因子
        if (getBalanceFactor(T) == 2)
        {
            if (getBalanceFactor(T->left) == 1)    //RR
            {
                R_Rotate(T);
            }
            else if (getBalanceFactor(T->left) == -1)  //LR
            {
                L_Rotate(T->left);
                R_Rotate(T);
            }
        }
#endif
    }

    return T;
}



void freeTree(link T)
{
    if (T->right)
        freeTree(T->right);
    if(T->left)
        freeTree(T->left);
    delete T;
}

int getHeight(link T)
{
    if (T == NULL)
        return 0;
    else
        return T->height;
}

int getBalanceFactor(link T)
{
    return getHeight(T->left)-getHeight(T->right);
}

void updateHeight(link T)
{
    T->height = max(getHeight(T->left), getHeight(T->right))+1;
}

总结

以上是关于04-树5 Root of AVL Tree的主要内容,如果未能解决你的问题,请参考以下文章

04-树5 Root of AVL Tree

04-树5 Root of AVL Tree

04-树5 Root of AVL Tree (25 分)

04-树5 Root of AVL Tree(25 分)

04-树4. Root of AVL Tree (25)

PAT 1066 Root of AVL Tree[AVL树][难]