感谢此博客

#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模板的更多相关文章

  1. PAT甲级1123 Is It a Complete AVL Tree【AVL树】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...

  2. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  3. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

  4. 数据结构图文解析之:AVL树详解及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  5. AVL树模板

    ///AVL树模板 typedef struct Node ///树的节点 { int val,data; int h; ///以当前结点为根结点的数的高度 int bf; ///平衡因子(左子树高度 ...

  6. 平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】

    平衡树初阶——AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是 ...

  7. PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. C++模板实现的AVL树

    1 AVL树的定义 AVL树是一种自平衡二叉排序树.它的特点是不论什么一个节点的左子树高度和右子树的高度差在-1,0,1三者之间. AVL树的不论什么一个子树都是AVL树. 2 AVL树的实现 AVL ...

  9. 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)

    题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...

随机推荐

  1. HTTP Transaction Delays

    w客户端.服务器超载 HTTP The Definitive Guide 与建立TCP连接以及传输请求和相应报文的时间相比,事务处理的时间是很短的.除非客户端或服务器超载或正在处理复杂的动态资源,否则 ...

  2. android开发笔记(一)Android studio 输入法

    以前都是用的时候查资料做些增添即可,现在下决心系统学习下. 首先发现developer.Android.com在开发工具上开始推出了 Android Studio了,不过他自己没有sdk manage ...

  3. d3.js:数据可视化利器之 修改文档:DOM操作符

    style: CSS样式操作符 style()操作符用来设置或获取选择集中各DOM元素的CSS样式: selection.style(name[,value[,priority]]) style()操 ...

  4. Unity3D优化技巧系列七

    笔者介绍:姜雪伟,IT公司技术合伙人.IT高级讲师,CSDN社区专家,特邀编辑.畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...

  5. postman 断言

    //断言 pm.test("message等于'操作成功'", function () { var jsonData = pm.response.json(); console.l ...

  6. bat批处理异备文件、压缩文件

    1.压缩本地文件,并把压缩后的文件复制到其他机器 net use Z: \\192.168.135.1\share_linux a123456! /user:chaoqun.guo set bath= ...

  7. JDK动态代理实现源码分析

    JDK动态代理实现方式 在Spring框架中经典的AOP就是通过动态代理来实现的,Spring分别采用了JDK的动态代理和Cglib动态代理,本文就来分析一下JDK是如何实现动态代理的. 在分析源码之 ...

  8. 浅谈Spark2.x中的Structured Streaming

    在Spark2.x中,Spark Streaming获得了比较全面的升级,称为Structured Streaming,和之前的很不同,功能更强大,效率更高,跟其他的组件整合性也更好. 连续应用程序c ...

  9. Spring的AOP实现

    内容详见切面编程系列 https://www.cnblogs.com/jiyukai/category/1265045.html.

  10. Look for this newest GS Jordan 6 Floral

    Named 'Bulls Over Broadway' and 'Gym Red', the most recent variation from the New Jordans 2015 is fo ...