AVL模板
感谢此博客
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define de(x) cout << #x << " = " << x << endl
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 15;
struct AvlTree
{
int val;
int ch[2];
int hei, sz;
/*node( int _v, int _h, int _sz )
{
val = _v;
hei = _h;
sz = _sz;
ch[0] = ch[1] = 0;
}*/
};
AvlTree t[N<<2];
int cnt, rt;
void node( AvlTree &x, int v, int _h, int _sz )
{
x.val = v;
x.hei = _h;
x.sz = _sz;
x.ch[0] = x.ch[1] = 0;
}
int height( int x )
{
if ( !x ) return -1;
else
return t[x].hei;
}
int rotate( int x, int f )
{
int y = t[x].ch[f^1];
t[x].ch[f^1] = t[y].ch[f];
t[y].ch[f] = x;
t[x].hei = max( height(t[x].ch[0]), height(t[x].ch[1]) ) + 1;
t[y].hei = max( height(t[y].ch[0]), height(t[y].ch[1]) ) + 1;
return y;
}
int doubleL2R( int x )
{
t[x].ch[0] = rotate( t[x].ch[0], 0 );
return rotate( x, 1 );
}
int doubleR2L( int x )
{
t[x].ch[1] = rotate( t[x].ch[1], 1 );
return rotate( x, 0 );
}
void ins( int &x, int v )
{
if ( !x ) node( t[x=cnt++], v, 0, 0);
else if ( v < t[x].val )
{
ins( t[x].ch[0], v );
if ( height(t[x].ch[0]) - height(t[x].ch[1]) == 2 )
{
int f = v < t[t[x].ch[0]].val;
if ( f )
x = rotate( x, f );
else
x = doubleL2R( x );
}
}
else if ( v > t[x].val )
{
ins( t[x].ch[1], v );
if ( height(t[x].ch[1]) - height(t[x].ch[0]) == 2 )
{
int f = v > t[t[x].ch[1]].val;
if ( f )
x = rotate( x, f^1 );
else
x = doubleR2L( x );
}
}
t[x].hei = max( height(t[x].ch[0]), height(t[x].ch[1]) ) + 1;
}
void find( int x, int v )
{
if ( !x ) return ;
if ( t[x].val == v ) return ;
printf("%d ", t[x].val );
find( t[x].ch[ v > t[x].val ], v );
}
void init()
{
cnt = 1;
rt = 0;
node( t[rt], 0, 0, 0 );
}
int main()
{
int n;
init();
scanf("%d", &n);
for ( int i = 0; i < n; i ++ )
{
int op, now;
scanf("%d%d", &op, &now);
if ( op == 1 ) ins( rt, now );
else
{
find( rt, now );
printf("%d\n", now);
}
}
return 0;
}
AVL模板的更多相关文章
- PAT甲级1123 Is It a Complete AVL Tree【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- 数据结构图文解析之:AVL树详解及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- AVL树模板
///AVL树模板 typedef struct Node ///树的节点 { int val,data; int h; ///以当前结点为根结点的数的高度 int bf; ///平衡因子(左子树高度 ...
- 平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】
平衡树初阶——AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是 ...
- PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- C++模板实现的AVL树
1 AVL树的定义 AVL树是一种自平衡二叉排序树.它的特点是不论什么一个节点的左子树高度和右子树的高度差在-1,0,1三者之间. AVL树的不论什么一个子树都是AVL树. 2 AVL树的实现 AVL ...
- 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)
题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...
随机推荐
- Centos7编译4.7.2内核
由于想要编译kvm-kmod,编译的时候提示内核版本太低,所以就不得不升级下linux内核,目前最新版内核是4.7.2,编译内核并不是一件很难的事,但是这么久没编译过,还是有必要记录下这一过程. 首先 ...
- 基于rman的坏块恢复
转载请注明出处 http://blog.csdn.net/guoyjoe/article/details/30965303 实验过程例如以下: 1.使用rman备份全库 Recovery Mana ...
- 使用Echarts进行可视化的数据线呈现
由于游戏后台需要统计游戏玩家的支付情况,恰好那天看见同学群里聊天说到了Echarts,于是我就看了眼,一看,哟,还是百度的产品,看了文档,示例,确实很屌的样子啊,于是自己就开始试了,最终效果如下: 个 ...
- AngularJS 笔记系列(四)控制器和表达式
控制器:在 Angular 中控制器是一个函数,用来向作用域中添加额外的功能.我们用它来给作用域对象设置初始状态,并添加自定义行为. 使用方法: var app = angualr.module('a ...
- The Cheap KD 10 is my best shoe yet
10 years of anything is fairly huge Cheap KD 10, but adding something as great as Flyknit causes it ...
- SSH查看Linux系统是32位还是64位?
Linux下如何明确地查看操作系统的位数 如何知晓操作系统是32位还是64位?这里介绍一种简单的方式: [root@localhost mysql-5.1.57]# getconf LONG_BI ...
- 如何获知PHP程序占用多少内存(复制)
想要知道编写的 PHP 脚本需要占用多少内存么?很简单,直接使用 PHP 查看当前分配给 PHP 脚本的内存的函数 memory_get_usage() 就可以了 下面是使用示例: 复制代码 代码如下 ...
- Restful风格API
一:协议 API与用户的通信协议,总是使用HTTPS协议. 二:域名 应该尽量将API部署在专用域名之下. https://api.example.com 如果确定API很简单,不会有进一步扩展,可以 ...
- Java设计原则—单一职责原则(转)
定义: 应该有且仅有一个原因引起类的变更. There should never be more than one reason for a class to change. 优点: 1.类的复杂性降 ...
- 多路选择I/O
多路选择I/O提供另一种处理I/O的方法,相比于传统的I/O方法,这种方法更好,更具有效率.多路选择是一种充分利用系统时间的典型. 1.多路选择I/O的概念 当用户需要从网络设备上读数据时,会发生的读 ...