笔试算法题(39):Trie树(Trie Tree or Prefix Tree)

议题:TRIE树 (Trie Tree or Prefix Tree);

分析:

  • 又称字典树或者前缀树,一种用于快速检索的多叉树结构;英文字母的Trie树为26叉树,数字的Trie树为10叉树;All the descendants of a node have a common prefix of the sequence associated with that node, and the root is associated with the empty sequence. 由于不同的sequence使用公共的前缀,所以Trie树可以节省大量空间;但如果sequence仅有很少的公共前缀时,Trie树耗用较大的空间;

  • Trie树中根节点为空字符,其他每个节点仅包含一个字符;从根节点到某一个终止节点的路径上经过的字符连接起来就是对应的string;Trie树中查 找长度为M的string的时间复杂度为O(M),BST树中查找长度为M的string则需要O(MlogN),N为树中的节点数,则logN为树的高 度,并且前提是BST为平衡树,Trie树的时间复杂度不受树是否平衡的影响;对于Hash表而言,其也可以实现查找,但是Trie树可以实现 Closest Fit,也就是近似查找,Hash表不能实现近似查找;同时,Hash表有插入冲突需要解决;

     
  • Trie树中有两类节点,元素节点和分支节点,前者表示一条完整路径的终结,后者表示路径中的一个节点;Trie树的应用包括字典查找 (Dictionary String Search)和近似匹配算法(Approximate Matching Algorithm);Trie树有三种结构:Standard Trie,Compressed Trie和Suffix Trie;标准Trie其实就是Prefix Trie;由于string本身可以存在一个单独的string1是另一个单独的string2的前缀,但是Trie树中规定所有stirng都是从根节 点到叶子节点的完整路径,所以实际实现中需要在所有string最后引入一个$字符标志结束;

  • 由于使用树+指针实现Trie树非常耗用内存,所以首先可以将原始Trie树进行压缩,也就是只有单个子节点的节点可以合并成一个节点;同时可以使用Double-Array Trie作为Trie树的实现:

样例:

 1 #define MAX_NUM 26
 2 /**
 3  * 表示两种类型的节点
 4  * */
 5 enum NodeType {
 6         COMPLETED,
 7         UNCOMPLETED
 8 };
 9 /**
10  * child指针数组存储a-z字母对应的索引
11  * */
12 struct Node {
13         NodeType type;
14         char ch;
15         Node *child[MAX_NUM];
16 };
17 
18 Node *root;
19 
20 Node* CreateNode(char ch) {
21         Node *node=new Node();
22         node->ch=ch;
23         node->type=UNCOMPLETED;
24         for(int i=0;i<MAX_NUM;i++)
25                 node->child[i]=NULL;
26         return node;
27 }
28 /**
29  * 初始化:将根节点创建为空字符的节点
30  * */
31 void Init() {
32         root=CreateNode('');
33 }
34 
35 void Clean(Node *root) {
36         if(root==NULL) return;
37         for(int i=0;i<MAX_NUM;i++) {
38                 /**
39                  * 首先递归处理子节点
40                  * */
41                 Clean(root->child[i]);
42         }
43         /**
44          * 删除动态内存
45          * */
46         delete [] root->child;
47         /**
48          * 删除节点本身
49          * */
50         delete root;
51 }
52 
53 bool IsExisted(char *string,int length) {
54         Node *index=root;
55         Node *temp;
56         int i;
57         for(i=0;i<length;i++) {
58                 temp=index->child[string[i]-'a'];
59                 /**
60                  * 如果某一个字符对应的指针位置为NULL,说明
61                  * 此字符不存在
62                  * */
63                 if(temp==NULL)
64                         break;
65                 index=index->child[string[i]='a'];
66         }
67         /**
68          * 仅当string匹配完全,并且trie树正好到达结束节点
69          * 才成功返回
70          * */
71         if((i==length) && (index->type==COMPLETED))
72                 return true;
73         else
74                 return false;
75 }
76 
77 
78 void InsertNew(char *string, int length) {
79         Node *index=root;
80         Node *temp;
81         int i;
82         for(i=0;i<length;i++) {
83                 temp=index->child[string[i]-'a'];
84                 if(temp==NULL)
85                         /**
86                          * 新插入的string的每一个字符都创建一个节点
87                          * */
88                         temp=CreateNode(string[i]);
89                 index=index->child[string[i]-'a'];
90         }
91         /**
92          * 最后一个节点需要为结束节点
93          * */
94         index->type=COMPLETED;
95 }

参考连接:
http://linux.thai.net/~thep/datrie/datrie.html
http://hxraid.iteye.com/blog/618962

转载于:https://www.cnblogs.com/leo-chen-2014/p/3752397.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值