Пример #1
0
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);
}
Пример #2
0
void RedBlackHash :: insert(int k, int v)
{
    int pos;
    RedBlack *tree;

    pos = hashCode(k);
    tree = hash_table[pos];
    tree->insert(k, v);
}