"Data Structure" C + + code splay

Source: Internet
Author: User

Splay, stretch the tree. The reason why I first wrote this class is not very commonly used in data structure, because I like splay very much, I think this is a very beautiful and flexible balance tree. In this first statement, my extension tree is derived from CLJ Daniel, the basic good classmate can go to his blog to see his splay implementation template, my implementation only borrowed from the clj of the great God a little implementation skills. My blog "In the heart of the Bull list" in the CLJ great God's blog link.

There are many students may not understand splay thought, then you can go to see Sqybi article "The Magical Splay", baidu library can be searched. This article is the clearest one I have ever seen explaining splay. Inside the diagram and explanation are very good, the code is Pascal, but it doesn't matter, only C + + students can see my code.

In this first to explain the clj of the great God of this splay implementation tip: Make full use of C + + in the bool type of true and false values set to 1 and 0, the tree each node of the left and right child pointer is defined in the following way--node *son[2]. So, Son[0] is the left child, Son[1] is the right child. At this point, you can write the code of the left and right operations into a paragraph in the implementation, using a bool variable to differentiate it. Therefore, the code will be less than half!

I say that people are certainly confused, no nonsense, directly on the code, we have a closer look at the code to understand.

Title: Sjtuoj 1221.

Refreshing version: No. Because the Splay implementation code is still relatively long, so do not consider direct writing, the code will be very messy.

Class Implementation version:

/** Title: Sjtuoj 1221 * OJ Link:Http://acm.sjtu.edu.cn/OnlineJudge */#include<iostream>#include<fstream>#include<string>#include<cstdio>using namespacestd;Const intMinint=1<< to;Const intmaxint=minint-1;structdot{intc,num,size; Dot*son[2],*Up ; Dot (intValue=0) {c=value; num=size=1; son[0]=son[1]=up=0; } BOOL Get(Dot *lr) {returnson[1]==LR;} voidADD_UP (intN) { for(Dot *u= This; u;u=u->up) u->size+=N;} Dot* Born (intk,dot*LR) {Son[k]=LR;if(!LR)return  This; LR->up= This; ADD_UP (lr->size); return  This; } Dot* Kill (intk) {dot*LR=SON[K];if(!LR)return 0; SON[K]=0; lr->up=0; ADD_UP (-lr->size); returnLR; }};intVV (dot *u) {returnU?u->c:0; }intSS (dot *u) {returnU?u->size:0; }intNN (dot *u) {returnU?u->num:0; }classsplay{ Public: Dot*root,*min,*Max;Private:    voidZG (dot *x) {dot*y=x->up,*z=y->Up ; BOOLI= (z?z->Get(y):0),k=y->Get(x); if(z) z->Kill (i); X->born (!k,y->born (K,y->kill (k)->kill (!k)); if(z) z->born (i,x); if(Y==root) Root=x;//maintenance root, may also be insurance? no! this is useful!    }    voidSplay (dot *x,dot *up=0) {dot*y,*Z;  while(x->up!=Up ) {y=x->up;if(Y->up==up) {ZG (x); Break; } Z=y->up; ZG ((z->Get(y) ==y->Get(x))?y:x);        ZG (x); }    }    voidRecycle (DOT *p) {if(!p)return; Recycle (P->son[0]); Recycle (p->son[1]);    Delete p; } Dot* Next (Dot *p,BOOLk) {splay (P); Dot*u=p->Son[k];  while(U->son[!k]) u=u->son[!K]; returnu; } Public: Splay () {Min=root=NewDot (minint); max=Newdot (maxInt); Min->born (1, Max); }    intSize () {returnroot->size-2; } Dot* Find (intc) {dot*u=Root;  while(u&&u->c!=c) u=u->son[c>u->c]; returnu; }        voidInsert (intc) {BOOLK; Dot*u=0, *v=Root;  while(v&&v->c!=c) {u=v; K= (c>v->c); V=v->Son[k]; }        if(v) {++v->num; v->add_up (1); }ElseSplay (U->born (k,NewDot (c))Son[k]); }    voidDelete (intc) {dot*p=find (c), *l,*R; --p->num; P-&GT;ADD_UP (-1); if(p->num==0) {L=next (P,0); R=next (P,1); Splay (l);            Splay (R,root); Recycle (R->kill (0)); }    }    voidDelete (intClintCR) {Dot*l,*r,*l,*R; Insert (CL);        Insert (CR); L=find (CL); R=Find (CR); L=next (L,0); R=next (R,1); Splay (l);        Splay (R,root); Recycle (R->kill (0)); }    intFind_ith (intI,dot *u)//There are three situations where the order cannot be easily changed.    {        intL=ss (u->son[0]),mid=u->num; if(i<=l)returnFind_ith (i,u->son[0]); if(I<=l+mid)returnU->C; returnFind_ith (i-l-1, u->son[1]); }}; Splay A; //Create a splay tree called aintMain () {//subject, assuming that the problem data are within the range of Minint+1 ~ maxInt-1    intN,x,y; Cin>>N; stringorder;  for(intI=1; i<=n;++i) {cin>>order; if(order=="Insert") {cin>>x; A.insert (x); }        if(order=="Delete") {cin>>x; A.delete (x); }        if(order=="Delete_less_than") {cin>>y; A.delete (minint+1, Y1); }        if(order=="Delete_greater_than") {cin>>x; A.delete (x+1, maxint-1); }        if(order=="Delete_interval") {cin>>x>>y; A.delete (x+1, Y1); }        if(order=="Find") {cin>>x; cout<< (a.find (x)?"Y":"N") <<Endl;} if(order=="Find_ith") {cin>>x; if(A.size () <x) {cout<<"N"<<endl;Continue; } cout<<a.find_ith (x+1, A.root) <<Endl; }    }        return 0;}

"Data Structure" C + + code splay

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.