data_t bst_search(bst_t bst, index_t index) { if (bst_type(bst) == isNotEmpty) { switch (index_compare(index, bst)) { case EQ: return (pair_snd(bst->pair)); break; case LT: return (bst_search(bst->izq, index)); break; case GT: return (bst_search(bst->der, index)); break; } } return (NULL); }
/* Finds the node with key qKey * This is the essence of binary search trees */ bt_cursor bst_search(bt_cursor cur, key_t qKey) { if(KEY_EQUAL(cur->data.key, qKey) || !bt_isInternal(*cur)) return cur; if(KEY_GREATER(cur->data.key, qKey)) return bst_search(cur->left, qKey); return bst_search(cur->right, qKey); }
struct bst *bst_search(struct bst *t, char e) { if (t == NULL) return NULL; if (t->data == e) return t; if (t->data > e) return bst_search(t->lchild, e); return bst_search(t->rchild, e); }
node_t *bst_search(node_t *n, unsigned int value) { if (!n) return NULL; if (value < n->value) bst_search(n->children[0], value); if (value > n->value) bst_search(n->children[1], value); return n; }
bst_node bst_search(bst_node *tree, double value){ if(*tree == NULL) return NULL; bst_node current = *tree; if(current->value == value){ return current; }else if(value > current->value){ return bst_search(¤t->right, value); }else{ return bst_search(¤t->left, value); } }
int bst_search(bst b, char *str){ if (b == NULL){ return 0; } else if (strcmp(str, b->key)==0){ return 1; } else if (strcmp(str, b->key)<0){ return bst_search(b->left, str); } else if (strcmp(str, b->key)>0){ return bst_search(b->right, str); } return 0; }
data_t bst_search(bst_t bst, index_t index) { data_t data = NULL; if (bst != NULL) { if (index_is_equal(index, pair_fst(bst->pair))) { data = pair_snd(bst->pair); } else { if (index_is_less_than(index, pair_fst(bst->pair))) { data = bst_search(bst->left, index); } else { data = bst_search(bst->right, index); } } } return (data); }
void *bst_search (t_bst_tree *t, void *elem, t_cmp f) { int cmp; if (!t) return (NULL); cmp = f(elem, t->data); if (!cmp) return (t->data); else if (cmp < 0) return (bst_search(t->left, elem, f)); else return (bst_search(t->right, elem, f)); }
/* Return NULL if nothing is found, else returns said vcard */ vcard *bst_search(bst *t, char *cnet, int *n_comparisons) { if(t == NULL) return NULL; (*n_comparisons)++; int cmp = strcmp(cnet, t -> c -> cnet); if(cmp == 0) return t -> c; if(cmp < 0) return bst_search(t -> lsub, cnet, n_comparisons); return bst_search(t -> rsub, cnet, n_comparisons); }
void dosearch(bst b, char *key) { if (bst_search(b, key) == 0) { printf("%s -- not found\n", key); } else { printf("%s -- found\n", key); } }
bstnode * bst_search(bstnode *root, char * key) { int cmp = 0; if (root == NULL) return NULL; cmp = strcmp(root->str, key); if (cmp > 0) return bst_search(root->left, key); else if (cmp < 0) return bst_search(root->right, key); else return root; }
bstnode * bst_update(bstnode *root, char * key, void * val, size_t size) { bstnode * n = NULL; n = bst_search(root, key); if (n != NULL) n = _update(n, key, val, size); return n; }
int bst_delete(bstree **t, bskey key) { bstree *p = NULL; if (t == NULL && bst_search(*t, key, NULL, &p) == 0) { return 1; } return 0; }
int search(bst * h, char * str){ bst_ret ret; searchTime.start(); ret = bst_search(h, str); searchTime.stop(); if(ret == bst_Found) return OP_OK; return NOT_FOUND; }
void bst_put(key_t nKey, int nValue) { struct generic_data *n_entry = malloc(sizeof(*n_entry)); n_entry->key = nKey; n_entry->value = nValue; bt_cursor n_pos = bst_search(t->root, nKey); if(KEY_EQUAL(n_pos.data.key, nKey)) n_pos->data.value = nValue; else if(KEY_GREATER(n_pos.data.key, nKey)) bt_set_left(*n_pos, *n_entry); else bt_set_right(*n_pos, *n_entry); }
def_t dict_search(dict_t dict, word_t word) { index_t index = index_from_string(word); assert(dict != NULL && word != NULL && dict_exists(dict, word)); def_t result = NULL; result = data_to_string(bst_search(dict->data, index)); free(index); index = NULL; assert(result != NULL); return result; }
/* Creates a new node and inserts it in the tree specified by root * Arguments: the tree, a comparison function and the data the node will contain * If NULL is passed instead of the comparison function it will blindly insert * the data (Not taking duplicates into consideration, etc) */ bst_node* bst_insert(bst_node* root, uint64_t key, int (*compare)(list_node*,void*), void *data) { if (root == NULL || data == NULL) return NULL; // We return NULL because we won't insert any data anyway bst_node** node = bst_search(&root, key); if (*node == NULL) { *node = bst_new_node(key, data); } else if (list_find((*node)->list, compare, data) == NULL) { list_insert((*node)->list, data); } return *node; }
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 ); }
bool dict_exists(dict_t dict, word_t word) { /*Precondition verification*/ assert(dict != NULL); assert(word != NULL); index_t index = index_from_string(word); data_t data = bst_search(dict->data, index); index_destroy(index); return (data != NULL); }
/* Inserts an already created node in a tree * Arguments: The tree to insert to and the node to insert */ bst_node *bst_insert_node(bst_node* root, bst_node* node) { if (root == NULL || node == NULL) return NULL; bst_node** s_node = bst_search(&root, node->key); if (*s_node == NULL) { *s_node = node; return *s_node; } /*else if (list_merge((*s_node)->list, compare, node->list) > 0) { return *s_node; }*/ return NULL; }
/* Although add and set could be `#define'd, you wouldn't be able to use * pointers to functions. */ static bstnode * _set(bstnode *root, char * key, void * val, size_t size, int act) { bstnode * n = NULL; n = bst_search(root, key); if (n == NULL) { n = new_bstnode(key, val, size); if (n != NULL) { insert(root, n); } } else { n = (act == SET) ? _update(n, key, val, size) : NULL; } return n; }
bool bst_search(bst_node_t *root, bst_elem_t elem) { bst_node_t *branch; if (elem < root->elem) { branch = root->left; } else if (elem > root->elem) { branch = root->right; } else { return true; } if (branch == NULL) { return false; } else { return bst_search(branch, elem); } }
bool dict_exists(dict_t dict, word_t word) { assert(dict != NULL && word != NULL); index_t index = index_from_string(word); data_t def = NULL; def = bst_search(dict->data, index); bool result = false; if (NULL != def) { result = true; } free(index); index = NULL; def = NULL; return result; }
def_t dict_search(dict_t dict, word_t word) { /*Precondition verification*/ assert(dict != NULL); assert(word != NULL); assert( dict_exists(dict, word) ); index_t index = index_from_string(word); data_t data = bst_search(dict->data, index); def_t definicion = data_to_string(data); index_destroy(index); /*Postcondition verification*/ assert(definicion != NULL); return (definicion); }
int bst_insert(bstree**t, bskey key) { bstree *p = NULL; bstree *s = NULL; if (t != NULL && bst_search(*t, key, NULL, &p) > 0) { return 0; } s = bst_new_node(key); if (s == NULL) return 0; if (!p) *t=s; else if (key < p->key) p->l=s; else if (key > p->key) p->r=s; return 1; }
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; }
int main() { node *root; root = insert_node(NULL, 5); insert_node(root, 34); insert_node(root, 23); insert_node(root, 54); insert_node(root, 1); insert_node(root, 25); insert_node(root, 12); insert_node(root, 18); insert_node(root, 5); insert_node(root, 13); insert_node(root, 13); insert_node(root, 29); //in_order(root); /* if (bst_search(root, 15)) { printf(" found\n"); }else { printf("not found\n"); } printf("min node = %d\n", min_value_node(bst_search(root, 23))->value); printf("min node = %d\n", min_value_node(bst_search(root, 23))->value); printf("max node = %d\n", max_value_node(root)->value); printf("pre node = %d\n", node_secessor(root, bst_search(root, 23))->value); printf("pre node = %d\n", node_secessor(root, bst_search(root, 5))->value); printf("pre node = %d\n", node_secessor(root, bst_search(root, 1))->value); */ delete_node(root, bst_search(root, 54)); in_order(root); }
bst_node_t *bst_remove(bst_t *bst, int key) { bst_node_t *node = bst_search(bst, key); if (NULL_NODE == node) return node; return bst_remove_node(bst, node); }
int bst_lookup (t_bst_tree *t, void *elem, t_cmp f) { return (bst_search(t, elem, f) != NULL); }
void *bbst_search(bbst *bb, void *elem_addr) { bst *b = &bb->b; return bst_search(b, elem_addr); }