PAT Grade A 1066 Root des AVL-Baums (25 Punkte) | C ++ - Implementierung

1. Titelbeschreibung

Ursprünglicher Titellink
Fügen Sie hier eine Bildbeschreibung ein

Eingabespezifikation:

Jede Eingabedatei enthält einen Testfall. Für jeden Fall enthält die erste Zeile eine positive ganze Zahl N (≤ 20), die die Gesamtzahl der einzufügenden Schlüssel darstellt. Dann werden in der nächsten Zeile N verschiedene Ganzzahlschlüssel angegeben. Alle Zahlen in einer Zeile sind durch ein Leerzeichen getrennt.

Ausgabespezifikation:

Drucken Sie für jeden Testfall den Stamm des resultierenden AVL-Baums in einer Zeile.

Beispiel Eingabe 1:

5
88 70 61 96 120

Beispielausgabe 1:

70

Beispiel Eingabe 2:

7
88 70 61 96 120 90 65

Beispielausgabe 2:

88

Zwei Ideen zur Problemlösung

Diese Frage ist eine relativ vollständige AVL-Baumfrage, die im Wesentlichen alle Operationen des AVL-Baums umfasst und sich nach dem Erlernen des AVL-Baums sehr gut für die Konsolidierung eignet. Spezifische Informationen finden Sie in der Erläuterung zum AVL-Baum in "Algorithm Notes".

Drei, AC-Code

#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;
}

Ich denke du magst

Origin blog.csdn.net/weixin_42393947/article/details/108704953
Empfohlen
Rangfolge