typedef TREE *NEXT; // struct TREE { NEXT *next; // char *str; // char *wrd; // NULL, };
str
field contains a string with all letters that go out of this node, the size of the next[]
array is equal to the length of this string.str
field is used to find a specific node among the array of nodes next[]
for this letter. To do this, we have introduced the ind()
function based on strchr():
int k=ind('',str); // () '' str
k<0
, then this letter is not in str
, otherwise next[k]
points to the node corresponding to this letter. For example, if str
contains the string ""
, then exactly 5 subtrees go from this node, and next[2]
refers to the letter ''
, since str[2]== ''
, the same with other letters. , , , ,
str==NULL
, then this node is a sheet and it contains the dictionary word wrd
. The node corresponding to the letter ''
contains the word ""
, nor is it a sheet, because there is another word in this dictionary, starting with -
. The wrd
field contains a string, which is obtained if you go from the root to the corresponding node, but if this string is not a dictionary word, then wrd
contains NULL
.""
following lines will fall into the inverted tree: "", "", "", "", ""
. Even for a test vocabulary with five words, such a tree will be cumbersome, so I’ll give a simplified version:wrd
serves as a flag ( NULL/ NULL
). Those. it does not store the string with the inverted prefix of the word, but:NULL
, for all other nodes.wrd!=NULL
, for the rest wrd==NULL
. For example: root_inv->str == ””; root_inv->next[0]->str == ””; root_inv->next[5]->next[0]->wrd == NULL; // root_inv->next[5]->next[0]->next[1]->wrd != NULL; //
wrd!=NULL
(red node), then this is a valid inverted word prefix. Having the prefix of a word, it is possible to find a node in the dictionary tree corresponding to the set letter, and then simply donate the remaining piece of the word already in the dictionary tree, starting from this node.count
. Before starting the search, this counter is initialized with the number 2 (for knot mode) or 1 (for the classic mode) - this is the only difference between the modes in terms of the algorithm.[x, y]
) the current letter s
. So we get 32 * n different positions, where n is the number of adjacent cells in the original positionroot_inv->next[]
we find the node t_inv
corresponding to the current letter, i.e. t_inv
is a subtree starting with the current letter: int k = ind(s, root_inv->str); //s – if(k<0) continue; // TREE * t_inv = root_inv->next[k];
count[x, y]
and call the search function in the find
subtree (about which below) for t_inv
and the cell [x, y]
find
subtree takes the playing field, the tree node ( tree
), and the cell coordinates (i, j)
, from which it is necessary to start moving along the floor, descending along the tree
:tree->wrd!=NULL
, then a valid substring is foundtree->wrd
to the array of found words.t
corresponding to this prefix, and call the find
function for t
and the cell [x, y]
(attention! Not the cell that was passed to this function, but the cell that the algorithm went to in step 3): TREE *t=root; // loopi(strlen(prefix)) //prefix { int k=ind(prefix[i],t->str); t=t->next[k]; } find(t,x,y);
tree->wrd
continue[i, j]
(from 2 in the corner to 4 in the center of the field)[i1, j1]
tree->str
, then go to step (3)count[i1, j1]==0
, then go to step (3)tree->next[]
we find the node tnext
corresponding to the letter in [i1, j1]
count[i1, j1]
and call find
for tnext
and cells [i1, j1]
””
into this cell (the remaining letters of the alphabet simply do not exist in root_inv->str
and the algorithm for them will end quickly without even starting). For each letter, call find
. For the letter
function will end quickly, because in the inverted tree there are no branches beginning with -, -
and -
, the same is true for , , ,
-
– , – , – , – , –
. For each prefix, we find a node in the dictionary tree (as described in item 1.b) and call the function find
for it.“”
corresponds to the node root->next[0]->next[0]->next[0]
:
or
, but the str
field of this node contains “”
, so we immediately finish the find
function, i.e. There are no branches in the tree starting at -
and -
. For the prefix “”
in the dictionary tree there are no branches starting with -, -, -, -
, -, -, -, -
, the same is true for the prefixes “”
and “”
.“”
corresponds to the node root->next[0]->next[0]->next[2]
.wrd!=NULL
field wrd!=NULL
, put the wrd ()
string wrd ()
in the array of found words and continue the search further. For the
and
there are no branches, we complete the find
function for them, but the
line
contained in the tree, we place it in the array of found words.99
, and it is half filled (perhaps this is one of the worst positions for any algorithm, because there are quite a lot of cells where you can go, and quite a good mutual arrangement of letters, because of which many long words can be made). In the nodules mode, the algorithm finds all words in the following time:
than the letter
).Source: https://habr.com/ru/post/207734/
All Articles