Trie tree | introduction and implementation of the dictionary tree trie, also known as the dictionary tree and word search tree, is a tree structure used to store a large number of strings. The advantage is that the common prefix of a string is used to save storage space. The trie tree is a relatively simple data structure. it is easy to understand, and the so-called simple things have to pay the price. therefore, the trie tree also has its disadvantages. The trie tree consumes a lot of memory. of course, it may be better to use the method of the Left son and right brother. its basic nature can be summarized as: 1. the root node does not contain characters. Each node except the root node only contains one character. 2. From the root node to a node, the character passing through the path is connected to the string corresponding to the node. 3. All subnodes of each node contain different characters. The basic operations are: Search for insert and delete, of course, delete operations are rare. here I only delete the entire tree, And the delete operation for a single word is also very simple. the method for searching dictionary items is as follows: (1) start a search from the root node; (2) obtain the first letter of the keyword to be searched, select the corresponding subtree according to the letter and go to the subtree to continue searching. (3) on the corresponding subtree, obtain the second letter of the keyword to be searched, select the corresponding subtree for search. (4) iteration process ...... (5) If all the letters of a keyword have been removed from a node, the information attached to the node is read to complete the search. Other operations are similar. /* Name: Basic implementation of the trie tree Author: maik Description: Basic implementation of the trie tree, including query insertion and deletion operations (satellite data can vary depending on the situation) */# include <algorithm> # include <iostream> using namespace STD; const int sonnum = 26, base = 'a'; struct trie {int num; // to remember how many word can reach here, that is to say, prefix bool terminal; // If terminal = true, the current point has no following point struct trie * son [sonnum]; // the following point}; trie * newtr IE () // create a new node {trie * temp = new trie; temp-> num = 1; temp-> terminal = false; For (INT I = 0; I <sonnum; ++ I) temp-> son [I] = NULL; return temp;} void insert (trie * PNT, char * s, int Len) // Insert a new word to trie tree {trie * temp = PNT; For (INT I = 0; I <Len; ++ I) {If (temp-> son [s [I]-base] = NULL) temp-> son [s [I]-base] = newtrie (); else temp-> son [s [I]-base]-> num ++; temp = temp-> son [s [I]-base];} temp-> terminal = true;} Vo Id Delete (trie * PNT) // Delete the whole tree {If (PNT! = NULL) {for (INT I = 0; I <sonnum; ++ I) if (PNT-> son [I]! = NULL) Delete (PNT-> son [I]); Delete PNT; PNT = NULL;} trie * Find (trie * PNT, char * s, int Len) // trie to find the current word {trie * temp = PNT; For (INT I = 0; I <Len; ++ I) if (temp-> son [s [I]-base]! = NULL) temp = temp-> son [s [I]-base]; else return NULL; return temp ;}