Simple implementation of dictionary tree

Source: Internet
Author: User

Simple implementation of dictionary tree

The Trie tree, also known as the dictionary tree, is a tree structure, a variant of the hash tree, and a multi-Cross-tree data structure for quick search.

Stores a large number of strings. The advantage is that the common prefix of a string is used to save storage space.

The core idea of Trie is to change the space for time. The public prefix of the string is used to reduce the overhead of the query time to improve the efficiency.

It has three basic properties:
1. The root node does not contain characters. Each node except the root node contains only 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.

You can search for dictionary items by using the following methods:

(1) start a search from the root node;

(2) obtain the first letter of the keyword to be searched, select the corresponding subtree based on the letter, and go to the subtree for further search;

(3) On the corresponding subtree, obtain the second letter of the keyword to be searched, and then select the corresponding subtree for retrieval. (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.
/* Function Description: creates, inserts, and queries the trie tree. Description: For statistical objects, uppercase words are required to conform to the regular "[a-z] *" format, punctuation and white space characters (spaces. TAB. enter the line break) to modify the size of the next array. A maximum of 255 characters can be contained */# include
 
  
# Include
  
   
# Include
   
    
# Define MAX_CHILD 26 // This function takes into account only 26 English letters. typedef struct Tree {int count; // It is used to mark this node as a word, if count! = 0, the path from the root Node to the Node can form a word struct Tree * child [MAX_CHILD];} Node, * Trie_node; /* child indicates the number of types of data on each layer. If it is only a lowercase letter, it is 26. If it is changed to a case letter, it is 52. If it is added with a number, it is 62, which is determined based on the meaning of the question. Count indicates the number of identical prefixes in a dictionary tree. You should learn to change it as needed. */Node * CreateTrie () // create a trie Node tree {node * Node = (Node *) malloc (sizeof (node); memset (Node, 0, sizeof (Node); return node;} void insert_node (Trie_node root, char * str) // trie Tree Insertion Node {if (root = NULL | * str = '\ 0') return; Node * t = root; while (* str! = '\ 0') {if (t-> child [* str-'a'] = NULL) {Node * tmp = CreateTrie (); t-> child [* str-'a'] = tmp;} t = t-> child [* str-'a']; str ++ ;} t-> count ++;} void search_str (Trie_node root, char * str) // check whether the string is in the trie tree {if (NULL = root | * str = '\ 0 ') {printf ("trie is empty or str is null \ n"); return;} Node * t = root; while (* str! = '\ 0') {if (t-> child [* str-'a']! = NULL) {t = t-> child [* str-'a']; str ++;} else break;} if (* str = '\ 0 ') {if (t-> count = 0) printf ("this string is not in the trie tree, but this string is the prefix of a word \ n "); else printf ("this string is in this trie tree \ n");} else printf ("this string is not in the trie tree \ n");} void del (Trie_node root) // release the heap space occupied by the entire dictionary tree {int I; for (I = 0; I
    
     
Child [I]! = NULL) del (root-> child [I]);} free (root);} int main () {int I, n; char str [20]; printf ("Enter the size of the trie tree to be created:"); scanf ("% d", & n); Trie_node root = NULL; root = CreateTrie (); if (root = NULL) printf ("failed to create the trie tree \ n"); for (I = 0; I
     
      

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.