PAT-ADVANCED1066——Root of AVL Tree
我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED
原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888
题目描述:
题目翻译:
AVL树是一棵自平衡的二分搜索树。在AVL树中,左右子树的高度差不会超过1;任何时刻如果左右子树的高度差超过了1,自平衡操作会使得其维持左右子树高度差不超过1这个性质。下图展示了旋转规则。
现在给你一串插入序列,你需要输出由该插入顺序构成的AVL树的根结点。
输入格式:
每个输入文件包含一个测试用例。在每个测试用例中,第一行有一个正整数N(<= 20)。第二行有N个不同的整数。一行中的所有数字由一个空格分隔。
输出格式:
对每个测试用例,输出该AVL树的根节点。
输入样例1:
5
88 70 61 96 120
输出样例1:
70
输入样例2:
7
88 70 61 96 120 90 65
输出样例2:
88
知识点:构建AVL树
思路一:按插入顺序构建AVL树(静态数组实现)
构建AVL树的思路和PAT-ADVANCED1123——Is It a Complete AVL Tree一模一样。
时间复杂度和空间复杂度均是O(N)。
C++代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node {
int data;
int height;
int lchild;
int rchild;
};
int N;
vector<int> input;
node Node[20];
int index = 0;
int newNode(int num);
int getHeight(int root);
int getBalanceFactor(int root);
void updateHeight(int root);
void leftTurn(int &root);
void rightTurn(int &root);
void insert(int &root, int num);
int create();
int main(){
cin >> N;
int num;
for(int i = 0; i < N; i++){
cin >> num;
input.push_back(num);
}
int root = create();
cout << Node[root].data << endl;
return 0;
}
int newNode(int num){
int root = index++;
Node[root].data = num;
Node[root].height = 1;
Node[root].lchild = Node[root].rchild = -1;
return root;
}
int getHeight(int root){
if(root == -1){
return 0;
}
return Node[root].height;
}
int getBalanceFactor(int root){
return getHeight(Node[root].lchild) - getHeight(Node[root].rchild);
}
void updateHeight(int root) {
Node[root].height = max(getHeight(Node[root].lchild), getHeight(Node[root].rchild)) + 1;
}
void leftTurn(int &root) {
int temp = Node[root].rchild;
Node[root].rchild = Node[temp].lchild;
Node[temp].lchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
}
void rightTurn(int &root) {
int temp = Node[root].lchild;
Node[root].lchild = Node[temp].rchild;
Node[temp].rchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
}
void insert(int &root, int num) {
if(root == -1){
root = newNode(num);
return;
}
if(num < Node[root].data){
insert(Node[root].lchild, num);
updateHeight(root);
if(getBalanceFactor(root) == 2){
if(getBalanceFactor(Node[root].lchild) == 1){
rightTurn(root);
}else if(getBalanceFactor(Node[root].lchild) == -1){
leftTurn(Node[root].lchild);
rightTurn(root);
}
}
}else{
insert(Node[root].rchild, num);
updateHeight(root);
if(getBalanceFactor(root) == -2){
if(getBalanceFactor(Node[root].rchild) == -1){
leftTurn(root);
}else if(getBalanceFactor(Node[root].rchild) == 1){
rightTurn(Node[root].rchild);
leftTurn(root);
}
}
}
}
int create(){
int root = -1;
for(int i = 0; i < N; i++){
insert(root, input[i]);
}
return root;
}
C++解题报告:
思路二:按插入顺序构建AVL树(指针实现)
构建AVL树的思路和PAT-ADVANCED1123——Is It a Complete AVL Tree一模一样。
时间复杂度和空间复杂度均是O(N)。
C++代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node {
int data;
int height;
node* lchild;
node* rchild;
};
int N;
vector<int> input;
node* newNode(int num);
int getHeight(node* root);
int getBalanceFactor(node* root);
void updateHeight(node* root);
void leftTurn(node* &root);
void rightTurn(node* &root);
void insert(node* &root, int num);
node* create();
int main(){
cin >> N;
int num;
for(int i = 0; i < N; i++){
cin >> num;
input.push_back(num);
}
node* root = create();
cout << root->data << endl;
return 0;
}
node* newNode(int num){
node* root = new node;
root->data = num;
root->height = 1;
root->lchild = root->rchild = NULL;
return root;
}
int getHeight(node* root){
if(root == NULL){
return 0;
}
return root->height;
}
int getBalanceFactor(node* root){
return getHeight(root->lchild) - getHeight(root->rchild);
}
void updateHeight(node* root){
root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
}
void leftTurn(node* &root){
node* temp = root->rchild;
root->rchild = temp->lchild;
temp->lchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
}
void rightTurn(node* &root){
node* temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
}
void insert(node* &root, int num){
if(root == NULL){
root = newNode(num);
return;
}
if(num < root->data){
insert(root->lchild, num);
updateHeight(root);
if(getBalanceFactor(root) == 2){
if(getBalanceFactor(root->lchild) == 1){
rightTurn(root);
}else if(getBalanceFactor(root->lchild) == -1){
leftTurn(root->lchild);
rightTurn(root);
}
}
}else{
insert(root->rchild, num);
updateHeight(root);
if(getBalanceFactor(root) == -2){
if(getBalanceFactor(root->rchild) == -1){
leftTurn(root);
}else if(getBalanceFactor(root->rchild) == 1){
rightTurn(root->rchild);
leftTurn(root);
}
}
}
}
node* create(){
node* root = NULL;
for(int i = 0; i < N; i++){
insert(root, input[i]);
}
return root;
}
C++解题报告: