splay-tree 是一个二叉排序树?
来源:互联网 发布:阿里云勒索病毒查杀 编辑:程序博客网 时间:2024/06/06 14:15
http://www.notonlysuccess.com/index.php/splay-tree/ (有ACM题目推荐)
http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html splay - tree (好长)
http://www.cnblogs.com/UnGeek/archive/2013/04/10/3013333.html 比较白话,就是字太小,还稍显啰嗦。。。o(╯□╰)o (includes demo)
http://www.link.cs.cmu.edu/cgi-bin/splay/splay-cgi.pl splay-tree demo (很有趣的实现)
http://hi.baidu.com/zab08/item/6125607df52c0d356dc37c56 左旋右旋
http://read.pudn.com/downloads99/ebook/404161/Splay.pdf 菜鸟笔记 人家无聊开始写的splaytree
http://www.cs.cmu.edu/~sleator/papers/self-adjusting.pdf 发明论文(看不懂)
http://www.cnblogs.com/kuangbin/archive/2013/03/17/2964546.html 跟着kuangbin写splaytree
注释不一定正确。。。o(╯□╰)o,可能错误的理解也写上去了。。。
#pragma comment(linker, "/STACK:1024000000,1024000000")#include <functional>#include <algorithm>#include <cstring>#include <vector>#include <queue>#include <cmath>#include <queue>using namespace std;#define CLR(c,v) memset(c,v,sizeof(c));typedef pair<int,int> pii;const int N = 1e5 + 10;const int INF = (0xff>>4);struct splaytree{int pre[N];// 父节点int key[N];// 权值int ch[N][2];// 左右孩子int root;// 根int tot;// 节点计数splaytree(){ tot=root=0; }void NewNode(int &r,int father,int x); // 树根取引用+父亲(默认0?)+新节点权void Update(int node,int father);int Insert(int value);void Rotate(int x,int type);void zig(int x);void zag(int x);void Splay(int r,int goal);int GetBigger(int node);int GetLess(int node);static const int left = 0;static const int right = 1;};typedef splaytree ST;void ST::NewNode(int &r,int father,int x){// 建立新节点r = ++tot;// 当前这个点的标号就是rpre[r] = father;// 当前点的父节点(默认0?)key[r] = x; // 当前点权ch[r][left] = 0;// 左孩子ch[r][right] = 0;// 右孩子}void ST::Rotate(int x,int type){// 旋转子树,type: 0左旋(zag),1右旋(zig)// 右旋的条件: 所旋转的结点x是其父亲结点y的左儿子, 左旋同理//3 1// / \zig=> / \// 1 40 3// / \<=zag / \//0 2 2 4int y = pre[x];// 保存父节点ch[y][!type] = ch[x][type];if(pre[y] != 0) // 当y不是root,x往上的时候要更新上面父节点的孩子ch[ pre[y] ][ ch[pre[y]][right]==y ] = x;pre[x] = pre[y];ch[x][type] = y;pre[y] = x;}void ST::zig(int x){// x作为root的左子树int y = pre[x];ch[y][left] = ch[x][right];// 若不是root还有考虑ch[ pre[y] ][ pre[y][1]==y ]ch[x][right] = y;pre[x] = pre[y];pre[y] = x;}void ST::zag(int x){// x作为root的右子树int y = pre[x];ch[y][right] = ch[x][left];// 若不是root还有考虑ch[ pre[y] ][ pre[y][1]==y ]ch[x][left] = y;pre[x] = pre[y];pre[y] = x;}void ST::Splay(int r, int goal){// 著名的伸展操作// 将节点r旋转到goal下面// 6 2// / \ / \// 4 7 1 4// / \Splay(6,0)<= / / \// 2 5=>Splay(2,0) 0 3 6// / \ / \// 1 3 5 7// ///0while(pre[r] != goal){if(pre[ pre[r] ] == goal){ // 如果下一步就是目标位置的下面this->Rotate(r, ch[pre[r]][left]==r);}else{int y = pre[r] , z = pre[y];int type = (ch[z][left] == y);if(ch[y][type] == r){ // 全速往上this->Rotate(r,!type);this->Rotate(r,type);}else{// 快到顶了this->Rotate(y,type);this->Rotate(r,type);}}}if(goal == 0) // 如果转到了rootroot = r;}int ST::Insert(int k){// 首先通过查找操作确定当前需要插入点的位置,// 然后判断插入其左子树或者右子树,// 最后不要忘记对插入点进行伸展操作。int r = root;while(ch[r][ key[r]<k ]){ // 找位置if(key[r] == k){ // 不重复的插入this->Splay(r,0);return 0;}r = ch[r][ key[r]<k ];}this->NewNode(ch[r][ key[r]<k ], r, k);this->Splay(ch[r][ key[r]<k ], 0);return k;}int ST::GetLess(int x){// 直接找左子树的最右边int tmin = ch[x][0];if(!tmin)return (1<<30);while(ch[tmin][1])tmin = ch[tmin][1];return key[x] - key[tmin];}int ST::GetBigger(int x){// 如果node 右子树为空,为什么不往上面找找?int tmax = ch[x][1];if(!tmax)return (1<<30);while(ch[tmax][0])tmax = ch[tmax][0];return key[tmax] - key[x];}int main(){freopen("in.txt","r",stdin);int n;while(~scanf("%d",&n)){ST s;int num,ans = 0;for(int i = 1 ; i <= n ; i++){if(scanf("%d",&num)==EOF) num = 0;if(i == 1){ // 如果第一个节点ans += num;s.NewNode(s.root,0,num);}else{if(s.Insert(num)==0)continue;int a = s.GetBigger( (int)s.root);int b = s.GetLess( (int)s.root);ans += min(a,b);}}printf("%d\n",ans);}return 0;}
- splay-tree 是一个二叉排序树?
- Splay Tree
- splay tree
- Splay Tree
- Splay Tree
- splay tree
- Splay tree
- splay tree
- Splay tree的splay操作
- Splay Tree(伸展树)
- Splay Tree 伸展树
- spoj 1470 //splay tree
- Splay-tree poj 3580
- 伸展树splay tree
- Splay Tree(伸展树)
- poj 3580 splay tree
- poj 2761 splay tree
- splay tree(伸展树)
- 将笔记本设置成WIFI热点--基于Win7系统
- HttpSession会话管理原理
- Nessus5.X部署手记
- 用命令查看占用侦听端口和应用程序的名字
- 【九的余数 105】
- splay-tree 是一个二叉排序树?
- Memcached分布式缓存,模拟Session
- 如何复制pdf的文字
- zedboard(zynq)怎么学,学习资料和推荐书籍
- 变量,常量,静态变量存储的位置
- super关键字
- BT5渗透之信息收集
- Qt学习笔记(一)
- 类与类之间的关系图(Class Diagram,UML图)