PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree
题意:
构造AVL树,返回root点val。
思路:
了解AVL树的基本性质。
AVL树
ac代码:
C++
// pat1066.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
using namespace std;
struct AvlNode
{
int data;
AvlNode* left;
AvlNode* right;
int height;
AvlNode(int x) : data(x), left(NULL), right(NULL), height(0) {}
};
int height(AvlNode* root)
{
return root == NULL ? -1 : root->height;
}
void LLRotate(AvlNode*& root) //右单
{
AvlNode* temp = root->left;
root->left = temp->right;
temp->right = root;
temp->height = max(height(temp->left), height(temp->right)) + 1;
root->height = max(height(root->left), height(root->right)) + 1;
root = temp;
}
void RRRotate(AvlNode*& root) //左单
{
AvlNode* temp = root->right;
root->right = temp->left;
temp->left = root;
temp->height = max(height(temp->left), height(temp->right)) + 1;
root->height = max(height(root->left), height(root->right)) + 1;
root = temp;
}
void RLRotate(AvlNode*& root)
{
LLRotate(root->right);
RRRotate(root);
}
void LRRotate(AvlNode*& root)
{
RRRotate(root->left);
LLRotate(root);
}
void insert(const int& x, AvlNode*& root)
{
if (!root)
{
root = new AvlNode(x);
}
else if (x < root->data)
{
insert(x, root->left);
if (height(root->left) - height(root->right) == 2)
{
if (x < root->left->data) LLRotate(root);
else LRRotate(root);
}
}
else if (x > root->data)
{
insert(x, root->right);
if (height(root->right) - height(root->left) == 2)
{
if (x > root->right->data) RRRotate(root);
else RLRotate(root);
}
}
root->height = max(height(root->left), height(root->right)) + 1;
}
int main()
{
AvlNode* root = NULL;
int N;
int i;
int num[22];
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
scanf("%d", &(num[i]));
}
for (int i = 0; i < N; i++)
{
insert(num[i], root);
}
printf("%d", root->data);
return 0;
}
PAT甲级1066. Root of AVL Tree的更多相关文章
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***
1066 Root of AVL Tree (25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...
- PAT 甲级 1066 Root of AVL Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...
- PAT甲级——A1066 Root of AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]
题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...
- pat(A) 1066. Root of AVL Tree
代码: #include<iostream> #include<cstdio> #include<cmath> #include<stdlib.h> # ...
- PAT甲级:1066 Root of AVL Tree (25分)
PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...
- PAT 1066 Root of AVL Tree[AVL树][难]
1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...
- PTA (Advanced Level) 1066 Root of AVL Tree
Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...
随机推荐
- Python设计模式中单例模式的实现及在Tornado中的应用
单例模式的实现方式 将类实例绑定到类变量上 class Singleton(object): _instance = None def new(cls, *args): if not isinstan ...
- 137.Single Number II---位运算---《剑指offer》40
题目链接:https://leetcode.com/problems/single-number-ii/description/ 题目大意:给出一串数,每个数都出现三次,只有一个数只出现一次,把这个出 ...
- puppet practice
目标 试验环境有两台主机(VM)构成,一台是master,一台是agent,完成以下工作: 新建用户newuser; 安装 ubuntu-cloud-keyring package,更改文件/etc/ ...
- iOS通知中心
iOS通知中心 它是iOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信.通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信. 当通知中心接受到消息后会 ...
- WPF拖动DataGrid滚动条时内容混乱的解决方法
WPF拖动DataGrid滚动条时内容混乱的解决方法 在WPF中,如果DataGrid里使用了模板列,当拖动滚动条时,往往会出现列表内容显示混乱的情况.解决方法就是在Binding的时候给Update ...
- Netty并发优化之ExecutionHandler
上文<Netty框架入门>说到:如果业务处理handler耗时长,将严重影响可支持的并发数. 针对这一问题,经过学习,发现了可以使用ExecutionHandler来优化. 先来回顾一下没 ...
- vsftpd.conf 详解
//不允许匿名访问 anonymous_enable=NO //设定本地用户可以访问.注意:主要是为虚拟宿主用户,如果该项目设定为NO那么所有虚拟用户将无法访问 local_enable=YES // ...
- Mysql中的Btree与Hash索引
B-Tree 索引特征 B-Tree索引可以被用在像=,>,>=,<,<=和BETWEEN这些比较操作符上.而且还可以用于LIKE操作符,只要它的查询条件是一个不以通配符开头的 ...
- MySql学习笔记——触发器
今天又学习了一下mysql触发器的相关知识,对此做了一些笔记和总结. 定义及作用 触发器是一个被指定关联到一个表的数据对象,触发器不需要调用,当对一个表的特别事件出现时,它就会被激活.触发器的代码也是 ...
- CentOS7.6安装AMD显卡驱动
1.检查自己的显卡类型[root@localhost amdgpu-pro-18.50-708488-rhel-7.6]# lspci | grep -i vga03:00.0 VGA compati ...