Trie树

来源:互联网 发布:淘宝汉服商家推荐2016 编辑:程序博客网 时间:2024/06/05 21:51

Trie树是前缀树,是指的对于一个节点的所有子节点具有相同的前缀。通常使用在字符串检索,经典使用场景是在搜索提示中对用户搜索词的提示,根据用户当前输入的搜索词查看有那些词语的前缀和你的输入是一样的,参考wiki;可以用于在在切词分词中用来保存词库的数据结构。对一篇文章进行切词分词的时候,通过与词库对比查找得出要如何切词,切词分词的简单介绍。

和Trie相关的另一种数据结构就是后缀树。后缀树是具有相同后缀的所有节点组成的一个树状数据结构。

参考了wiki百科中的实现,对Trie树进行简单的实现。包括插入一个新的词语和搜索一个词语。其中对于建立一棵Trie树可以循环调用插入方法直到词库结束。对于搜索一个词其实是一个确定有限状态自动机的过程。后来看到wiki才想起在编译原理中学过的这个定义。简单来说就是给定一个状态之后,有一个确定的下一状态可以调整或者调整到结束状态。对于Tries树,每个节点其实就是一个状态,而节点之间的连线就是状态转移。

下面是代码实现,一个简单程序中c与c++风格都有,囧。

[cpp] view plaincopy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #define ALPHABET_SIZE 26  
  5. #define CHAR_TO_INDEX(c) ((int)c - (int)'a')  
  6. typedef struct trie_node{  
  7.         int value;  
  8.         trie_node *children[ALPHABET_SIZE];       
  9. };  
  10.   
  11. typedef struct trie{  
  12.         trie_node *root;  
  13.         int count;        
  14. };  
  15.   
  16. void initNode(trie_node *current_node){  
  17.      for(int level = 0; level < ALPHABET_SIZE; level ++){  
  18.              current_node -> children[level] = 0;  
  19.      }  
  20.      current_node -> value = -1;  
  21. }  
  22.   
  23. void insert(trie *pTrie, char *key, int value){  
  24.      if(pTrie == 0){  
  25.               pTrie = (trie *) malloc(sizeof(trie));  
  26.               pTrie -> count = 0;  
  27.       }  
  28.       pTrie -> count++;  
  29.       trie_node *tn = pTrie -> root;  
  30.       int length = strlen(key);  
  31.       int level = 0, index = 0;  
  32.       for(; level < length; level ++){  
  33.             index = CHAR_TO_INDEX(key[level]);  
  34.             if(tn -> children[index] == 0 && !(tn -> children[index])){  
  35.                   trie_node *new_node = (trie_node *) malloc(sizeof(trie_node));  
  36.                   initNode(new_node);  
  37.                   tn -> children[index] = new_node;  
  38.             }  
  39.             tn = tn -> children[index];  
  40.       }  
  41.       tn -> value = value;  
  42.         
  43. }  
  44. int search(const trie *pTrie, char *key){  
  45.     int result = -1;  
  46.     if(pTrie == 0){  
  47.          return result;  
  48.   }  
  49.   trie_node *tn = ((trie *) pTrie) -> root;  
  50.   int level = 0, length = strlen(key);  
  51.   int index = 0;  
  52.   for(; level < length; level ++){  
  53.         index = CHAR_TO_INDEX(key[level]);  
  54.         if(tn -> children[index] == 0 || !(tn -> children[index])){  
  55.                      printf("can't finde the \" %s \"", key);  
  56.                      result = -1;  
  57.              break;   
  58.         }  
  59.         tn = tn -> children[index];  
  60.         result = level;  
  61.    }  
  62.     return result;  
  63. }  
  64.   
  65. main(){  
  66.        trie *pTrie = (trie *) malloc(sizeof(trie));  
  67.        pTrie -> count = 0;  
  68.        pTrie -> root = (trie_node *) malloc(sizeof(trie_node));  
  69.        initNode(pTrie -> root);  
  70.        trie_node *node = pTrie -> root;  
  71.        insert(pTrie, "abc", -1);  
  72.        int result = search(pTrie, "ac");  
  73.        printf("\nresult num : %d\n", result);  
  74.       system("pause");   

原创粉丝点击