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 ...
随机推荐
- Notice: Undefined index: wjs_cookie
w执行顺序. ok <!doctype html> <html> <head> <meta charset="UTF-8"> < ...
- 时间序列模式——ARIMA模型
ARIMA模型全称为自回归积分滑动平均模型(Autoregressive Integrated Moving Average Model,简记ARIMA),是由博克思(Box)和詹金斯(Jenkins ...
- BitTrex行情查看与技术指标系统
上个月的时候,向TradingView申请K线图行情插件,填了各种资料,被问了N多问题,结果却仍是不愿意提供插件给我们. 于是,我们自己开发了一个BitTre行情查看与技术指标系统, 这套系统被国内多 ...
- python基础-第九篇-9.1初了解Python线程、进程、协程
了解相关概念之前,我们先来看一张图 进程: 优点:同时利用多个cpu,能够同时进行多个操作 缺点:耗费资源(重新开辟内存空间) 线程: 优点:共享内存,IO操作时候,创造并发操作 缺点:抢占资源 通过 ...
- 网络编程 - 1.简单的套接字通信/2.加上通信循环/3.bug修复/4.加上链接循环/5.模拟ssh远程执行命令
1.简单的套接字通信 服务端 ''' 服务端 接电话 客户端 打电话 1.先启动服务端 2.服务端有两种套接字 1.phone 用来干接收链接的 2.conn 用来干收发消息的 ''' import ...
- point in polygon algorithm
Point-In-Polygon Algorithm — Determining Whether A Point Is Inside A Complex Polygon © 1998,2006,200 ...
- Laravel 5.3 使用内置的 Auth 组件实现多用户认证功能
https://blog.csdn.net/kevinbai_cn/article/details/54341779 概述 在开发中,我们经常会遇到多种类型的用户的认证问题,比如后台的管理员和前台的普 ...
- bat命令运行java程序
注意空格 本文主要介绍在window下bat批处理文件调用java的方法. @echo off echo 正在加密,请稍后....echo path:%~dp0 set base=%~dp0 set ...
- SQLSERVER2016 无域控AlwaysOn 实施步骤
SQLSERVER2016 无域控AlwaysOn 实施步骤 步骤: 一.安装3个windows server 2016系统 1)在3台机器建立具有administrators权限的相同账号密码,本例 ...
- Visual C++的DLL
动态链接库 (DLL) 是作为共享函数库的可执行文件. 动态链接提供了一种方法,使进程可以调用不属于其可执行代码的函数. 函数的可执行代码位于一个 DLL 中,该 DLL 包含一个或多个已被编译.链接 ...