Tries
trie,又稱前綴樹或字典樹,是一種有序樹,用于保存關聯數組,其中的鍵通常是字符串。與二叉查找樹不同,鍵不是直接保存在節點中,而是由節點在樹中的位置決定。一個節點的所有子孫都有相同的前綴,也就是這個節點對應的字符串,而根節點對應空字符串。一般情況下,不是所有的節點都有對應的值,只有葉子節點和部分內部節點所對應的鍵才有相關的值。
一個保存了 8 個鍵的 trie 結構,"A", "to", "tea", "ted", "ten", "i", "in", and "inn".
In the example shown, keys are listed in the nodes and values below them. Each complete English word has an arbitrary integer value associated with it. A trie can be seen as adeterministic finite automaton, although the symbol on each edge is often implicit in the order of the branches.
It is not necessary for keys to be explicitly stored in nodes. (In the figure, words are shown only to illustrate how the trie works.)
Though tries are most commonly keyed by character strings, they don't need to be. The same algorithms can easily be adapted to serve similar functions of ordered lists of any construct, e.g., permutations on a list of digits or shapes. In particular, abitwise trieis keyed on the individual bits making up a short, fixed size of bits such as an integer number or memory address.
A trie can also be used to replace ahash table, over which it has the following advantages:
Looking up data in a trie is faster in the worst case, O(m) time (where m is the length of a search string), compared to an imperfect hash table. An imperfect hash table can have key collisions. A key collision is the hash function mapping of different keys to the same position in a hash table. The worst-case lookup speed in an imperfect hash table isO(N)time, but far more typically is O(1), with O(m) time spent evaluating the hash.
There are no collisions of different keys in a trie.
Buckets in a trie, which are analogous to hash table buckets that store key collisions, are necessary only if a single key is associated with more than one value.
There is no need to provide a hash function or to change hash functions as more keys are added to a trie.
A trie can provide an alphabetical ordering of the entries by key.
Tries do have some drawbacks as well:
Tries can be slower in some cases than hash tables for looking up data, especially if the data is directly accessed on a hard disk drive or some other secondary storage device where the random-access time is high compared to main memory.[5]
Some keys, such as floating point numbers, can lead to long chains and prefixes that are not particularly meaningful. Nevertheless a bitwise trie can handle standard IEEE single and double format floating point numbers.
Some tries can require more space than a hash table, as memory may be allocated for each character in the search string, rather than a single chunk of memory for the whole entry, as in most hash tables.
Implementation:
1 #include<stdio.h>
2 #include<conio.h>
3 #include<stdlib.h>
4 #include<string.h>
5
6 typedef struct trie trie;
7 struct trie
8 {
9 char key;
10 trie *next,*children;
11 };
12
13 trie *newnode(char s)
14 {
15 trie *t=(trie *)malloc(sizeof(trie));
16 t->key=s;
17 t->next=t->children=NULL;
18 }
19
20 void insert(trie **t,char *s,int start)
21 {if(s[start]=='')
22 {
23 *t=newnode('#');
24 return;
25 }
26 if(*t==NULL)
27 {
28 *t=newnode(s[start]);
29 insert(&(*t)->children,s,start+1);
30 }
31 if((*t)->key==s[start])
32 insert(&(*t)->children,s,start+1);
33 else
34 insert(&(*t)->next,s,start);
35 }
36
37
38 bool search(trie *t ,char *s,int start)
39 {
40
41
42 if(t==NULL)
43 return false;
44
45 if(t->key=='#' && s[start]=='')
46 return true;
47
48 if(t->key!='#' && s[start]=='' || t->key=='#' && s[start]!='')
49 return false;
50
51 if(t->key==s[start])
52 return search(t->children,s,start+1);
53
54 else
55 return search(t->next,s,start);
56
57 return false;
58 }
59
60 /*void push(trie **t ,char *str)
61 { int i=0;
62 for(i=0;i<strlen(str);i++)
63 insert(t,str[i]);
64 }*/
65
66 main()
67 { int i=0;
68
69 trie *t=NULL;
70 char ch='y';
71 while(ch=='y')
72 {
73 {char str[20];
74 fflush(stdin);
75 printf("Enter the word ");
76 gets(str);
77
78
79 insert(&t,str,0);
80 }
81 // push(&t,str);
82 fflush(stdin);
83 printf("more y/n ::");
84 ch=getchar();
85 }
86
87 ch='y';
88 while(ch=='y')
89 {char str[20];
90 fflush(stdin);
91 printf("Enter the string you want to search::");
92 gets(str);
93
94 fflush(stdin);
95 if(search(t,str,0))
96 printf("Found");
97 else
98 printf("Not Found");
99
100 printf("
more y/n ::");
101 scanf("%c",&ch);
102
103 }
104
105 getch();
106
107 }
總結
- 上一篇: Linux中unlink函数和删除文件的
- 下一篇: html版权符号如何打出来