Exemple #1
0
static int recursive_insert(BSTreeNode* root, BSTreeNode* node, BSTree_Compare* compare)
{
    int ret = 1;
    int r = compare(node->key, root->key);
    
    if( r == 0 )
    {
        ret = 0;
    }
    else if( r < 0 )
    {
        if( root->left != NULL )
        {
            ret = recursive_insert(root->left, node, compare);
        }
        else
        {
            root->left = node;
        }
    }
    else if( r > 0 )
    {
        if( root->right != NULL )
        {
            ret = recursive_insert(root->right, node, compare);
        }
        else
        {
            root->right = node;
        }
    }
}
Exemple #2
0
int BSTree_Insert(BSTree* tree, BSTreeNode* node, BSTree_Compare* compare) 
{
    TBSTree* btree = (TBSTree*)tree;
    int ret = (btree != NULL) && (node != NULL) && (compare != NULL);
    
    if( ret )
    {
        node->left = NULL;
        node->right = NULL;
        
        if( btree->root == NULL )
        {
            btree->root = node;
        }
        else
        {
            ret = recursive_insert(btree->root, node, compare);
        }
        
        if( ret )
        {
            btree->count++;
        }
    }
    
    return ret;
}
AvlNode<Key>* AVLtree<Key, Comparator>::recursive_insert (const Key& key, AvlNode<Key>* current_root){

	if (current_root == nullptr)
	{
		size++;
		return new AvlNode<Key>(key);
	}
	if (key == current_root -> key)
		return current_root;

	if (comparator(key, current_root -> key))
		current_root -> left = recursive_insert(key, current_root -> left);
	else 
		current_root -> right = recursive_insert(key, current_root -> right);

	//Балансирует, поднимаясь вверх по рекурсии.
	return balance(current_root);
}
Exemple #4
0
void OcrBST :: recursive_insert(OcrBSTNode * current_node, const Occurrence & v)
{
	int compare_result;
	compare_result = v.compare(current_node->GetValue());

	if(compare_result < 0)
	{
		//go left
		if(current_node->GetLeft() == NULL)
		{
			current_node->left = new OcrBSTNode(v);
			size++;
			returnValue = current_node->left;
		}
		else
		{
			recursive_insert(current_node->GetLeft(), v);
		}
	}

	else if(compare_result > 0)
	{
		//go right
		if(current_node->GetRight() == NULL)
		{
			current_node->right = new OcrBSTNode(v);
			size++;
			returnValue = current_node->right;
		}
		else
		{
			recursive_insert(current_node->GetRight(), v);
		}
	}
	else
	{
		returnValue = NULL;
	}

}
Exemple #5
0
OcrBSTNode * OcrBST::Insert(const Occurrence & v)
{
    if(root == NULL)
    {
        root = new OcrBSTNode(v);
        size++;
        return root;
    }
    else
    {
		returnValue = NULL;
		recursive_insert(root, v);
		return returnValue;
    }
}
Exemple #6
0
void lm_trie_build(lm_trie_t *trie, ngram_raw_t **raw_ngrams, uint32 *counts, int order)
{
    int i;
    if (lm_trie_quant_to_train(trie->quant)) {
        E_INFO("Training quantizer\n");
        for (i = 2; i < order; i++) {
            lm_trie_quant_train(trie->quant, i, counts[i - 1], raw_ngrams[i-2]); 
        }
        lm_trie_quant_train_prob(trie->quant, order, counts[order - 1], raw_ngrams[order - 2]);
    }
    E_INFO("Building LM trie\n");
    recursive_insert(trie, raw_ngrams, counts, order);
    /* Set ending offsets so the last entry will be sized properly */
    // Last entry for unigrams was already set.  
    if (trie->middle_begin != trie->middle_end) {
        middle_t *middle_ptr;
        for (middle_ptr = trie->middle_begin; middle_ptr != trie->middle_end - 1; ++middle_ptr) {
            middle_t *next_middle_ptr = middle_ptr + 1;
            middle_finish_loading(middle_ptr, next_middle_ptr->base.insert_index);
        }
        middle_ptr = trie->middle_end - 1;
        middle_finish_loading(middle_ptr, trie->longest->base.insert_index);
    }
}
void AVLtree<Key, Comparator>::insert(const Key& key){
	root = recursive_insert(key, root);
}
Exemple #8
0
int main(void) {
	int i;
	node *t1, *t2, *t3;

	/* recursive_insert() */
	printf("=== Tesing Insert Recursive ===\n");
	t1 = NULL;

	int tree_values[10] = {5, 8, 3, 6, 1, 4, 9, 0, 7, 2};

	for (i=0; i<10; i++) {
		recursive_insert(&t1,tree_values[i]);
	}

	printf("=== Inorder Traversal ===\n");
	print_inorder_traverse(t1);
	printf("\n");


	printf("=== PreOrder Traversal ===\n");
	print_preorder_traverse(t1);
	printf("\n");

	printf("=== PostOrder Traversal ===\n");
	print_postorder_traverse(t1);
	printf("\n");

	node *elem;
	printf("=== Recursive Search ===\n");
	elem = recursive_search(t1, 5);
	printf("%d\n", elem->value);
	printf("%d\n", elem->left->value);
	printf("%d\n", elem->right->value);
	printf("\n");

	printf("=== Copy Tree ===\n");
	t2=copy(t1);
	print_inorder_traverse(t1);
	printf("\n");
	print_inorder_traverse(t2);
	printf("\n");

	printf("=== Depth ===\n");
	int _d = depth(t1);
	printf("%d\n", _d);
	printf("\n");


	printf("=== Num Nodes ===\n");
	int _n = num_nodes(t1);
	printf("%d\n", _n);
	printf("\n");

	printf("=== Destroy ===\n");
	destroy(&t1);
	print_inorder_traverse(t1);
	printf("\n");


	printf("=== Is Identical ===\n");
	t1=NULL;
	t2=NULL;

	for (i=0; i<10; i++) {
		recursive_insert(&t1,tree_values[i]);
		recursive_insert(&t2,tree_values[i]);
	}

	int identical = is_identical(t1, t2);
	if (identical)
		printf("%s\n", "Trees are identical");
	else
		printf("%s\n", "Trees are not identical");
	printf("\n");


	printf("=== Num of Leaves ===\n");
	int leaves = num_leaves(t1);
	printf("%d\n", leaves);
	printf("\n");

}
Exemple #9
0
/**
 * Inserts a new value into the ART tree
 * @arg t The tree
 * @arg key The key
 * @arg key_len The length of the key
 * @arg value Opaque value.
 * @return NULL if the item was newly inserted, otherwise
 * the old value pointer is returned.
 */
