Пример #1
0
void free_trie(struct trie_node *root) {
    struct trie_node *node = root;
    if(node != NULL) {
        free_trie(node->brother);
        free_trie(node->child);
        free(node);
    }
}
Пример #2
0
/* Need a separate function to recursively walk the tree */
static void free_trie( token_trie *t ) {
    if( !t ) {
        return;
    }
    else {
        free_trie(t->next);
        free_trie(t->child);
        free(t);
    }
}
Пример #3
0
/* Function to close and clean up a task */
void task_close () {
	print_trie (Head);
	fflush (FOUT); fflush (FERR);
	fclose (FOUT); fclose (FERR);
	free_trie (Head);
	Free (task);
}
Пример #4
0
/* deallocate mem */
void free_trie(trie *t)
{
  int i;
  if (t!=NULL)
    {
      for (i=0;i<MAX;i++)
	free_trie(t->next[i]);
      free(t);
    }
}
Пример #5
0
int main(void)
{
  trie *t;
  
  t = new_trie();
  printf("Added %s %d\n","texta",trie_insert(t,"texta"));
  printf("Added %s %d\n","textb",trie_insert(t,"textb"));
  printf("Added %s %d\n","texta",trie_insert(t,"texta"));
  printf("Searched %s %d\n","texta",trie_search(t,"texta"));
  printf("Searched %s %d\n","textc",trie_search(t,"textc"));
  free_trie(t);

  return 0;
}
Пример #6
0
/* Recursive Function to free all the nodes in a TRIE */
void free_trie (struct node *node) {
	int i;
	
	if (UInfo) {
		Free (UID);
		Free (UInfo);
	}

	for (i = 0; i < 10; i++)
		if (Children[i])
			free_trie (Children[i]);

	Free (node);
}
void free_trie (struct trienode* root)
{
#if 0
    struct trienode* zero;
    struct trienode* one;
    int index;

    if (!root || *root) {
        return;
    }

    //for (index = 0; index <
    free_trie(*root->link[0]);
#endif
}
Пример #8
0
int main(void) {
    char word[11];
    int i, n, m;
    root = create_empty_trie();
    scanf("%d", &n);
    for(i = 0; i < n; i++) {
        scanf("%s", word);
        add_string(root, word);
    }
    scanf("%d", &m);
    for(i = 0; i < m; i++) {
        scanf("%s", word);
        printf("%d\n", search_string(root, word));
    }
    free_trie(root);
    return 0;
}
Пример #9
0
result write_messages(FILE *out, Messages *m) {
    int i;
    result return_code;

    init_trie();
    for (i = 0; i < m->count; i++) {
        if(write_message(out, &m->messages[i]) == ERROR) {
            fprintf(stderr, "Error writing message %d\n", i);
            return_code = ERROR;
            goto free_trie;
        }
    }
    return_code = OK;

free_trie:
    free_trie();
    return return_code;
}
Пример #10
0
static void compiler_cleanup() {
    token_list *tokens, *t;
    label_loc_map *map, *m;
    op_list *ol, *o;
    /* Eat token list */
    tokens = tl;
    while( tokens ) {
        t = tokens;
        tokens = tokens->next;
        if( t->ident ) {
            free(t->ident);
        }
        free(t);
    }
    tl_end = NULL;
    free_trie(parser_trie);
    /* Note: The label/loc maps use shallow copies of the ident fields from the
       token list for the name fields. These strings are freed when the token
       list is freed, so we don't have to deal with them here. */
    /* Eat label map */
    map = lmap;
    while( map ) {
        m = map;
        map = map->prev;
        free(m);
    }
    /* Eat branch map */
    map = fmap;
    while( map ) {
        m = map;
        map = map->prev;
        free(m);
    }
    /* Eat tentative instruction list */
    ol = ops;
    while( ol ) {
        o = ol;
        ol = ol->next;
        free(o);
    }
}
int find_max_xor_two_numbers_in_array_v2 (int* array, int len)
{
    int index, bit_index, max_xor, num_max_xor, link;
    struct trienode* root = NULL;
    struct trienode* current;

    if (!array || (len <= 1)) {
        return(0);
    }

    init_trie(&root);

    if (!root) {
        return(0);
    }

    for (index = 0; index < len; ++index) {
        insert_into_trie(root, array[index]);
    }

    max_xor = 0;
    for (index = 0; index < len; ++index) {

        current = root;
        num_max_xor = array[index];

        for (bit_index = sizeof(int) * CHAR_BIT - 1;
             bit_index >= 0; --bit_index) {

            if (array[index] & (1 << bit_index)) {
                link = 1;
            } else {
                link = 0;
            }

            if (link == 0) {
                if (!current->links[1]) {
                    num_max_xor &= ~(1 << bit_index);
                    current = current->links[0];
                } else {
                    num_max_xor |= (1 << bit_index);
                    current = current->links[1];
                }
            } else {
                if (!current->links[0]) {
                    num_max_xor &= ~(1 << bit_index);
                    current = current->links[1];
                } else {
                    num_max_xor |= (1 << bit_index);
                    current = current->links[0];
                }
            }
        }

        if (num_max_xor > max_xor) {
            max_xor = num_max_xor;
        }
    }

    free_trie(root);

    return(max_xor);
}
Пример #12
0
int
main(int argc, char **argv) {
    trie_t *dict = gen_trie();
    node_t *current_node, *last_known_node;
    int inserts = 1, i, last_known_idx, last_known_i, sec_known_idx, total;

    /* Initialization */
    last_known_idx = last_known_i = sec_known_idx = total = 0;
    current_node = last_known_node = dict->root;

    /* Instead of saving the entire file into an array, read the file and
     * process it as you read.
     * Trie is memory hungry enough, best to save memory where possible */
    while((i=getchar()) != EOF) {
        total++;
#if DEBUG
        if(i!=10) fprintf(stderr, "i = %d // char = %c\n", i, i);
        else fprintf(stderr, "i = 10 // char = </n>\n");
#endif

        if((current_node = search_node(current_node, i)) == NULL) {
            /* No match found.
             * Since at this point, its guranteed that previous values are
             * matching, simply add new character to the latest node */
#if DEBUG
            fprintf(stderr, " > NOT FOUND\n");
#endif
            insert_into_node(last_known_node, i, inserts++);

            printf("%c%d\n", i, last_known_idx);

            /* re-initialize */
            last_known_idx = 0;
            current_node = last_known_node = dict->root;
        } else {
            /* Keep track of history - affects constant for n^2 
             * Affects program speed slightly.
             * To negate this, reading the input in chunks then recording
             * history only for the last chunk will negate unneccesary
             * recording of history in previous chunks but this is
             * too minor impact to performance compared to amount of
             * complexity of the code */
            sec_known_idx   = last_known_idx;
            last_known_idx  = current_node->idx;
            last_known_node = current_node;
#if DEBUG
            fprintf(stderr, " > FOUND under index %d\n", current_node->idx);
#endif
        }

        last_known_i = i;

#if DEBUG
            fprintf(stderr, "----------\n");
#endif
    }

    if(last_known_idx != 0) {
        /* Handling exception situations where the last character was still
         * included in the count. This means if text is:
         *   (prev strings) . . X Y \0
         * ..XY were found in previously and present in dictionary.
         * Therefore following the formatting for the assignment, print
         * index for ..X then character Y */
        printf("%c%d\n", last_known_i, sec_known_idx); 
        inserts++;
    }

    /* Free trie. May not seem necessary since the program terminates
     * immediately after this stage however in case of practical use of
     * stacked calls, there it is :)
     * NOTE: In my Linux system, free did not actually reduce memory usage.
     *       This was true for this program and Alistair's malloc programs. */
    free_trie(dict);

    fprintf(stderr, "encode: %6d bytes input\n",total);
    fprintf(stderr, "encode: %6d factors generated\n",inserts-1);

    return 0;
}