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分摊分析的结论
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 ); } }