04-1. Root of AVL Tree (PAT)

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

    

    

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print ythe root of the resulting AVL tree in one line.

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),输出根结点元素解题思路:判断插入结点对现有结点的平衡因子的影响,进而进行LL,LR,RL,RR旋转假设三个结点连接关系为A->B->C,C为新插入结点并使得A的平衡因子==2若C在A的左孩子的左子树上,则对A与B进行LL旋转若C在A的左孩子的右子树上,则对A,B,C进行LR旋转,可分解为首先对B与C进行RR旋转,再对A与C进行LL旋转若C在A的右孩子的右子树上,则对A与B进行RR旋转若C在A的右孩子的左子树上,则对A,B,C进行RL旋转,可分解为首先对B与C进行LL旋转,再对A与C进行RR旋转
#include <iostream>
#include <string>

typedef struct AVLTreeNode{
    int Data;
    AVLTreeNode *Left;
    AVLTreeNode *Right;
    int Height;
}nAVLTree ,*pAVLTree;

pAVLTree AVLInsertion( int nodeValue, pAVLTree pAvl );
int GetALVHeight( pAVLTree );
pAVLTree SingleLeftRotation( pAVLTree );
pAVLTree DoubleLeftRotation( pAVLTree );
pAVLTree SingleRightRotation( pAVLTree );
pAVLTree DoubleRightRotation( pAVLTree );
int Max( int hight1, int hight2 );
using namespace std;

int main()
{
    int num;
    int i;
    int value;
    pAVLTree pAvl;
    pAvl = NULL;
    cin >> num;
    for ( i = 0; i < num; i++ )
    {
        cin >> value;
        pAvl = AVLInsertion( value, pAvl);
    }
    cout << pAvl->Data;
}

pAVLTree AVLInsertion( int nodeValue, pAVLTree pAvl )
{
    if ( pAvl == NULL )
    {
        pAvl = ( pAVLTree )malloc( sizeof( nAVLTree ) );
        pAvl->Left = pAvl->Right = NULL;
        pAvl->Data = nodeValue;
        pAvl->Height = 0;
    }
    else if ( nodeValue < pAvl->Data )
    {
        pAvl->Left = AVLInsertion( nodeValue, pAvl->Left );
        if ( GetALVHeight( pAvl->Left ) - GetALVHeight( pAvl->Right ) == 2 )
        {
            if ( nodeValue < pAvl->Left->Data )
            {
                pAvl = SingleLeftRotation( pAvl );
            }
            else
            {
                pAvl = DoubleLeftRotation( pAvl );
            }
        }
    }
    else if ( nodeValue > pAvl->Data )
    {
        pAvl->Right = AVLInsertion( nodeValue, pAvl->Right );
        if ( GetALVHeight( pAvl->Right ) - GetALVHeight( pAvl->Left ) == 2 )
        {
            if ( nodeValue > pAvl->Right->Data )
            {
                pAvl = SingleRightRotation( pAvl );
            }
            else
            {
                pAvl = DoubleRightRotation( pAvl );
            }
        }
    }
    pAvl->Height = Max( GetALVHeight( pAvl->Left ), GetALVHeight( pAvl->Right ) ) + 1;
    return pAvl;
}

pAVLTree SingleLeftRotation( pAVLTree A )
{
    pAVLTree B = A->Left;
    A->Left = B->Right;
    B->Right = A;
    A->Height = Max( GetALVHeight( A->Left ), GetALVHeight( A->Right ) ) + 1;
    B->Height = Max( GetALVHeight( B->Left ), A->Height ) + 1;
    return B;
}

pAVLTree DoubleLeftRotation( pAVLTree A )
{
    A->Left = SingleRightRotation( A->Left );
    return SingleLeftRotation( A );
}

pAVLTree SingleRightRotation( pAVLTree A )
{
    pAVLTree B = A->Right;
    A->Right = B->Left;
    B->Left = A;
    A->Height = Max( GetALVHeight( A->Left ), GetALVHeight( A->Right ) ) + 1;
    B->Height = Max( GetALVHeight( B->Right ), A->Height ) + 1;
    return B;
}

pAVLTree DoubleRightRotation( pAVLTree A )
{
    A->Right = SingleLeftRotation( A->Right );
    return SingleRightRotation( A );
}

int GetALVHeight( pAVLTree pAvl)
{
    if ( pAvl == NULL )
    {
        return 0;
    }
    else
    {
        return pAvl->Height;
    }
}

int Max( int hight1, int hight2 )
{
    return hight1 > hight2 ? hight1 : hight2;
}
时间: 2024-10-08 03:50:09

04-1. Root of AVL Tree (PAT)的相关文章

1066. Root of AVL Tree (25)【AVL树】——PAT (Advanced Level) Practise

题目信息 1066. Root of AVL Tree (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more tha

PAT Root of AVL Tree

Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. F

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

1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore

PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

1066 Root of AVL Tree (25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this

04-树5 Root of AVL Tree + AVL树操作集

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illust

PAT_A1066#Root of AVL Tree

Source: PAT A1066 Root of AVL Tree (25 分) Description: An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalanci

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

04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any tim

04-1. Root of AVL Tree (25)

04-1. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time

pat1066. Root of AVL Tree (25)

1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any tim