void* art_insert(art_tree *t, const unsigned char *key, int key_len, void *value) {
    int old_val = 0;
    void *old = recursive_insert(t->root, &t->root, key, key_len, value, 0, &old_val);
    if (!old_val) t->size++;
    return old;
}
Exemple #10
0
static void* recursive_insert(art_node *n, art_node **ref, const unsigned char *key, int key_len, void *value, int depth, int *old) {
    // If we are at a NULL node, inject a leaf
    if (!n) {
        *ref = (art_node*)SET_LEAF(make_leaf(key, key_len, value));
        return NULL;
    }

    // If we are at a leaf, we need to replace it with a node
    if (IS_LEAF(n)) {
        art_leaf *l = LEAF_RAW(n);

        // Check if we are updating an existing value
        if (!leaf_matches(l, key, key_len, depth)) {
            *old = 1;
            void *old_val = l->value;
            l->value = value;
            return old_val;
        }

        // New value, we must split the leaf into a node4
        art_node4 *new_node = (art_node4*)alloc_node(NODE4);

        // Create a new leaf
        art_leaf *l2 = make_leaf(key, key_len, value);

        // Determine longest prefix
        int longest_prefix = longest_common_prefix(l, l2, depth);
        new_node->n.partial_len = longest_prefix;
        memcpy(new_node->n.partial, key+depth, min(MAX_PREFIX_LEN, longest_prefix));
        // Add the leafs to the new node4
        *ref = (art_node*)new_node;
        add_child4(new_node, ref, l->key[depth+longest_prefix], SET_LEAF(l));
        add_child4(new_node, ref, l2->key[depth+longest_prefix], SET_LEAF(l2));
        return NULL;
    }

    // Check if given node has a prefix
    if (n->partial_len) {
        // Determine if the prefixes differ, since we need to split
        int prefix_diff = prefix_mismatch(n, key, key_len, depth);
        if ((uint32_t)prefix_diff >= n->partial_len) {
            depth += n->partial_len;
            goto RECURSE_SEARCH;
        }

        // Create a new node
        art_node4 *new_node = (art_node4*)alloc_node(NODE4);
        *ref = (art_node*)new_node;
        new_node->n.partial_len = prefix_diff;
        memcpy(new_node->n.partial, n->partial, min(MAX_PREFIX_LEN, prefix_diff));

        // Adjust the prefix of the old node
        if (n->partial_len <= MAX_PREFIX_LEN) {
            add_child4(new_node, ref, n->partial[prefix_diff], n);
            n->partial_len -= (prefix_diff+1);
            memmove(n->partial, n->partial+prefix_diff+1,
                    min(MAX_PREFIX_LEN, n->partial_len));
        } else {
            n->partial_len -= (prefix_diff+1);
            art_leaf *l = minimum(n);
            add_child4(new_node, ref, l->key[depth+prefix_diff], n);
            memcpy(n->partial, l->key+depth+prefix_diff+1,
                    min(MAX_PREFIX_LEN, n->partial_len));
        }

        // Insert the new leaf
        art_leaf *l = make_leaf(key, key_len, value);
        add_child4(new_node, ref, key[depth+prefix_diff], SET_LEAF(l));
        return NULL;
    }

RECURSE_SEARCH:;

    // Find a child to recurse to
    art_node **child = find_child(n, key[depth]);
    if (child) {
        return recursive_insert(*child, child, key, key_len, value, depth+1, old);
    }

    // No child, node goes within us
    art_leaf *l = make_leaf(key, key_len, value);
    add_child(n, ref, key[depth], SET_LEAF(l));
    return NULL;
}