Exemplo n.º 1
0
void btree_test() {
	struct btree *node1 = btree_create(NULL, TYPE_INT, 1);
	struct btree *node2 = btree_create(NULL, TYPE_INT, 2);
	struct btree *node3 = btree_create(NULL, TYPE_INT, 3);
	struct btree *node4 = btree_create(NULL, TYPE_INT, 4);
	struct btree *node5 = btree_create(NULL, TYPE_INT, 5);
	struct btree *node6 = btree_create(NULL, TYPE_INT, 6);
	struct btree *node7 = btree_create(NULL, TYPE_INT, 7);
	struct btree *node8 = btree_create(NULL, TYPE_INT, 8);
	node1->left = node2;
	node1->right = node3;
	node2->left = node4;
	node2->right = node5;
	node3->left = node6;
	node3->right = node7;
	node4->left = node8;

	fprintf(stdout, "preorder:\n");
	btree_preorder(node1);

	fprintf(stdout, "postorder:\n");
	btree_postorder(node1);

	fprintf(stdout, "inorder:\n");
	btree_inorder(node1);
	
	fprintf(stdout, "btree_search(node5, node8) = %d\n", btree_search(node5, node8));	
	fprintf(stdout, "btree_search(node4, node8) = %d\n", btree_search(node4, node8));	

	btree_destroy(node1);
}
Exemplo n.º 2
0
/**
* @brief test case for random number insert, search and delete.
*/
void testcase_for_random() {
	TREE* T = btree_create();
	int i = 0, n = 100;
	int _t = 0;
	srand(time(NULL));
	for (i = 0; i < n; i ++) {
		_t = rand() % n;
		printf("btree_insert(T, %d) = %d\n", _t, btree_insert(T, _t));
	}
	for (i = 0; i < n; i ++) {
		_t = rand() % n;
		printf("btree_search(T, %d) = %d\n", _t, btree_search(T, _t));
	}

	printf("btree_traveral: ");
	btree_traveral(T, printout_node);
	printf("\n");

	for (i = 0; i < n; i ++) {
		_t = rand() % n;
		printf("btree_delete(T, %d) = %d\n", _t, btree_delete(T, _t));
	}

	printf("btree_traveral: ");
	btree_traveral(T, printout_node);
	printf("\n");

	btree_destory(T);
}
Exemplo n.º 3
0
void *acl_btree_remove(ACL_BTREE *tree, unsigned int key)
{
	const char *myname = "acl_btree_remove";
	BTREE_NODE *x;
	void *data;

	btree_validate(tree);
	x = btree_search(tree->root, key);
	if (x == NULL) {
		acl_msg_error("%s(%d): Item not on tree - key %u\n",
			myname, __LINE__, key);
		return (NULL);
	}

	/* Note value that gets freed is not necessarily the the same
	 * as node that gets removed from tree since there is an
	 * optimization to avoid pointer updates in tree which means
	 * sometimes we just copy key and data from one node to another.  
	 */

	data = x->data;
	x = btree_delete_node(tree, x);
	acl_slice_free2(tree->slice, x);

	return (data);
}
Exemplo n.º 4
0
void *
btree_get_data(btree_t *root, int key)
{
    btree_t *node= NULL;
    node = btree_search(root, key);
    return (node && node->data) ? node->data : NULL;
}
Exemplo n.º 5
0
int
btree_remove(btree_t *tree, uint32_t key, void **data)
{
    btree_node_t *x;

    btree_validate(tree);
    x = btree_search(tree->root, key);
    if (x == NULL) {
        debug_msg("Item not on tree - key %ul\n", key);
        *data = NULL;
        return FALSE;
    }

    /* Note value that gets freed is not necessarily the the same
     * as node that gets removed from tree since there is an
     * optimization to avoid pointer updates in tree which means
     * sometimes we just copy key and data from one node to
     * another.
     */

    *data = x->data;
    x = btree_delete_node(tree, x);
    xfree(x);

    return TRUE;
}
Exemplo n.º 6
0
/* name here includes entity qual ... &/% */ 
Entity* get_ref_entity_def(Ctl* ctl, int parameter_entity, XString name,
                           int strip) {

   XString     name1, dummystring;
   Node       *defnode;
   LookupType  lu_type;
   XmlBTree    *tree;
   BTreeCtl    *treectl;
   uint16       index;

   lclmemcpy(&name1, &name, sizeof(XString));
  if(strip) {
     name1.loc += 1 + ctl->is_double; 
     name1.len -= 1 +ctl->is_double;
   }
   

   treectl = &ctl->btree_ctl; 
   tree = treectl->btree;
   if(parameter_entity) lu_type = lu_pe;
    else lu_type = lu_ge;
  
   index = btree_search(ctl, lu_type, &name1, &dummystring, 0);
   if(index == 0) return 0;

   defnode = tree[index].definition;
   assert(defnode->node_type == ENTITYDECL);
   return ( (Entity*)defnode->internal_rep);

 }
