浙大数据结构04-树5 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. Figures 1-4 illustrate the rotation rules.

浙大数据结构04-树5 Root of AVL Tree_平衡二叉树_第1张图片

浙大数据结构04-树5 Root of AVL Tree_平衡二叉树_第2张图片

浙大数据结构04-树5 Root of AVL Tree_平衡二叉树_第3张图片

浙大数据结构04-树5 Root of AVL Tree_平衡二叉树_第4张图片

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 the 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
#include 
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef struct node *AVL;
struct node
{
    int value;
    AVL left,right;
};
int getHeight(AVL tree)
{
    if(tree == NULL)
    {
        return 0;
    }
    int a=getHeight(tree->left);
    int b=getHeight(tree->right);
    return max(a,b)+1;
}
AVL leftRotation(AVL A)
{
    AVL B=A->left;
    A->left=B->right;
    B->right=A;
    return B;
}
AVL rightRotation(AVL A)
{
    AVL B=A->right;
    A->right=B->left;
    B->left=A;
    return B;
}
void insert(AVL &tree,int value)
{
    if(tree == NULL)
    {
        tree = (AVL)malloc(sizeof(struct node));
        tree->left=NULL;
        tree->right=NULL;
        tree->value=value;
    }
    else
    {
        if(value < tree->value)
        {
            insert(tree->left,value);
            int l=getHeight(tree->left),r=getHeight(tree->right);
            if(l-r > 1)     // 需要左旋
            {
                if(value < tree->left->value)  // 左单旋
                {
                    tree = leftRotation(tree);
                }
                else    // 左右双旋
                {
                    tree->left = rightRotation(tree->left);
                    tree = leftRotation(tree);
                }
            }
        }
        else if(value > tree->value)
        {
            insert(tree->right,value);
            int l=getHeight(tree->left),r=getHeight(tree->right);
            if(r-l > 1)     // 需要右旋
            {
                if(value > tree->right->value)  // 右单旋
                {
                    tree = rightRotation(tree);
                }
                else    // 右左双旋
                {
                    tree->right = leftRotation(tree->right);
                    tree = rightRotation(tree);
                }
            }
        }
    }
}
int main()
{
//    system("chcp 65001");
    cin.tie(0);
    cout.tie(0);
//    freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
    AVL tree=NULL;
    int n,t;
    cin>>n;
    for(int i=0;i>t;
        insert(tree,t);
    }
    cout<< tree->value;
    return 0;
}

你可能感兴趣的:(浙大数据结构,1024程序员节)