Ejemplo n.º 1
0
int main( )
{
	int		depth	   = 0;
	Bst		* bst	   = create_bst( show_string, NULL, strcmp );
	char	a[10][10]  = { "2", "8", "1", "3", "9", "4", "7", "5", "6", "0" };
	char	*b[10];
	int		i;
	int		ret;

	for( i = 0; i < 10; i++ )
	{
		b[i] = a[i];
	}

	bst_insert( &bst, b[0] );
	bst_insert( &bst, b[1] );
	bst_insert( &bst, b[2] );
	bst_insert( &bst, b[3] );
	bst_insert( &bst, b[4] );
	in_order( bst );
	ret = bst_search( bst, b[2] );
	printf( "search result %d\n", ret );

	ret = bst_delete( &bst, b[0] );
	printf( "0 in order after delete %d\n", ret );
	in_order( bst );

	ret = bst_delete( &bst, b[1] );
	printf( "1 in order after delete %d\n", ret );
	in_order( bst );

	ret = bst_delete( &bst, b[2] );
	printf( "2 in order after delete %d\n", ret );
	in_order( bst );
	printf("bst %p\n", bst);

	destroy_bst(&bst);
	
	printf("bst %p\n", bst);

	ret = bst_delete( &bst, b[3] );
	printf( "3 in order after delete %d\n", ret );
	in_order( bst );

	ret = bst_delete( &bst, b[4] );
	printf( "4 in order after delete %d\n", ret );

	in_order( bst );

}
Ejemplo n.º 2
0
void *
bstree_remove (bstree_table_t *tbl, void *key, int keylen)
{
        bst_entry_t         *entry = NULL;
        bst_entry_t         searchentry = {0, };
        void                    *dataref = NULL;

        if ((!tbl) || (!key))
                return NULL;


        searchentry.key = key;
        searchentry.keylen = keylen;

        LOCK (&tbl->tablelock);
        entry = bst_delete (tbl->table, &searchentry);
        UNLOCK (&tbl->tablelock);

        if (!entry)
                return NULL;
        // 内存要自己释放
        free (entry->key);
        dataref = entry->data;

        // 内存要自己释放 
        free(entry);

        // 数据返回给上层
        return dataref;
}
Ejemplo n.º 3
0
uint8_t inner_main_clean_up(int exit_code)
{
	static int max_exit_code;

	max_exit_code = BS_MAX(exit_code, max_exit_code);

	/*
	 * posix_soc_clean_up may not return if this is called from a SW thread,
	 * but instead it would get posix_exit() recalled again
	 * ASAP from the HW thread
	 */
	posix_soc_clean_up();

	hwll_terminate_simulation();
	nrf_hw_models_free_all();
	bs_dump_files_close_all();

	bs_clean_back_channels();

	u8_t bst_result = bst_delete();

	if (bst_result != 0U) {
		bs_trace_raw_time(2, "main: The TESTCASE FAILED with return "
				  "code %u\n", bst_result);
	}
	return BS_MAX(bst_result, max_exit_code);
}
Ejemplo n.º 4
0
Archivo: bst.c Proyecto: dxtr/bst
/* DELETE ALL THE THINGS!
 * Arguments: Quite obvious. The tree to destroy
 */
