PAT Grade A 1066 Root of AVL Tree (25 points) | Implémentation C ++

1. Description du titre

Lien du titre original
Insérez la description de l'image ici

Spécification d'entrée:

Chaque fichier d'entrée contient un cas de test. Pour chaque cas, la première ligne contient un entier positif N (≤20) qui est le nombre total de clés à insérer. Alors N clés entières distinctes sont données dans la ligne suivante. Tous les nombres d'une ligne sont séparés par un espace.

Spécification de sortie:

Pour chaque cas de test, imprimez la racine de l'arborescence AVL résultante sur une ligne.

Exemple d'entrée 1:

5
88 70 61 96 120

Exemple de sortie 1:

70

Exemple d'entrée 2:

7
88 70 61 96120 90 65

Exemple de sortie 2:

88

Deux idées de résolution de problèmes

Cette question est une question d'arborescence AVL relativement complète, qui implique essentiellement toutes les opérations de l'arborescence AVL, et est très appropriée pour la consolidation après avoir appris l'arborescence AVL. Pour des connaissances spécifiques, veuillez vous référer à l'explication sur l'arborescence AVL dans "Notes sur l'algorithme".

Trois, code AC

#include<iostream>
#include<algorithm>
using namespace std;
struct Node //建立结点结构体
{
    
    
    int key;
    Node* lchild, *rchild;
};
Node* rotateLeft(Node* root)    //左旋操作
{
    
    
    Node *t = root->rchild;
    root->rchild = t->lchild;
    t->lchild = root;
    return t;
}
Node* rotateRight(Node* root)   //右旋操作
{
    
    
    Node *t = root->lchild;
    root->lchild = t->rchild;
    t->rchild = root;
    return t;
}
Node* rotateLeftRight(Node* root)   //先左旋再右旋
{
    
    
    root->lchild = rotateLeft(root->lchild);
    return rotateRight(root);
}
Node* rotateRightLeft(Node* root)   //先右旋再左旋
{
    
    
    root->rchild = rotateRight(root->rchild);
    return rotateLeft(root);
}
int getHeight(Node* root)   //获取高度
{
    
    
    if(root == NULL)    return 0;
    return max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
}
Node* insert(Node *root, int val)   //AVL树的插入
{
    
    
    if(root == NULL)
    {
    
    
        root = new Node();
        root->lchild = NULL;
        root->rchild = NULL;
        root->key = val;
        return root;
    }
    else if(val < root->key)
    {
    
    
        root->lchild = insert(root->lchild, val);
        if(getHeight(root->lchild) - getHeight(root->rchild) == 2)
            root = val < root->lchild->key ? rotateRight(root) : rotateLeftRight(root);
    }
    else
    {
    
    
        root->rchild = insert(root->rchild, val);
        if(getHeight(root->rchild) - getHeight(root->lchild) == 2)
            root = val > root->rchild->key ? rotateLeft(root) : rotateRightLeft(root);
    }
    return root;
}
int main()
{
    
    
    int N, val;
    scanf("%d", &N);
    Node *root = NULL;
    for(int i=0; i<N; i++)
    {
    
    
        scanf("%d", &val);
        root = insert(root, val);
    }
    printf("%d", root->key);
    return 0;
}

Je suppose que tu aimes

Origine blog.csdn.net/weixin_42393947/article/details/108704953
conseillé
Classement