- 论坛徽章:
- 0
|
构建一个索引树!
讲课时用过这个例子,参考一下吧!
- #define TREEWIDTH 256
- typedef struct node_t {
- struct node_t *next[TREEWIDTH];
- int count;
- } node_t;
- /* 这个函数把一个word导入一个索引树,然后返回这个word以前曾经导入过多少次 */
- int
- indexword(node_t *head, const char *word)
- {
- int i;
- node_t *cur,*new;
- for (i=0,cur=head;;++i) {
- if (word[i]=='\0') {
- cur->count=cur->count+1;
- if (cur->count > 1) {
- return cur->count-1;
- }
- break;
- }
- if (cur->next[word[i]] == NULL) {
- cur->next[word[i]] = (node_t*)malloc(sizeof(node_t));
- if (cur->next[word[i]] == NULL) {
- return -1;
- }
- }
- cur=cur->next[word[i]];
- }
- return 0;
- }
复制代码 |
|