强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬
本文由参考于柳神博客写成
柳神的博客,这个可以搜索文章
柳神的个人博客,这个没有广告,但是不能搜索
还有就是非常非常有用的 算法笔记 全名是
算法笔记 上级训练实战指南 //这本都是PTA的题解
算法笔记
PS 今天也要加油鸭
题目原文
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.
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
生词如下:
illustrate 说明
PS:这题我还是看懂了的
题目大意:
就是给你一串插入序列,然后问你这个序列,当你把他调整为AVL树的时候
树的根是多少
思路如下:
算法笔记上的模板一套就OK
代码如下:
#include<iostream>
#include<vector>
using namespace std;
struct node {
int v, height; //v为结点地址,height为当前子树高度
node* lchild, * rchild; //左右孩子的结点地址
};
node* newNode(int v) {
node* Node = new node; //申请一个node型变量的地址空间
Node->v = v; //结点权值为v
Node->height = 1; //结点高度初始为1
Node->lchild = Node->rchild = NULL; //初始状态下没有左右孩子
return Node;
}
int getHeight(node* root) { //获取以root为根结点的子树的当前height
if (root == NULL) return 0; //空结点高度为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 L(node*& root) { //左旋
node* temp = root->rchild;
root->rchild = temp->lchild;
temp->lchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
}
void R(node*& root) {
node* temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
}
void insert(node*& root, int v) {
if (root == NULL) { //到达空结点
root = newNode(v);
return;
}
if (v < root->v) { //v比根结点的权值小
insert(root->lchild, v);
updateHeight(root); //更新树高
if (getBalanceFactor(root) == 2) {
if (getBalanceFactor(root->lchild) == 1) {
R(root);
}
else if (getBalanceFactor(root->lchild) == -1) {
L(root->lchild);
R(root);
}
}
}
else { //v比根结点的权值大
insert(root->rchild, v);
updateHeight(root);
if (getBalanceFactor(root) == -2) {
if (getBalanceFactor(root->rchild) == -1) {
L(root);
}
else if (getBalanceFactor(root->rchild) == 1) {
R(root->rchild);
L(root);
}
}
}
}
node* Create(int data[], int n) {
node* root = NULL;
for (int i = 0; i < n; ++i) insert(root, data[i]);
return root;
}
int main() {
int N, data[22];
node *root;
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
scanf("%d", &data[i]);
}
root=Create(data, N);
printf("%d", root->v);
return 0;
}
柳神的思路如下:
柳神也是套模板,这题没有什么太大的意思
柳神的代码如下:
#include <iostream>
using namespace std;
struct node {
int val;
struct node *left, *right;
};
node *rotateLeft(node *root) {
node *t = root->right;
root->right = t->left;
t->left = root;
return t;
}
node *rotateRight(node *root) {
node *t = root->left;
root->left = t->right;
t->right = root;
return t;
}
node *rotateLeftRight(node *root) {
root->left = rotateLeft(root->left);
return rotateRight(root);
}
node *rotateRightLeft(node *root) {
root->right = rotateRight(root->right);
return rotateLeft(root);
}
int getHeight(node *root) {
if(root == NULL) return 0;
return max(getHeight(root->left), getHeight(root->right)) + 1;
}
node *insert(node *root, int val) {
if(root == NULL) {
root = new node();
root->val = val;
root->left = root->right = NULL;
} else if(val < root->val) {
root->left = insert(root->left, val);
if(getHeight(root->left) - getHeight(root->right) == 2)
root = val < root->left->val ? rotateRight(root) : rotateLeftRight(root);
} else {
root->right = insert(root->right, val);
if(getHeight(root->left) - getHeight(root->right) == -2)
root = val > root->right->val ? 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->val);
return 0;
}
如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗
相信我,你也能变成光.
如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.