Пример #1
0
int main()
{
	int path[TR_MAX_DEPTH];
	struct tree *t = tr_init();
	struct generic_data d;

	d.val = 1;
	tr_insert_root(t, d);

	d.val = 2;
	path[0] = 1;
	tr_insert(t, d, path, 1);
		
	d.val = 3;
	path[0] = 0;
	tr_insert(t, d, path, 1);

	d.val = 4;
	path[0] = 0;
	path[1] = 1;
	tr_insert(t, d, path, 2);

	d.val = 5;
	path[0] = 0;
	path[1] = 1;
	path[2] = 1;
	tr_insert(t, d, path, 3);

	//tr_walk_df(t, print_data);
	tr_walk_bf(t, print_data);
	printf("Tree height: %d\n", tr_height(t, &t->root));

}
Пример #2
0
int main(int argc, char *argv[]) {
		
	struct trie *t = NULL;
	char keybuf[1024], databuf[1024];
	size_t i;

	tr_compar = charcmp;

	t = malloc(sizeof(struct trie));
	if (!t)
		err(1, "malloc");

	memset(t, 0, sizeof(struct trie));

	stats_init();
	while (fgets(keybuf, 1023, stdin)) {

		keybuf[strlen(keybuf)-1] = '\0'; /* FIXME \r\n */
		
		snprintf(databuf, 1023, "%s is the data as well", keybuf);
	
		stats_depth_0();
		int ret = tr_insert(t, keybuf, strdup(databuf));
		if (ret != 0)
			err(1, "tr_insert");
	}

	stats_dump();

	for (i = 1; i < (size_t)argc; i++) {
		tr_search(t, argv[i]);
	}

	printf("<?xml version='1.0' standalone='no'?>\n"
                "<svg:svg width='100%%' height='100%%' version='1.1' xmlns:svg='http://www.w3.org/2000/svg'>\n");

	t->tr_key='*';
	tr_print(t, 0.0, canvas_width, 512.0, 32.0);
	printf("</svg:svg>\n");

	tr_destroy(t);
	stats_fini();

	return (0);
}
Пример #3
0
int tr_insert(struct trie *t, const char *s, void *data) {


	struct trie tmp, *tmpp = &tmp,
		*memb = NULL;


	if (*s == '\0') {
		if (t->tr_data) {
			printf("node already exists: %c: '%s'\n", t->tr_key, (char *)data);
			return (-1); /* Node already exists */
		}


		t->tr_data = data;

		return (0);
	}

	tmp.tr_key = *s;

	if (!t) {
		t = (struct trie *)malloc(sizeof(struct trie));
		if (!t)
			err(1, "malloc");

		memset(t, 0, sizeof(struct trie));

		/* the head member does not have a tr_key and
		   no data should be associated with it	*/
	} 

	if (t->tr_nmemb) {

		memb = bsearch(&tmpp, t->tr_members, t->tr_nmemb,
				sizeof(struct trie *), tr_nodecmp);
		
		stats_depth_down();

		if (memb && *(struct trie **)memb) { /* recurse here */
			return (tr_insert(*(struct trie **)memb, ++s, data));
		}
	}


	t->tr_nmemb++;
	t->tr_members = (struct trie **)realloc(t->tr_members, 
				t->tr_nmemb * sizeof(struct trie)); /* int ovrflw */


	memb = malloc(sizeof(struct trie));
	if (!memb)
		err(1, "malloc");

	memset(memb, 0, sizeof(struct trie));
	memb->tr_key = *s;

	t->tr_members[t->tr_nmemb - 1] = memb;

	if (t->tr_nmemb > 1)
		qsort(t->tr_members, t->tr_nmemb, sizeof(struct trie *), tr_nodecmp);

	stats_depth_bottom();

	return (tr_insert(memb, ++s, data));
}
Пример #4
0
int main()
{
    //-------------------------------------------------------
    // init, destroy tests 
    // test, where memory allocation is not successful
    
    root_t *root = tr_init();
    tr_destroy(root);
    tr_destroy(root);
    tr_destroy(NULL);
    
    //-------------------------------------------------------
    
    //-------------------------------------------------------
    // insert & delete & dump tests
    // test, where memory allocation is not successful
    
    root = tr_init();
    
    tr_insert(root, 0);
    tr_insert(root, 10);
    tr_insert(root, -10);
    tr_insert(root, 5);
    tr_insert(root, 15);
    tr_insert(root, 3);

    tr_delete(root, 13);
    tr_delete(root, 15);
    tr_delete(root, 0);

    tr_insert(root, 5);
    tr_insert(root, 6);
    tr_insert(root, 7);
    tr_insert(root, 8);
    tr_insert(root, 9);

    tr_fdump(root, stdout);

    tr_delete(root, 2);
    tr_delete(root, 7);
    tr_delete(root, 5);
    tr_delete(root, 6);
    tr_delete(root, 8);
    tr_delete(root, 9);

    tr_destroy(root);
    tr_insert(root, 0);
    tr_delete(root, 0);
    tr_insert(NULL, 0);
    tr_delete(NULL, 0);
    //-------------------------------------------------------

    //-------------------------------------------------------
    // height & elem & ok tests

    root = tr_init();

    tr_dump(root);

    tr_insert(root, 0);
    tr_elem(root, -1);
    tr_elem(root, 1);
    tr_elem(root, 0);


    tr_dump(NULL);
    tr_dump(root);
    tr_nok(root);
    tr_nok(NULL);

    tr_destroy(root);
    tr_elem(root, 0);
    tr_elem(NULL, 0);
     //-------------------------------------------------------
    

    //-------------------------------------------------------
    //iterator test 
    root = tr_init();
    tr_insert(root, 0);
    tr_insert(root, 10);
    tr_insert(root, -10);
    tr_insert(root, 5);
    tr_insert(root, 15);
    tr_insert(root, 3);


    int status;
    void *node = tr_itfirst(root, NULL);
    node = tr_itfirst(NULL, &status);
    node = tr_itfirst (NULL, NULL);
    node = tr_itfirst(root, &status);

    while (!tr_itislast(root, node, &status)) 
        node = tr_itnext (node);

    while (node != tr_itfirst(root, NULL)) 
        node = tr_itprev (node);


    node = tr_itleft(node);
    node = tr_itright(node);
    printf("%d\n", tr_itgetheight(node));
    printf("%d\n", tr_itgetkey(NULL, &status));

    printf("%d\n", tr_itgetkey(node, &status));

    tr_itleft(NULL);
    tr_itright(NULL);

    tr_itislast(NULL, node, NULL);
    tr_itnext (NULL);
    tr_itprev (NULL);



    tr_destroy(root);

    //-------------------------------------------------------
    return 0;
}