void Splay::PrintLine(){ if(this==null) return; down(); l->PrintLine(); putchar(c); r->PrintLine(); }
void Splay::PrintLine(){ if(this==null) return; down(); l->PrintLine(); printf("%u",this-ele); if(--leftprint) putchar(' '); r->PrintLine(); }
void printtree(int tab) { if (this==null) return; int i; for (i=1;i<=tab;++i) printf(" "); if (i==tab+1) print(); l->printtree(tab+1); r->printtree(tab+1); for (i=1;i<=tab;++i) printf(" "); if (i==tab+1) printf("END\n"); }
Splay* del() { splay(); Splay *x=(l!=null?l->cutr():r!=null?r->cutl():null); x->l=l; x->l->fa=x; x->r=r; x->r->fa=x; delete this; return x; }
void rotate(Splay *x) { Splay *p = x->f; int d = x->dir(); if (!p->isr()) p->f->setCh(x, p->dir()); else x->f = p->f; p->setCh(x->ch[!d], d); x->setCh(p, !d); p->pull(); x->pull(); }
int main(){ //cout << RAND_MAX << endl; //freopen("b.in", "priority", stdin); int op; int cnt = 0; while (scanf("%d", &op) != EOF && op){ cnt++; //cout << "[" << ++cnt << "]" << endl; if (op == 1){ int K, P; scanf("%d%d", &K, &P); splay.insert(P, K); }else if (op == 2){ if (splay.empty()) printf("0\n"); else{ SplayNode* no = splay.findMax(); printf("%d\n", no->value); splay.remove(no); } }else{ if (splay.empty()) printf("0\n"); else{ SplayNode* no = splay.findMin(); printf("%d\n", no->value); splay.remove(no); } } } //for (int i = 0; i< cnt; i++){ //splay.findMin(), splay.findMax(); //} //cout << cnt << endl; }
template <typename T> void testSplayRandom ( int n ) { //随机访问测试 Splay<T> splay; while ( splay.size() < n ) { T e = dice ( ( T ) n * 3 ); //[0, 3n)范围内的e switch ( dice ( 3 ) ) { case 0: { //查找,成功率 <= 33.3% printf ( "Searching for " ); print ( e ); printf ( " ...\n" ); splay.search ( e ) ? printf ( "Found with" ), print ( splay.root() ), printf ( "\n" ) : printf ( "Not found\n" ); break; } case 1: { //删除,成功率 <= 33.3% printf ( "Removing " ); print ( e ); printf ( " ...\n" ); splay.remove ( e ) ? printf ( "Removal done\n" ) : print ( e ), printf ( " not exists\n" ); break; } default: {//插入,成功率 == 100% printf ( "Inserting " ); print ( e ); printf ( " ...\n" ); splay.insert ( e ); ( e == splay.root()->data ) ? printf ( "Insertion done with" ), print ( splay.root() ), printf ( "\n" ) : print ( e ), "duplicated"; break; } } //switch print ( splay ); //无论调用哪个接口,Splay都会自我调整形态,故需统一输出 } //while while ( splay.size() > 0 ) { T e = dice ( ( T ) n * 3 ); //[0, 3n)范围内的e printf ( "Removing " ); print ( e ); printf ( " ...\n" ); splay.remove ( e ) ? printf ( "Removal done\n" ), print ( splay ) : print ( e ), printf ( " not exists\n" ); } } //课后:利用这一接口,针对不同分布的访问,验证课上对Splay分摊分析的结论
void currect(){ if(pa!=null) pa->currect(),pa->down(); }
template <typename T> void testSplayPeriod ( int n ) { //周期性访问测试 Splay<T> splay; for ( int i = 0; i < n; i++ ) splay.insert ( ( T ) i ); print ( splay ); for ( int i = 0; i < n; i++ ) { splay.search ( ( T ) i ); print ( splay ); } }
void del(){ if(this==null) return; l->del(); r->del(); delete this; }