int RedBlackHash :: search(int k) { int pos = hashCode(k); RedBlack *tree = hash_table[pos]; return tree->search(k); }
void RedBlackHash :: insert(int k, int v) { int pos; RedBlack *tree; pos = hashCode(k); tree = hash_table[pos]; tree->insert(k, v); }
void RedBlack::RBMenu() { using namespace std; RedBlack* tree = new RedBlack(); int choice, item; while (1) { cout << endl; cout<<"---------------------"<<endl; cout<<"Arbol Rojo-Negro"<<endl; cout<<"---------------------"<<endl; cout<<"\t1.Insertar elemento"<<endl; cout<<"\t2.Desplegar Arbol"<<endl; cout<<"\t3.Eliminar Elemento"<<endl; cout<<"\t4.Salir"<<endl; cout<<"\tIngrese su opcion: "; cin>>choice; switch(choice) { case 1: cout<<"Ingrese el valor deseado: "; cin>>item; tree->RedBlack::Insert(item); break; case 2: if (tree->root == NULL) { cout<<"Arbol vacio"<<endl; continue; } cout<<"Arbol Rojo-Negro:"<<endl; tree->RedBlack::Display(tree->root,0); break; case 3: cout<<"Ingrese el elemento a eliminar: "; cin>>item; tree->Delete(item); break; case 4: return; break; default: cout<<"Opcion Invalida"<<endl; } } }
void testRedBlack(int n) { RedBlack<T>* rb = new RedBlack<T>; while (rb->size() < n) { T e = dice((T)n*3); //[0, 3n)范围内的e switch (dice(6)) { case 0: { //查找(概率 = 1/6) printf("Searching for "); print(e); printf(" ...\n"); BinNodePosi(T) p = rb->search(e); p ? printf("Found with"), print(p), printf("\n") : printf("Not found\n"); break; } case 1: case 2: { //删除(概率 = 2/6) printf("Removing "); print(e); printf(" ...\n"); rb->remove(e) ? printf("Done\n"), print(rb) : printf("Not exists\n"); break; } default: { //插入(概率 = 3/6) printf("Inserting "); print(e); printf(" ...\n"); BinNodePosi(T) v = rb->insert(e); printf("Done with"), print(v), printf("\n"), print(rb); break; } } } while (rb->size() > 0) { T e = dice((T)n*3); //[0, 3n)范围内的e printf("Removing "); print(e); printf(" ...\n"); rb->remove(e) ? printf("Done\n"), print(rb) : printf("Not exists\n"); } release(rb); }
// BEGIN KAWIGIEDIT TESTING // Generated by KawigiEdit 2.1.4 (beta) modified by pivanof bool KawigiEdit_RunTest(int testNum, vector <int> p0, bool hasAnswer, int p1) { cout << "Test " << testNum << ": [" << "{"; for (int i = 0; int(p0.size()) > i; ++i) { if (i > 0) { cout << ","; } cout << p0[i]; } cout << "}"; cout << "]" << endl; RedBlack *obj; int answer; obj = new RedBlack(); clock_t startTime = clock(); answer = obj->numTwists(p0); clock_t endTime = clock(); delete obj; bool res; res = true; cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl; if (hasAnswer) { cout << "Desired answer:" << endl; cout << "\t" << p1 << endl; } cout << "Your answer:" << endl; cout << "\t" << answer << endl; if (hasAnswer) { res = answer == p1; } if (!res) { cout << "DOESN'T MATCH!!!!" << endl; } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) { cout << "FAIL the timeout" << endl; res = false; } else if (hasAnswer) { cout << "Match :-)" << endl; } else { cout << "OK, but is it right?" << endl; } cout << "" << endl; return res; }