0% found this document useful (0 votes)
7 views6 pages

Splay

The document contains C++ code for implementing various types of binary trees, including splay trees and treaps. It provides functions for inserting, finding, and removing nodes, as well as performing rotations to maintain tree properties. The main function demonstrates the usage of these data structures by reading input values and executing operations based on user input.

Uploaded by

dingtaodtdt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Splay

The document contains C++ code for implementing various types of binary trees, including splay trees and treaps. It provides functions for inserting, finding, and removing nodes, as well as performing rotations to maintain tree properties. The main function demonstrates the usage of these data structures by reading input values and executing operations based on user input.

Uploaded by

dingtaodtdt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

http://blog.csdn.

net/le_ballon_rouge/article/details/49741985
���Ӷ�֤��

#include<bits/stdc++.h>
using namespace std;
struct node
{
node *l,*r,*fa;
int key,tp;
node()
{
l=NULL;
r=NULL;
}
}*root;
void zig(node *p,node *fa,int t)
{
if(t==0) p->fa=fa->fa;
else p->fa=p;
p->tp=fa->tp;
fa->l=p->r,fa->fa=p,fa->tp=1;
if(fa->l!=NULL) fa->l->fa=fa,fa->l->tp=0;
p->r=fa;
}
void zag(node *p,node *fa,int t)
{
if(t==0) p->fa=fa->fa;
else p->fa=p;
p->fa=fa->fa,p->tp=fa->tp;
fa->r=p->l,fa->fa=p,fa->tp=0;
if(fa->r!=NULL) fa->r->fa=fa,fa->r->tp=1;
p->l=fa;
}
void zig_zig(node *p,node *fa,node *faa)
{
zig(fa,faa,0);
zig(p,fa,0);
}
void zag_zag(node *p,node *fa,node *faa)
{
zag(fa,faa,0);
zag(p,fa,0);
}
void zig_zag(node *p,node *fa,node *faa)
{
zig(p,fa,0);
zag(p,faa,0);
}
void zag_zig(node *p,node *fa,node *faa)
{
zag(p,fa,0);
zig(p,faa,0);
}
void _splay(node *p)
{
while(1){
if(p->tp==2) break ;
if(p->fa->tp==2){
if(p->tp==0) zig(p,p->fa,1);
else zag(p,p->fa,1);
//p=p->fa;
}
else{
if(p->tp==0&&p->fa->tp==0) zig_zig(p,p->fa,p->fa->fa);
else if(p->tp==1&&p->fa->tp==1) zag_zag(p,p->fa,p->fa->fa);
else if(p->tp==0&&p->fa->tp==1) zig_zag(p,p->fa,p->fa->fa);
else zag_zig(p,p->fa,p->fa->fa);
//p=p->fa->fa;
}
}
root=p;
}
void _insert(node *rt,int k)
{
node *p=rt;
while(1){
if(k<p->key){
if(p->l==NULL){
p->l=new node();
p->l->fa=p;
p=p->l;
p->key=k,p->tp=0;
_splay(p);
return ;
}
else{
p=p->l;
}
}
else{
if(p->r==NULL){
p->r=new node();
p->r->fa=p;
p=p->r;
p->key=k,p->tp=1;
_splay(p);
return ;
}
else{
p=p->r;
}
}
}
}
bool _find(node *rt,int k)
{
node *p=rt;
while(1){
if(p==NULL) return 0;
if(p->key==k){
_splay(p);
return 1;
}
if(p->key>k) p=p->l;
else p=p->r;
}
}
int main()
{
int n,x;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&x);
if(i==0){
root=new node();
root->key=x,root->tp=2,root->fa=root;
}
else _insert(root,x);
}
while(~scanf("%d",&x)){
printf("%d\n",_find(root,x));
}
return 0;
}

#include<bits/stdc++.h>

using namespace std;


struct node
{
node *p,*s[2];
int key,size;
node(){
p=s[0]=s[1]=NULL;
size=1,key=0;
}
node(int _key):key(_key){
p=s[0]=s[1]=NULL;
size=1;
}
bool getlr(){
return p->s[1]==this;
}
node *link(int w,node *p){
s[w]=p;
if(p) p->p=this;
return this;
}
void update(){
size=1+(s[0]?s[0]->size:0)+(s[1]?s[1]->size:0);
}
};
node *root;
void rot(node *p)
{
node *q=p->p->p;
p->getlr()?p->link(0,p->p->link(1,p->s[0])) : p->link(1,p->p->link(0,p->s[1]));
p->p->update();
if(q) q->link(q->s[1]==p->p,p);
else{
p->p=0;
root=p;
}
}
void splay(node *p,node *tar)
{
while(p->p!=tar&&p->p->p!=tar){
p->getlr()==p->p->getlr()?(rot(p->p),rot(p)):(rot(p),rot(p));
}
if(p->p) rot(p);
p->update();
}
void _insert(int k)
{
node *p=root,*q=NULL;
while(p){
//p->push_down();
q=p;
p=p->s[p->key<k];
}
p=new node(k);
if(!q){
root=p;
return ;
}
q->link(q->key<k,p);
q->update();
splay(p,0);
}
node *_find(int k)
{
node *p=root;
int w=p->s[0]?p->s[0]->size:0;
while(w+1!=k){
if(w>=k) p=p->s[0];
else{
k-=w+1;
p=p->s[1];
}
w=p->s[0]?p->s[0]->size:0;
}
splay(p,0);
return p;
}

int main()
{
int t;
root=NULL;
while(~scanf("%d",&t)){
_insert(t);
}
while(~scanf("%d",&t)){
printf("%d\n",_find(t)->key);
}
return 0;
}

https://wenku.baidu.com/view/7857b870aaea998fcc220ed8.html
#include<bits/stdc++.h>

using namespace std;


struct node
{
node *ch[2];
int r,v;
int cmp(int x){
if(x==v) return -1;
return x<v?0:1;
}
};
node *_newnode(int x)
{
node *p=new node();
p->v=x,p->r=rand(),p->ch[0]=p->ch[1]=NULL;
return p;
}
void _rotate(node* &o,int d)
{
node *k=o->ch[d^1];
o->ch[d^1]=k->ch[d];
k->ch[d]=o;
o=k;
}
void _insert(node* &o,int x)
{
if(o==NULL){
o=_newnode(x);
}
else{
int d=o->cmp(x);
_insert(o->ch[d],x);
if(o->ch[d]->r>o->r){
_rotate(o,d^1);
}
}
}
void _remove(node* &o,int x)
{
int d=o->cmp(x);
if(d==-1){
if(o->ch[0]==NULL) o=o->ch[1];
else if(o->ch[1]==NULL) o=o->ch[0];
else{
int d2=o->ch[0]->r > o->ch[1]->r;
_rotate(o,d2);
_remove(o->ch[d2],x);
}
}
else{
_remove(o->ch[d],x);
}
}
int _find(node* o,int x)
{
while(o!=NULL){
int d=o->cmp(x);
if(d==-1) return 1;
else o=o->ch[d];
}
return 0;
}
int main()
{

You might also like