Exemplo n.º 7
0
int     btree_remove(node_bt *list, node_bt *node, int(*func)(void*,void*), void *key)
{
  node_bt *parent;
  node_bt **buff;
  
  if (list == node)
  {
    if (remove_root(list) == 0)
      return 0;
    else
      return -1;  
  }
  parent = btree_search(list, key, func);
  if (parent->left == node) 
  {
    buff = &parent->left; 
  }
  else 
  {
    buff = &parent->right;
  }
  
  remove_not_root(node, buff);
  
  return 0;
}
Exemplo n.º 8
0
/**
* @brief test case for single insert, search and delete method.
*/
void testcase_for_single() {
	TREE* T = btree_create();

	printf("btree_insert(T, 32) = %d\n", btree_insert(T, 32));

	printf("btree_search(T, 32) = %d\n", btree_search(T, 32));

	printf("btree_delete(T, 32) = %d\n", btree_delete(T, 32));

	printf("btree_search(T, 32) = %d\n", btree_search(T, 32));

	printf("btree_traveral: ");
	btree_traveral(T, printout_node);
	printf("\n");

	btree_destory(T);
}
Exemplo n.º 9
0
R_API void *btree_get(struct btree_node *proot, void *x, BTREE_CMP(cmp)) {
    struct btree_node *p = btree_search (proot, x, cmp, 0);
    if (p) {
        p->hits++;
        return p->data;
    }
    return NULL;
}
Exemplo n.º 10
0
R_API void *btree_search(struct btree_node *root, void *x, BTREE_CMP(cmp), int parent) {
    struct btree_node *p = NULL;

    if (root!=NULL) {
        if (cmp (x, root->data)<0)
            p = btree_search (root->left, x, cmp, parent);
        else if (cmp(x, root->data)>0)
            p = btree_search (root->right, x, cmp, parent);
        else p = root;
    }
    /* node found */
    if (p) {
        if (parent)
            return root;
        return p;
    }
    return NULL;
}
Exemplo n.º 11
0
R_API int btree_del(struct btree_node *proot, void *x, BTREE_CMP(cmp), BTREE_DEL(del)) {
    struct btree_node *p = btree_search (proot, x, cmp, 1);
    if (p) {
        // p->right =
        btree_remove (p->left, del);
        return R_TRUE;
    }
    return R_FALSE;
}
Exemplo n.º 12
0
/**
* @brief B tree delete method.
*
* @param T B tree.
* @param k the element to delete.
*
* @return return 1 means delete success, otherwise return 0 means not delete it
* as the element k is not include in the B tree.
*/
PUBLIC int btree_delete(TREE* T, ELEMENT k) {
	if (!btree_search(T, k)) { return 0; }
	_btree_delete(T->root, k);
	NODE* r = T->root;
	if (0 == r->n) {
		T->root = r->c[0];
		_btree_free_node(r);
	}
	return 1;
}
Exemplo n.º 13
0
tnode *btree_search(char *key, tnode *t){
  int cmp = 0;
  tnode *result = NULL;
  
  if(t == NULL){
    result = NULL;
  }else{
    cmp = strcmp(t->key, key);
    if(cmp == 0){
      result = t;
    }else if(cmp < 0){
      result = btree_search(key, t->left);
    }else{
      result = btree_search(key, t->right);
    }
  }

  return result;
}
Exemplo n.º 14
0
R_API bool btree_del(struct btree_node *proot, void *x, BTREE_CMP(cmp), BTREE_DEL(del)) {
	struct btree_node *p = btree_search (proot, x, cmp, 1);
	if (p) {
		// p->right = 
		btree_remove (p->left, del);
		p->left = NULL;
		return true;
	}
	return false;
}
Exemplo n.º 15
0
String btree_search(String key, Btree t)
{
  if (btree_isempty(t))
    return NULL;
  else if (strcmp(key, t->key) < 0){
    if (btree_isempty(t->left))
      return NULL;
    else
      return btree_search(key, t->left);
  }
  else if (strcmp(key, t->key) > 0){
    if (btree_isempty(t->right))
      return NULL;
    else
      return btree_search(key, t->right);
  }
  else
    return t->value;
}
Exemplo n.º 16
0
int main(int argc, char *argv[])
{
	btree_tree *tmp;
	FILE *f;
	char urlBuffer[2048], *data;
	uint64_t id;
	uint64_t i = 0, found = 0;
	int error;

	tmp = btree_open("tree2.mmap", &error);
	if (!tmp) {
		printf("Couldn't open tree, errno %d.\n", error);
		exit(1);
	}

	if (argc < 2) {
		printf("Please pass a text file via argv to search for in the btree\n");
		exit(1);	
	}

	f = fopen(argv[1], "r");
	if (!f) {
		printf("Couldn't open file %s, errno %d.\n", argv[1], errno);
		exit(1);
	}
	while (!feof(f)) {
		fgets(urlBuffer, 2048, f);
		data = strchr(urlBuffer, ' ');
		if (data) {
			data++;
			data[-1] = '\0';
			id = atoll(urlBuffer);
			found += btree_search(tmp, tmp->root, id);
			found += btree_search(tmp, tmp->root, id + 1);
			i++;
		}
	}
	printf("%lu %lu\n", found, i);

	btree_close(tmp);

	return 0;
}
Exemplo n.º 17
0
void *acl_btree_find(ACL_BTREE *tree, unsigned int key)
{
	BTREE_NODE *x;

	btree_validate(tree);
	x = btree_search(tree->root, key);
	if (x != NULL) {
		return (x->data);
	}
	return (NULL);
}
Exemplo n.º 18
0
int
btree_find(btree_t *tree, uint32_t key, void **d)
{
    btree_node_t *x;

    btree_validate(tree);
    x = btree_search(tree->root, key);
    if (x != NULL) {
        *d = x->data;
        return TRUE;
    }
    return FALSE;
}
Exemplo n.º 19
0
int main()
{
  Btree t = (struct tnode *)malloc(sizeof(struct tnode));
  t = btree_empty();
  String s = (char *)malloc(50);
  String a = (char *)malloc(50);
  String b = (char *)malloc(50);
  while (scanf("%49s ", s) != EOF){
    if (strcmp(s, "insert") == 0){
      scanf("%49s ", a);
      scanf("%49s\n", b);
      t = btree_insert(a, b, t);
    }
    else if (strcmp(s, "delete") == 0){
      scanf("%49s\n", a);
      t = btree_delete(a, t);
    }
    else if (strcmp(s, "search") == 0){
      scanf("%49s\n", a);
      if (btree_search(a, t) == NULL)
	printf("(not found)\n");
      else
	printf("%s\n", (btree_search(a, t)));
    }
    else if (strcmp(s, "quit") == 0)
      break;
    else{
      printf("(unknown command)\n");
    }
  }

  free_btree(t);
  free(a);
  free(b);
  free(s);

return 0;

}
void updateTree(BTree* tree, char* word, int pos, char* urlDoc)
{
  BTreeKey* existingKey = btree_search(tree->root, word);
  if (existingKey)
  {
    addPositionVector(existingKey->mInfo, urlDoc, pos);
    free(word);
  } else {
    Vector* bkey_info = vector_newVector();
    addPositionVector(bkey_info, urlDoc, pos);
    BTreeKey* bkey = btree_newBTreeKey(word, bkey_info);
    btree_insert(tree, tree->root, bkey);
  }
}
Exemplo n.º 21
0
Node *btree_get_def(Ctl *ctl, LookupType lu_type, XString *name) {

   XmlBTree *tree; 
   BTreeCtl *treectl;
   XString   dummy_value;
   uint16    index;

   treectl = &ctl->btree_ctl;
   tree    = treectl->btree;

   index = btree_search(ctl, lu_type, name, &dummy_value, 0);
   if(index == 0) return NULL;
   return(tree[index].definition);

} 
Exemplo n.º 22
0
/**
* @brief B tree insert method.
*
* @param T B tree.
* @param k the element to insert.
*
* @return return 1 means insert success, otherwise return 0 means not insert it
* as the element is already include in the B tree.
*/
PUBLIC int btree_insert(TREE* T, ELEMENT k) {
	if (btree_search(T, k)) { return 0; }
	NODE* r = T->root;
	if (FULL_KEY_COUNT == r->n) {
		NODE* s = _btree_allocate_node();
		T->root = s;
		s->leaf = FALSE;
		s->n = 0;
		s->c[0] = r;
		_btree_split_child(s, 0);
		_btree_insert_nonfull(s, k);
	} else {
		_btree_insert_nonfull(r, k);
	}
	return 1;
}
Exemplo n.º 23
0
void *
h_table_find( h_table_t *table, char *key )
{
	int index;
	
	index = btree_search( table->tree, key );
	
	if( index != -1 )
	{
		return table->data_arr[ index ];
	}
	else
	{
		return NULL;
	}
}
Exemplo n.º 24
0
static BI_DATA *
find_elapsed(const char *txt)
{
    BI_DATA *result = 0;
    char temp[80];
    char *s = strchr(mktrimmed(strcpy(temp, txt)), L_PAREN);
    if (s != 0)
	*s = EOS;
    if (*temp != EOS) {
	if ((result = btree_search(&elapsed_tree, temp)) == 0) {
	    BI_DATA data;
	    memset(&data, 0, sizeof(data));
	    data.bi_key = temp;
	    result = btree_insert(&elapsed_tree, &data);
	}
    }
    return result;
}
Exemplo n.º 25
0
BTreeNode * btree_search( BTreeNode * node, Key k, int * index )
{
	int i = 0;

	while( i < node->n && k > node->keys[i] )
		++i;

	if( i < node->n && k == node->keys[i] ) {
		*index = i;
		return node;
	}
	else if( node->leaf ) {
		return NULL;
	}
	else {
		return btree_search(node->children[i], k, index );
	}
}
Exemplo n.º 26
0
int main(){
  char command[100];
  char word[100];
  char tango[100];
  tnode *tree = NULL;
  tnode *temp = NULL;

  while(1){
    scanf("%s", command);
    if(strcmp(command, "insert") == 0){
      scanf("%s %s", word, tango);
      temp = btree_insert(word, tango, tree);
      if(tree == NULL){
	tree = temp;
      }
    }else if(strcmp(command, "delete") == 0){
      scanf("%s", word);
      temp = btree_delete(word, tree);
      if(temp == tree->left){
	tree = tree->left;
      }else if(temp == tree->right){
	tree = tree->right;
      }
      
	
    }else if(strcmp(command, "search") == 0){
      scanf("%s", word);
      temp = btree_search(word, tree);
      if(temp == NULL){
	printf("(not found)\n");
      }else{
	printf("%s\n", temp->value);
      }
    }else if(strcmp(command, "quit") == 0){
      break;
    }else{
      printf("ERROR\n");
    }
  }

  btree_destroy(tree);
  return 0;
}
Exemplo n.º 27
0
int
btree_get_next_key(btree_t *tree, uint32_t cur_key, uint32_t *next_key)
{
    btree_node_t *x;

    btree_validate(tree);
    x = btree_search(tree->root, cur_key);
    if (x == NULL) {
        return FALSE;
    }

    x = btree_successor(x);
    if (x == NULL) {
        return FALSE;
    }

    *next_key = x->key;
    return TRUE;
}
Exemplo n.º 28
0
int acl_btree_get_next_key(ACL_BTREE *tree,
	unsigned int cur_key, unsigned int *next_key)
{
	BTREE_NODE *x;

	btree_validate(tree);
	x = btree_search(tree->root, cur_key);
	if (x == NULL) {
		return (-1);
	}
        
	x = btree_successor(x);
	if (x == NULL) {
		return (-1);
	}
        
	*next_key = x->key;
	return (0);
}
Exemplo n.º 29
0
int
binode_cache_find (uint64_t key, bt_key_val *temp_binode)
{
  clock_t start, end;
  int ret = 0;
  bt_key_val kv;
  LINKED *temp;

#ifdef DEBUG_FUXEXMP
  fprintf(stderr, "binode_cache_find: key = %llx\n", key);
#ifdef DEBUG
  print_subtree(block_inode_tree, block_inode_tree->root);
#endif
#endif
  kv = btree_search(block_inode_tree,key);

  if (kv.val != 0)
  {
    *temp_binode = kv;
    ret = SUCCESS;
    temp=kv.pt;

    if(temp!=binode_list)
    {
      temp->prev->next=temp->next;
      temp->next->prev=temp->prev;
      temp->prev=binode_list->prev;
      temp->next=binode_list;
      binode_list->prev=temp;
      binode_list=temp;
    }
  }

  else
  {
    ret = FAILURE;
  }

  return ret;
}
Exemplo n.º 30
0
int
memory_cache_find (uint64_t hash_key, bt_key_val  *temp_memory)
{

  clock_t start, end;
  int ret = 0;
  bt_key_val kv;
  LINKED *temp;

  kv = btree_search(memory_tree,hash_key);

  if (kv.val != 0)
  {
    *temp_memory = kv;
    ret = SUCCESS;
    temp=kv.pt;


    if(temp!=memory_list) 
    {
      temp->prev->next=temp->next;
      temp->next->prev=temp->prev;
      temp->prev=memory_list->prev;
      temp->next=memory_list;
      memory_list->prev=temp;
      memory_list=temp;
    } 


  }
  else
  {
    ret = FAILURE;
  }

  return ret;

}