What is the difficulty of creating a lexicographic tree
4 answers
As Gabe mentioned, Trie is a good solution, but it is a little difficult to implement dictionaries with a lot of words. If O (n log n) algorithm works for you, you can solve this problem with binary search. Here's the code written in C:
char dict [n] [m]; // where n is number of words in dictionary and
// m is maximum possible length of word
char word [m]; // it your word
int l = -1, r = n;
while (l + 1 <r) {
int k = (l + r) / 2;
if (strcmp (dict [k], word) <0) l = k;
else r = k;
}
int len โโ= strlen (word);
l ++; // first word index with greater or equal prefix then word is l + 1
bool matches = (strlen (word [l])> = len);
for (int i = 0; i <len && matches; i ++) {
if (word [i]! = dict [l] [i]) {
matches = 0;
}
}
if (matches) {
printf ("given word is prefix of% dth word.", l);
} else {
printf ("given word isn't in dictinary.");
}
0
source to share