My dictionary tree Template
# Define Maxnum 26 // Define dictionary Tree Structure Typedef Struct Trie { Bool Flag; // Is it a word from the root? Trie * Next [maxnum];} trie; // Declare a root Trie * Root; // Initialize the root Void Init () {Root = (Trie *) malloc ( Sizeof (Trie); root -> Flag = False ; For ( Int I = 0 ; I <maxnum; I ++ ) Root -> Next [I] = NULL ;} // Insert words into the dictionary tree Void Insert ( Char *Word) {Trie * TEM = Root; While (* Word! = ' \ 0 ' ){ If (TEM-> next [* word- ' A ' ] = Null) {Trie * Cur = (trie *) malloc ( Sizeof (Trie )); For ( Int I = 0 ; I <maxnum; I ++ ) Cur -> Next [I] = NULL; cur -> Flag = False ; TEM -> Next [* word- ' A ' ] = Cur;} TEM = Tem-> next [* word- ' A ' ]; Word ++ ;} TEM -> Flag = True ;} // Query a word Bool Search ( Char * Word) {Trie * TEM = Root; For ( Int I = 0 ; Word [I]! =' \ 0 ' ; I ++ ){ If (TEM = NULL | tem-> next [word [I]- ' A ' ] = Null) Return False ; TEM = Tem-> next [word [I]- ' A ' ];} Return TEM-> Flag ;} // Release the dictionary Tree Memory Operation.ProgramAutomatically jumps out, so no memory release function is written here Void Del (trie * Cur ){ For ( Int I = 0 ; I <maxnum; I ++ ){ If (Cur-> next [I]! = Null) del (cur ->Next [I]);} Free (cur );}
For details about the dictionary tree, refer to Baidu Baike's interpretation of the dictionary tree. The template can be used to solve this type of general problems with slight changes.