用Trie树实现词频统计和单词查询

/*
文件名: trie.c
功能描述:用trie树实现单词词频统计以及单词查询
说明:
     对统计对象,要求符合正则"[a-z]*"的格式的单词
     若考虑大写,标点和空白字符(空格.TAB.回车换行符),
     可修改next数组大小,最多255可包含所有字符
*/

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

typedef struct Trie_node_stru
{
    int  count; //记录该处终结的单词个数
    struct Trie_node_stru *next[26];//指向各个子树的指针,下标0-25代表26字符

}TrieNode, *Trie;

TrieNode* trie_createTrieNode()
{
    TrieNode* tmp = (TrieNode*)malloc(sizeof(TrieNode));
    tmp->count = 0;
    memset(tmp->next, 0, sizeof(tmp->next));
    return tmp;
}


int trie_insert(Trie root, char* word)
{
    TrieNode* node = root;
    char *p = word;
    while(*p)
    {
        if(NULL == node->next[*p-'a'])
        {
            node->next[*p-'a'] = trie_createTrieNode();
        }
        node = node->next[*p-'a'];
        p++;
    }
    node->count += 1;
    printf("%-20s appears for %d time(s) in the Trie!\r\n",
           word, node->count);
    return 0;
}

int trie_search(Trie root, char* word)
{
    TrieNode* node = root;
    char *p = word;
    while(*p && NULL!=node)
    {
        node = node->next[*p-'a'];
        p++;
    }
    return (node!=NULL && node->count>0);
}

int trie_remove(Trie root, char* word)
{
    //TODO
    return 0;
}


int main()
{
    Trie t = trie_createTrieNode();
    trie_insert(t, "a");
    trie_insert(t, "abc");
    char * c = "test";
    trie_insert(t, c);
    trie_insert(t, "testonly");
    trie_insert(t, "testonly");
    trie_insert(t, "a");


    printf("====================\r\n");
    if(trie_search(t, "a"))
      printf("true\n");
    else
      printf("false\n");
    if(trie_search(t, "testnly"))
      printf("true\n");
    else
      printf("false\n");

    return 0;
}

学习trie树知识: http://www.cnblogs.com/cherish_yimi/archive/2009/10/12/1581666.html

    原文作者:Trie树
    原文地址: https://blog.csdn.net/zhulei632/article/details/6704496
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