PTA数据结构练习题——平衡二叉树的根

题目介绍:

将给定的一系列数字插入初始为空的AVL树,请你输出最后生成的AVL树的根结点的值。

输入格式:
输入的第一行给出一个正整数N(≤20),随后一行给出N个不同的整数,其间以空格分隔。

输出格式:
在一行中输出顺序插入上述整数到一棵初始为空的AVL树后,该树的根结点的值。
输入样例1:

5
88 70 61 96 120

输出样例1:

70

输入样例1:

7
88 70 61 96 120 90 65

输出样例1:

88

代码实现:

#include<iostream>
#include<algorithm>

typedef struct AVLNode* AVLTree;
struct AVLNode{
    int v;
    AVLTree left;
    AVLTree right;
    int Height;
    AVLNode(int x): v(x), left(nullptr), right(nullptr), Height(0) {}
};

using std::cin;
using std::cout;
using std::endl;
using std::max;

AVLTree InsertAVL(AVLTree T, int x);//插入节点并更新头节点
void FreeTree(AVLTree T);
AVLTree SingleLeftRotation (AVLTree A);//左单旋
AVLTree SingleRightRotation (AVLTree A);//右单旋
AVLTree DoubleLeftRightRotation (AVLTree A);//左右双旋
AVLTree DoubleRightLeftRotation (AVLTree A);//右左双旋
int GetHeight(AVLTree T);//获取树高


int main()
{
    int N, temp;
    cin >> N;
    cin >> temp;
    AVLTree head = new AVLNode(temp);
    for(int i = 1; i < N; i++){
        cin >> temp;
        head = InsertAVL(head, temp);
    }
    cout << head->v;
    FreeTree(head);
    return 0;
}

AVLTree SingleLeftRotation (AVLTree A){
    AVLTree B = A->left;
    A->left = B->right;
    B->right = A;
    A->Height = max(GetHeight(A->left), GetHeight(A->right)) + 1;
    B->Height = max(GetHeight(B->left), A->Height) + 1;
    return B;
}

AVLTree SingleRightRotation (AVLTree A){
    AVLTree B = A->right;
    A->right = B->left;
    B->left = A;
    A->Height = max(GetHeight(A->left), GetHeight(A->right)) + 1;
    B->Height = max(A->Height, GetHeight(B->right)) + 1;
    return B;
}

AVLTree DoubleLeftRightRotation (AVLTree A){
    A->left = SingleRightRotation(A->left);
    return SingleLeftRotation(A);
}

AVLTree DoubleRightLeftRotation (AVLTree A){
    A->right = SingleLeftRotation(A->right);
    return SingleRightRotation(A);
}

AVLTree InsertAVL(AVLTree T, int x){
    if(!T){
        T = new AVLNode(x);
    }
    else if(x < T->v){
        T->left = InsertAVL(T->left, x);
        if(GetHeight(T->left) - GetHeight(T->right) == 2){
            if(x < T->left->v)
                T = SingleLeftRotation(T);
            else
                T = DoubleLeftRightRotation(T);
        }
    }
    else if(x > T->v){
        T->right = InsertAVL(T->right, x);
        if(GetHeight(T->right) - GetHeight(T->left) == 2){
            if(x > T->right->v)
                T = SingleRightRotation(T);
            else
                T = DoubleRightLeftRotation(T);
        }
    }
    T->Height = max(GetHeight(T->left), GetHeight(T->right)) + 1;
    return T;
}
int GetHeight(AVLTree T){
    if(!T)
        return 0;
    else
        return max(GetHeight(T->left), GetHeight(T->right)) + 1;
}

void FreeTree(AVLTree T){
    if(T->left)
        FreeTree(T->left);
    if(T->right)
        FreeTree(T->right);
    delete T;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值