#include #include "bot.h" enum { RED, BLACK }; /* Hashing Function */ hash_t hash(char *data) { hash_t h = 0; while((*data) != '\0') { h += (unsigned int)(tolower(*data)) * 37; ++data; } return h; } struct tree_t *mk_tnode(char *data, hash_t h) { struct tree_t *node; fprintf(stderr,"\t\t\tmk_tnode\n"); node = malloc(sizeof(struct tree_t)); node->data = malloc(strlen(data) + 1); strcpy(node->data,data); node->hash = h; node->parent = NULL; node->left = NULL; node->right = NULL; node->other = NULL; if(node == NULL) { fprintf(stderr,"\t\t\tmk_tnode: node == NULL\n"); } else { fprintf(stderr,"\t\t\tmk_tnode: end :)\n"); } return(node); } struct tree_t *tree_insert(struct tree_t *base_node, char *data) { hash_t h = hash(data); struct tree_t *c_node = base_node; struct tree_t *n_node = NULL; int found = 0; fprintf(stderr,"\t\ttree_insert: %s\n",data); while(!found && c_node != NULL) { if(c_node->hash > h) { if(c_node->left == NULL) { c_node->left = mk_tnode(data,h); n_node = c_node->left; n_node->parent = c_node; found = 1; } else { c_node = c_node->left; } } else if(c_node->hash < h) { if(c_node->right == NULL) { c_node->right = mk_tnode(data,h); n_node = c_node->right; n_node->parent = c_node; found = 1; } else { c_node = c_node->right; } } else { found = 1; } } fprintf(stderr,"\t\ttree_insert: end\n"); return n_node; } struct tree_t *tree_find(struct tree_t *base_node, hash_t hash) { struct tree_t *c_node = base_node; while(c_node != NULL) { if(c_node->hash > hash) { c_node = c_node->left; } else if(c_node->hash < hash) { c_node = c_node->right; } else { return c_node; } } return c_node; } void tree_free(struct tree_t *node) { if (node->left != NULL) { tree_free(node->left); } if (node->right != NULL) { tree_free(node->right); } free(node->data); free(node); } struct tree_t *tree_init(char *data) { struct tree_t *base_node; base_node = mk_tnode(data,hash(data)); return base_node; }