/* insert vcard into bst */ int bst_insert(bst *t, vcard *c) { if(t == NULL) { fprintf(stderr, "Tree is null.\n"); exit(1); } int cmp = strcmp(c -> cnet, t -> c -> cnet); if(cmp == 0) return 0; if(cmp < 0) { if(t -> lsub == NULL) { t -> lsub = bst_singleton(c); return 1; } return bst_insert(t -> lsub, c); } if(t -> rsub == NULL) { t -> rsub = bst_singleton(c); return 1; } return bst_insert(t -> rsub, c); }
struct treeNode *bst_insert(struct treeNode *tree, char *value) { /* * If the tree is empty, create a new node for the value and return * the pointer to this node. */ if ( tree == NULL ) { tree = (struct treeNode *) malloc( sizeof(struct treeNode) ) ; tree->left = NULL ; tree->right = NULL ; strcpy(&tree->value, &value) ; return tree ; } /* * Non-empty tree: compare the node's value to the argument. * - If the argument precedes the node's value, insert left. * - If the argument follows the nodes' value, insert right. * - If the argument equals the node's value, do nothing. */ int compare = strcmp( value, tree->value ) ; if ( compare < 0 ) { tree->left = bst_insert( tree->left, value ) ; } else if ( compare > 0 ) { tree->right = bst_insert( tree->right, value ) ; } return tree ; }
static NodeSP bst_insert(NodeSP root, NodeSP node) { if (!root) return node; if (!node) return root; if (root->data > node->data) { root->left_ = bst_insert(root->left_, node); return root; } else { root->right_ = bst_insert(root->right_, node); return root; } }
tnode *bst_insert(tnode *ptr, int key) { if (ptr == NULL) return new_tnode(key) ; if ( ptr->data > key ) ptr->left = bst_insert( ptr->left, key) ; else ptr->right = bst_insert( ptr->right, key) ; ptr->size++ ; // update size return ptr ; }
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; }
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 ); }
int bst_test() { bst bst; int iRet=1; int i=0; int arry_init[]={5,10,5,20,17,12,19,2}; LOGD("[F:%s, L:%d]\n", __FUNCTION__, __LINE__); //1. construct binary sort tree memset(&bst, 0, sizeof(bst)); bst_insert(&bst, arry_init, sizeof(arry_init)/sizeof(int)); LOGD("[F:%s, L:%d], bst size=%d\n", __FUNCTION__, __LINE__, bst.nBstSize); //2. search item iRet = bst_find(&bst, 17); //3. inorder traverse, print bst_inorderTraverse(&bst); //4. destruct binary sort tree bst_destruct(&bst); return iRet; }
int insert(bst * h, char * str){ bst_ret ret; char * new_str = (char *) malloc(strlen(str) + 1); if(new_str == NULL){ printf("ERROR: No memory for new string!\n"); exit(1); } strcpy(new_str, str); /* Insert into Hash Table */ insTime.start(); ret = bst_insert(h, new_str); insTime.stop(); if(ret == bst_NoMem){ printf("ERROR: No memory for new node!\n"); exit(1); } /* We could insert the set */ if(ret == bst_Ok) return OP_OK; /* We had already inserted the set */ if(ret == bst_PrevInserted){ free(new_str); return PREV_INSERTED; } /* The hash table is full */ return BST_FULL; }
void insert(struct node **root, int key) { struct node *z; z = bst_insert(root, key); z->color = RED; fix_rb_tree(root, z); }
int main( void ){ // Initialization int i,j; bst_t* tree = NULL; SystemInit(); init_scroll(); GLCD_Clear( Blue ); tree = malloc(sizeof(bst_t)); bst_init(tree); // Insertion for (i=0; i<100; i++) bst_insert(tree, value_array[i]); printf("0 | %d, %d\n",bst_min(tree), bst_max(tree)); // Deletion for (i=0; i<5; i++){ for (j=0; j<20; j++) bst_erase(tree, erase_array[i][j]); printf("%d | %d, %d\n", i+1, bst_min(tree), bst_max(tree)); } // Destruction bst_destroy(tree); while ( 1 ) { /* An emebedded system does not terminate... */ } }
SplayNode<T>* SplayTree<T>::bst_insert(SplayNode<T> * &node, T &key) { if (node == NULL) { node = new SplayNode<T>(); node ->val = key; } else if (node -> val < key) { return bst_insert(node->right, key); } else { return bst_insert(node->left, key); } }
int main(int argc, char const* argv[]) { /* create a new city bst */ bst_t* bst = bst_new(city_free, city_cmp_pol); /* read data and insert into bst */ int read = 0; char name_buf[256]; char country_buf[256]; int population; while (scanf("%[^,],%[^,],%d\n", name_buf, country_buf, &population) == 3) { city_t* new_city = city_new(name_buf, country_buf, population); int ret = bst_insert(bst, new_city); if (ret != BST_SUCCESS) { printf("error inserting city. duplicate?"); city_free(new_city); } read++; } printf("parsed %d cities\n", bst->num_elements); assert(bst->num_elements == read); /* print all cities */ bst_traverse(bst, BST_INORDER, city_print); /* find the largest */ node_t* largest_city_node = bst_max(bst); if (largest_city_node) { printf("largest city is:\n"); city_print(largest_city_node->data); } return 0; }
int main(){ int i; int a[] = {8, 2, 7, 9, 11, 3, 2, 6}; BST_PTR t = bst_create(); for(i=0; i<8; i++) bst_insert(t, a[i]); assert(bst_size(t) == 7); test_insert(t); test_contains(t); bst_inorder(t); bst_preorder(t); bst_postorder(t); bst_ith_smallest(t, 1) bst_size(t); bst_free(t); }
void t_avl() { int i; BST_PTR t = bst_create(); int *a = gen_random_arr(SAMPLE_SIZE); for(i=0; i<SAMPLE_SIZE; i++) bst_insert(t, i); printf("Height of root: %d\n", bst_height(t)); printf("Size of tree: %d\n", bst_size(t)); printf("Min elem: %d\n", bst_min(t)); printf("Max elem: %d\n", bst_max(t)); printf("Nearest elem: %d\n", bst_get_nearest(t,500)); printf("Num LEQ: %d\n", bst_num_leq(t,10)); for(i=0; i<SAMPLE_SIZE-1; i++) { bst_remove(t,a[i]); printf ("Delete %d\n", a[i]); } assert(bst_to_array(t)[0]==a[SAMPLE_SIZE-1]); printf("Height of root: %d\n", bst_height(t)); printf("Size of tree: %d\n", bst_size(t)); printf("Min elem: %d\n", bst_min(t)); printf("Max elem: %d\n", bst_max(t)); printf("Nearest elem: %d\n", bst_get_nearest(t,500)); printf("Num LEQ: %d\n", bst_num_leq(t,10)); free(a); bst_free(t); }
int main(int argc, char * argv[]) { FILE * fp = fopen(argv[1], "r"); if(fp!=NULL) { char currLine[17]; fgets(currLine, 17, fp); BST * progTree = NULL; unsigned short currNum; while(!feof(fp)){ currNum = convertToShort(currLine); progTree = bst_insert(progTree,currNum); fgets(currLine,17,fp); } bst_traverseInOrder(progTree); } fclose(fp); /*string to short assignments */ char zero[] = "0000000000000000"; char one[] = "0000000000000001"; char sixfivefivethreefive[] = "1111111111111111"; char onethreeseven[] = "0000000010001001"; /*BST assignment */ BST * myTree = NULL; /*string to short tests */ checkit_int(convertToShort(zero),0); checkit_int(convertToShort(one),1); checkit_int(convertToShort(sixfivefivethreefive),65535); checkit_int(convertToShort(onethreeseven), 137); /*bst tests */ checkit_int(bst_isValueInTree(myTree, 8),0); myTree = bst_insert(myTree, 8); checkit_int(bst_isValueInTree(myTree, 8),1); checkit_int(bst_isValueInTree(myTree, 5),0); checkit_int(bst_isValueInTree(myTree, 6),0); /*bst_traverseInOrder(myTree);*/ myTree = bst_insert(myTree, 1); myTree = bst_insert(myTree, 3); myTree = bst_insert(myTree,6); myTree = bst_insert(myTree,4); myTree = bst_insert(myTree,7); myTree = bst_insert(myTree,10); myTree = bst_insert(myTree,14); myTree = bst_insert(myTree,13); bst_traverseInOrder(myTree); checkit_int(bst_isValueInTree(myTree, 6),1); checkit_int(bst_isValueInTree(myTree, 13),1); return 0; }
void bst_insert(node_t *n, unsigned int value) { if (value < n->value) { if (!n->children[0]) n->children[0] = bst_alloc_node(n, value); else bst_insert(n->children[0], value); } else { if (!n->children[1]) n->children[1] = bst_alloc_node(n, value); else bst_insert(n->children[1], value); } }
int main(void) { bst b = bst_new(); printf("inserting d,b,f,a,c,e,g\n"); b = bst_insert(b, "d"); b = bst_insert(b, "b"); b = bst_insert(b, "f"); b = bst_insert(b, "a"); b = bst_insert(b, "c"); b = bst_insert(b, "e"); b = bst_insert(b, "g"); printf("inorder traversal\n"); bst_inorder(b, print_key); printf("preorder traversal\n"); bst_preorder(b, print_key); printf("searching\n"); dosearch(b, "f"); dosearch(b, "o"); dosearch(b, "x"); dosearch(b, "e"); dosearch(b, "d"); bst_free(b); return EXIT_SUCCESS; }
bst bst_insert(bst b, char *str){ if (b == NULL){ b = emalloc(sizeof *b); b->key = emalloc((strlen(str)+1) * sizeof str[0]); strcpy(b->key, str); return b; } else if (strcmp(str, b->key)==0){ return NULL; } else if (strcmp(str, b->key)<0){ b->left = bst_insert(b->left, str); } else if (strcmp(str, b->key)>0){ b->right = bst_insert(b->right, str); } return b; }
void add_child (struct server_child* child) { ASSERT (child != NULL); pthread_mutex_lock (&index.lock); bst_insert (&index.tree, child); pthread_mutex_unlock (&index.lock); };
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; }
/* Put a new key/value pair into a table. */ int ht_put(hashtbl_t * tbl, char *key, void *data) { unsigned long h; bst_node_t *treenode; ht_elem_t *elem, key_elem; key_elem.key = key; elem = mempool_alloc(tbl->ht_elem_pool, sizeof(ht_elem_t)); if (! elem) return -1; elem->key = mempool_alloc(tbl->key_pool, sizeof(char) * strlen(key) + 1); if (! elem->key) { /* elem leaks here, but we cannot free it from the mempool. */ return -1; } strcpy(elem->key, key); elem->data = data; h = tbl->hash((unsigned char *) elem->key) % tbl->arrsz; if (!tbl->arr[h]) { tbl->arr[h] = xmalloc(sizeof(bstree_t)); /* No free() fn for the bst, since its elements are in a mempool. */ bst_init(tbl->arr[h], ht_key_cmp, NULL); bst_insert(tbl->arr[h], elem); tbl->nelems++; return 0; } treenode = bst_find(tbl->arr[h], &key_elem); /* If no match is found, insert the new element and increase the counter. * Otherwise, replace the old data with the new. */ if (!treenode) { bst_insert(tbl->arr[h], elem); tbl->nelems++; } else { if (tbl->free) tbl->free(((ht_elem_t *) treenode->data)->data); treenode->data = elem; } return 0; }
// example of using assertions void test_insert(BST_PTR t){ int i, x; for(i=0; i<10; i++){ x = rand() % 100; bst_insert(t, x); assert(bst_contains(t,x)); } }
bstree bst_create(element_type a[], int size) { int i = 1; if (size <= 0) return NULL; bstree bst = bst_create_root(a[0]); for (; i < size; i++) { bst_insert(bst, a[i]); } return bst; }
void bst_insert(bstree bst, element_type e) { if (bst->data == e) { return; } else if (bst->data < e) { if (bst->right != NULL) { bst_insert(bst->right, e); } else { bstree node = bst_create_root(e); bst->right = node; } } else { if (bst->left != NULL) { bst_insert(bst->left, e); } else { bstree node = bst_create_root(e); bst->left = node; } } }
bst_tree_t *bst_insert( bst_elem_t value, bst_tree_t *tree) { if (tree == NULL) { tree = malloc(sizeof(bst_tree_t)); if (tree == NULL) { fprintf(stderr, "Out of space"); exit(1); } else { tree->elem = value; tree->left = tree->right = NULL; } } else if (value < tree->elem) { tree->left = bst_insert(value, tree->left); } else if (value > tree->elem){ tree->right = bst_insert(value, tree->right); } return tree; }
void read_file(const char *filename) { unsigned int value; FILE *fp = open_file(filename); fscanf(fp, "%x", &value); root = bst_alloc_node(NULL, value); while (fscanf(fp, "%x", &value) != EOF) bst_insert(root, value); }
static void reinsert(bst *b, sarray *sa, size_t m, size_t n) { if (m > n) return; size_t middle = (m + n) / 2; /* Middle index */ void *value = sarray_value(sa, middle); /* Middle node */ bst_insert(b, value); if (middle > 0) reinsert(b, sa, m, middle - 1); /* Insert the left nodes */ reinsert(b, sa, middle + 1, n); /* Insert the right nodes */ }
int t_bst_insert() { int i; int a[] = {8, 2, 6, 9, 11, 3, 7}; BST_PTR t = bst_create(); for(i=0; i<7; i++) bst_insert(t, a[i]); assert(bst_size(t) == 7); bst_free(t); }
int main() { bst B = bst_new(); int *x = malloc(sizeof(int)); *x = 1; bst_insert(B, (void*)x); bst copy = bst_dup(B); // Make sure copy is actually a completely different bst // than B in memory printf("orig adr - %p, new adr - %p\n", B, copy); // Make sure contents are the same printf("contents of orig bst\n"); print_inorder(B); printf("\n\n"); printf("contents of copy bst\n"); print_inorder(copy); int *b = malloc(sizeof(int)); int *c = malloc(sizeof(int)); *b = 2; *c = 3; bst_insert(B, (void*)b); bst_insert(B, (void*)c); bst copy2 = bst_dup(B); printf("orig adr - %p, new adr - %p\n", B, copy2); printf("contents of orig bst\n"); print_inorder(B); printf("\n\n"); printf("contents of copy2 bst\n"); print_inorder(copy2); free(x); free(b); free(c); return 0; }
struct bst *bst_init(int count, char *items) { int i; struct bst *t; if (count <= 0) return NULL; t = bst_create(items[0]); for (i = 1; i < count; i++) bst_insert(&t, items[i]); return t; }