示例#1
0
文件: hw6_2.c 项目: hyde1234r/hw6
int main() {
	struct avl_table* avl_tree;
	struct bst_table* bst_tree;
	struct rb_table* rb_tree;
	char* input_str[STR_NUM];
	char buf[BUF_SIZE];
	void** p[3];
	int str_length;
	
	avl_tree = avl_create( str_cmp , NULL, NULL) ;
	bst_tree = bst_create( str_cmp, NULL , NULL);
	rb_tree = rb_create(str_cmp, NULL , NULL);
	
	// input string
	for(int i=0;i<STR_NUM;i++){
		if(fgets(buf, BUF_SIZE , stdin)!= NULL) {
			str_length = strlen(buf)-1;
			input_str[i] = malloc(str_length*sizeof(char));
			strncpy(input_str[i],buf,str_length);
			//printf("%s\n",input_str[i]);
			p[0] = avl_probe( avl_tree , input_str[i]);
			p[1] = bst_probe( bst_tree , input_str[i]);
			p[2] = rb_probe( rb_tree , input_str[i]);
		}
	}
	print_avl(avl_tree->avl_root);
	printf("\n");
	print_bst(bst_tree->bst_root);
	printf("\n");
	print_rb(rb_tree->rb_root);
	printf("\n");
	return 0;
}
示例#2
0
int
rbthash_insert_entry (rbthash_table_t *tbl, rbthash_entry_t *entry)
{
        struct rbthash_bucket   *bucket = NULL;
        int                     ret = -1;

        if ((!tbl) || (!entry))
                return -1;

        bucket = rbthash_entry_bucket (tbl, entry);
        if (!bucket) {
                gf_log (GF_RBTHASH, GF_LOG_ERROR, "Failed to get bucket");
                goto err;
        }

        ret = 0;
        LOCK (&bucket->bucketlock);
        {
                if (!rb_probe (bucket->bucket, (void *)entry)) {
                        gf_log (GF_RBTHASH, GF_LOG_ERROR, "Failed to insert"
                                " entry");
                        ret = -1;
                }
        }
        UNLOCK (&bucket->bucketlock);

err:
        return ret;
}
示例#3
0
文件: hw6_2.c 项目: wheatdog/dsa
int main() 
{
    struct avl_table *avltree;
    struct bst_table *bsttree;
    struct rb_table *rbtree;

    avltree = avl_create(Compare_by_lexicographical_order, NULL, NULL);
    bsttree = bst_create(Compare_by_lexicographical_order, NULL, NULL);
    rbtree = rb_create(Compare_by_lexicographical_order, NULL, NULL);

    for (int i = 0; i < 32; i++)
    {
        struct word *element = 
            (struct word *)malloc(sizeof(struct word));

        ReadWord(element);

#ifdef DEBUG
        printf("Read in word: %s, length = %d\n", element->s, element->length);
#endif

        avl_probe(avltree, element);
        bst_probe(bsttree, element);
        rb_probe(rbtree, element);
    }

    preorder_avl(avltree->avl_root); puts("");
    preorder_bst(bsttree->bst_root); puts("");
    preorder_rb(rbtree->rb_root); puts("");
    return 0;
}
示例#4
0
/* Inserts |item| into |table|, replacing any duplicate item.
   Returns |NULL| if |item| was inserted without replacing a duplicate,
   or if a memory allocation error occurred.
   Otherwise, returns the item that was replaced. */
void *
rb_replace (struct rb_table *table, void *item)
{
    void **p = rb_probe (table, item);
    if (p == NULL || *p == item)
        return NULL;
    else
    {
        void *r = *p;
        *p = item;
        return r;
    }
}
示例#5
0
/* Attempts to insert |item| into |tree|.
   If |item| is inserted successfully, it is returned and |trav| is
   initialized to its location.
   If a duplicate is found, it is returned and |trav| is initialized to
   its location.  No replacement of the item occurs.
   If a memory allocation failure occurs, |NULL| is returned and |trav|
   is initialized to the null item. */
void *
rb_t_insert (struct rb_traverser *trav, struct rb_table *tree, void *item)
{
    void **p;

    assert (trav != NULL && tree != NULL && item != NULL);

    p = rb_probe (tree, item);
    if (p != NULL)
    {
        trav->rb_table = tree;
        trav->rb_node =
            ((struct rb_node *)
             ((char *) p - offsetof (struct rb_node, rb_data)));
        trav->rb_generation = tree->rb_generation - 1;
        return *p;
    }
示例#6
0
void BankRBTree::createAccount(const string& id, const string& passwd){
  //find account's position, if not exist return |NULL|
  string now_id = id;
  DataNode la = DataNode(&now_id, NULL);
  if(rb_find(rb_tree, &la) == NULL){
    string* new_id = new string(id);
    Account* new_ac = new Account(id, passwd);
    DataNode* data = new DataNode(new_id, new_ac);
    rb_probe(rb_tree, data);
    cout << "success\n";
  }

  else{
    cout << "ID " << id << " exists, ";
    // recommend 10 best account
    RecommendId best10;
    getRecommend(id, best10);
    cout << best10[0];
    for (int i = 1; i < 10; ++i)
      cout << "," <<  best10[i];
    cout << "\n";
  }
}
示例#7
0
/* Inserts |item| into |table|.
   Returns |NULL| if |item| was successfully inserted
   or if a memory allocation error occurred.
   Otherwise, returns the duplicate item. */
void *
rb_insert (struct rb_table *table, void *item)
{
    void **p = rb_probe (table, item);
    return p == NULL || *p == item ? NULL : *p;
}