void bst_destroy(bst_node **node)
{
	if (node == NULL || *node == NULL) return;
	while (*node) {
		bst_delete(node);
	}
}
Ejemplo n.º 5
0
void *test(void *data) {
	fprintf(stderr, "Starting test\n");
	//get the per-thread data
    thread_data_t *d = (thread_data_t *)data;

    //place the thread on the apropriate cpu
    set_cpu(the_cores[d->id]);
    int op_count = 10000;

    ssalloc_init();
    bst_init_local(d->id);

    /* Wait on barrier */
    barrier_cross(d->barrier);

	int i;
	bool_t added;

	for ( i = 1; i <= op_count; i++){

		added = bst_insert(i, root, d->id);
		// fprintf(stderr, "[%d] Added %d? %d\n", d->id, i, added==TRUE);
		if (added == TRUE) {
			d->num_insert++;
		}
	}

	// printf("Root right node: %d", root->right->key);
	
	for ( i = 1; i <= op_count; i++){

		node_t* found = bst_find(i, root, d->id);
		// printf("Contains %d? %d\n", i, found==FOUND);
		if (found != NULL) {
			d->num_search ++;
		} 
	}

	for ( i = 1; i <= op_count; i++){

		bool_t removed = bst_delete(i, root, d->id);
		// printf("Removed %d? %d\n", i, removed==TRUE);
		if (removed == TRUE) {
			d->num_remove ++;
		}
	}

	// for ( i = 1; i < 10; i++){

	// 	bool_t found = bst_contains(i);
	// 	printf("Contains %d? %d\n", i, found==FOUND);
	// }


	return NULL;
}
Ejemplo n.º 6
0
int main()
{
	struct bstnode *bst = NULL;
	bst_insert(6, &bst);
	//bst_insert(6, &bst);
    bst_insert(2, &bst);
    bst_insert(7, &bst);
    bst_insert(9, &bst);
    bst_insert(8, &bst);
    bst_insert(1, &bst);
    bst_insert(4, &bst);
    bst_insert(3, &bst);
    bst_insert(5, &bst);
    printf("preorder: ");
    preorder(bst);
    printf("\n");
    printf("inorder: ");
    inorder(bst);
    printf("\n");
    printf("postorder: ");
    postorder(bst);
    printf("\n");
    struct bstnode *result = search(&bst, 10);
    if (result) {
        printf("found\n");
        
    }else
    {
        printf("not found\n");
    }
    deletebstnode(&bst, 6);
    
    printf("inorder: ");
    inorder(bst);
    printf("\n");
    
    deletebstnode(&bst, 4);
    
    printf("inorder: ");
    inorder(bst);
    printf("\n");
    deletebstnode(&bst, 7);
    
    printf("inorder: ");
    inorder(bst);
    printf("\n");
    
	deletebstnode(&bst, 3);
    
    printf("inorder: ");
    inorder(bst);
    printf("\n");
    
    bst_delete(bst);
	return 0;
}
bstree bst_delete(bstree bst, element_type e)
{
	if (bst == NULL) {
		donothing_warnning("element not found");
		return bst;
	} else if (bst->data > e) {
		bst->left = bst_delete(bst->left, e);
	} else if (bst->data < e) {
		bst->right = bst_delete(bst->right, e);
	} else {
		// bst->data == e
		if (bst->left == NULL && bst->right == NULL) {
			free(bst);
			return NULL;
		} else if (bst->left == NULL || bst->right == NULL) {
			node_ptr node = bst->left == NULL ? bst->right : bst->left;
			free(bst);
			return node;
		}
		bst->right = bst_delete_min(bst->right, &bst->data);
	}
	return bst;
}
Ejemplo n.º 8
0
void test_bst(test_balanced_t kind) {
    bstree_t *tree;
    bst_node_t *node;

    if (kind == BALANCED)
        fprintf(stderr, "Testing balanced tree\n");
    else if (kind == LEFT_HEAVY)
        fprintf(stderr, "Testing left-heavy tree\n");
    else if (kind == RIGHT_HEAVY)
        fprintf(stderr, "Testing right-heavy tree\n");

    tree = make_tree(kind, 0);
    ASSERT_TRUE(bst_find(tree, "AAA") != NULL,
                "bst_find: look up existing value (0)");
    ASSERT_TRUE(bst_find(tree, "BBB") != NULL,
                "bst_find: look up existing value (1)");
    ASSERT_TRUE(bst_find(tree, "CCC") != NULL,
                "bst_find: look up existing value (2)");
    ASSERT_TRUE(bst_find(tree, "DDD") == NULL,
                "bst_find: look up non-existing value");
    node = bst_insert(tree, strdup("DDD"));
    ASSERT_TRUE(bst_find(tree, "DDD") != NULL, "bst_insert: insert an element.");

    /* This should exercise each path when run for all of the balance kinds. */
    bst_delete(tree, "AAA");
    ASSERT_TRUE(bst_find(tree, "AAA") == NULL, "bst_delete: remove element A");
    bst_delete(tree, "BBB");
    ASSERT_TRUE(bst_find(tree, "BBB") == NULL, "bst_delete: remove element B");
    bst_delete(tree, "CCC");
    ASSERT_TRUE(bst_find(tree, "CCC") == NULL, "bst_delete: remove element C");
    bst_delete(tree, "DDD");
    ASSERT_TRUE(bst_find(tree, "DDD") == NULL, "bst_delete: remove element D");

    bst_destroy(tree);
    ASSERT_TRUE(tree->root == NULL, "bst_destroy: null out structure.");
    free(tree);
}
Ejemplo n.º 9
0
bst bst_delete(bst b, char *str) {
   if (0 == bst_search(b, str)) {
      return b;
   }
   else if (strcmp(str, b->key)==0){
      if (b->left == NULL && b->right != NULL){
         free(b->key);
         free(b);
         b = b->right;
      }
      else if (b->left != NULL && b->right == NULL){
         free(b->key);
         free(b);
         b = b->left;
      }
      else if (b->left == NULL && b->right == NULL){
         free(b->key);
         free(b);
         b = NULL;
      }
      else {
         bst temp  = b->right;
         while (temp->left != NULL){
            temp = temp->left;
         }
         b = temp;
         b = bst_delete(b, temp->key);
      }
   }
   else if (strcmp(str, b->key)<0){
      b->left = bst_delete(b->left, str);
   }
   else if (strcmp(str, b->key)>0){
      b->right = bst_delete(b->right, str);
   }
   return b;
}
Ejemplo n.º 10
0
bst_tree_t *bst_delete(bst_elem_t value, bst_tree_t *tree)
{
    struct bst_node_t *tmp_node;
    if (tree == NULL) {
        return NULL;
    } else if (value < tree->elem) {
        tree->left = bst_delete(value, tree->left);
    } else if (value > tree->elem) {
        tree->right = bst_delete(value, tree->right);
    } else if (tree->left && tree->right) {
        tmp_node = bst_find_min(tree->right);
        tree->elem = tmp_node->elem;
        tree->right = bst_delete(tree->elem, tree->right);
    } else {
        tmp_node = tree;
        if (tree->left == NULL) {
            tree = tree->right;
        } else if (tree->right == NULL) {
            tree = tree->left;
        }
        free(tmp_node);
    }
    return tree;
}
Ejemplo n.º 11
0
int main(void) {
    bst_tree_t *tree = NULL;
    tree = bst_insert(1, tree);
    tree = bst_insert(8, tree);
    tree = bst_insert(2, tree);
    tree = bst_insert(4, tree);
    tree = bst_insert(10, tree);
    tree = bst_insert(7, tree);
    bst_print(tree);
    printf("\n");
    tree = bst_delete(8, tree);
    bst_print(tree);
    printf("\n");
    bst_make_empty(tree);
}
Ejemplo n.º 12
0
struct server_child*
remove_child (int ourid)
{
    ASSERT (ourid > 0);
    
    /* This is used only to compare with and find the correct element */
    struct server_child temp;
    temp.ourid = ourid;

    pthread_mutex_lock (&index.lock);
    struct server_child* result =
        (struct server_child*) bst_delete (&index.tree, &temp);
    pthread_mutex_unlock (&index.lock);

    return result;
};
Ejemplo n.º 13
0
/* remove a key/value pair from a table */
void ht_delete(hashtbl_t * tbl, char *key) {
  unsigned long h;
  bstree_t *tree;
  bst_node_t *treenode;
  ht_elem_t key_elem;

  h = tbl->hash((unsigned char *) key) % tbl->arrsz;
  tree = tbl->arr[h];

  if (! tree)  /* A NULL slot means the key is unknown. */
    return;
  key_elem.key = key;
  treenode = bst_find(tree, &key_elem);
  if (treenode) {
    if (tbl->free)
      tbl->free(((ht_elem_t *) treenode->data)->data);
    bst_delete(tree, &key_elem);
    tbl->nelems--;
  }
}
Ejemplo n.º 14
0
Archivo: bst.c Proyecto: Cheyans/bst
int main() {
    bst_t *tree = (bst_t *) malloc(sizeof(bst_t));
    int *found = (int *) malloc(sizeof(int));
    printf("Test null tree find\n");
    printf("expected value\n");
    printf("found: 0, value: -1\n");
    printf("actual value\n");
    printf("found: %d, value: %d\n\n", *found, bst_find(NULL, 0, found));

    printf("Test only head find & insert\n");
    printf("expected value\n");
    printf("found: 1, value: 10\n");
    printf("actual value\n");
    bst_insert(tree, 10);
    printf("found: %d, value: %d\n\n", *found, bst_find(tree, 10, found));

    printf("Test level 1 node find & insert\n");
    printf("expected value\n");
    printf("found: 1, value: 15\n");
    printf("actual value\n");
    bst_insert(tree, 15);
    printf("found: %d, value: %d\n\n", *found, bst_find(tree, 15, found));

    printf("Test delete\n");
    printf("Current Tree\n");
    print_tree(tree);
    printf("Delete 10 Tree\n");
    bst_delete(tree, 10);
    print_tree(tree);
    printf("Delete 15 Tree\n");
    bst_delete(tree, 15);
    print_tree(tree);
    printf("Delete value nonexistent Tree\n");
    bst_delete(tree, -1);
    print_tree(tree);

    printf("\nLarge insert set\n");
    bst_insert(tree, 30);
    bst_insert(tree, 15);
    bst_insert(tree, 45);
    bst_insert(tree, 35);
    bst_insert(tree, 10);
    bst_insert(tree, 26);
    bst_insert(tree, 28);
    bst_insert(tree, 23);
    bst_insert(tree, 20);
    bst_insert(tree, 24);
    bst_insert(tree, 22);
    bst_insert(tree, 29);
    printf("Current Tree\n");
    print_tree(tree);

    printf("\nTest two child delete\n");
    printf("Delete 15 Tree\n");
    bst_delete(tree, 15);
    print_tree(tree);

    printf("\nTest one child delete\n");
    printf("Delete 28 Tree\n");
    bst_delete(tree, 28);
    print_tree(tree);

    printf("\nTest no child delete\n");
    printf("Delete 29 Tree\n");
    bst_delete(tree, 29);
    print_tree(tree);

    printf("\nTest delete root from large tree\n");
    printf("Delete 30 Tree\n");
    bst_delete(tree, 30);
    print_tree(tree);
    //gotta stay squeaky clean
    delete_tree(tree);
    free(found);
}