0001 #include <windows.h> 0002 #include "Splay_test.h" 0003 0004 void testSplay(Rank n) { 0005 Splay<Rank> splay; 0006 0007 //随机访问 0008 while (splay.size() < n) { 0009 Rank e = dice(n*3); 0010 switch (dice((Rank) 3)) { 0011 case 0: { //查找,成功率 <= 33.3% 0012 printf("Search"); print(e); printf("\n"); 0013 splay.search(e); 0014 break; 0015 } 0016 case 1: { //删除,成功率 <= 33.3% 0017 printf("Remove"); print(e); printf("\n"); 0018 if (splay.remove(e)) print(splay); 0019 break; 0020 } 0021 default: {//插入,成功率 == 100% 0022 printf("Insert"); print(e); printf("\n"); 0023 splay.insert(e); print(splay); 0024 break; 0025 } 0026 } //switch 0027 } //while 0028 while (splay.size() > 0) { 0029 Rank e = dice(n*3); 0030 printf("Remove"); print(e); 0031 splay.remove(e); 0032 } //while 0033 0034 //单调访问 0035 for (Rank i = 0; i < n; i++) splay.insert(i); print(splay); 0036 for (Rank i = 0; i < n; i++) { splay.search(i); print(splay); } 0037 } //课后:利用这一接口,针对不同分布的访问,验证课上关于Splay均摊性能的结论 0038 0039 int main(int argc, char* argv[]) { 0040 SetConsoleOutputCP(CP_UTF8); SetConsoleCP(CP_UTF8); 0041 if (3 > argc) { printf("Usage: %s <size> <tests>\a\a\n", argv[0]); return 1; } 0042 srand(31415926); //srand((uint32_t) time(nullptr)); //随机种子 0043 Rank s = atoi(argv[1]), t = atoi(argv[2]); 0044 for (Rank k = 0; k < t; k++) { 0045 printf("\n[Test #%d/%d]\n", k, t); 0046 testSplay(s); 0047 } 0048 return 0; 0049 }