A summary of common application of Trie tree (interview + code implementation) __trie

Source: Internet
Author: User
(i) Introduction to Trie
Trie tree, also called Dictionary tree, Word lookup tree or prefix tree, is a kind of multi fork tree structure for fast retrieval, such as English Alphabet Dictionary tree is a 26-fork tree, the number of dictionary tree is a 10-fork tree. His core idea is space for time, space consumption, but insert and query has excellent time complexity.
(ii) Definition of trie

The keys of the trie tree are not stored directly in the node, but are determined by the node's position in the tree. All descendants of a node have the same prefix (prefix), and all letters from the root node to the path of the current node form a string of the current position, which can hold the current string, the number of occurrences, the pointer array (pointing to the subtree), and whether it is a trailing flag, and so on.

[CPP] view plain copy typedef struct TRIE_NODE {char count[15];    The number of times the word prefix appears struct trie_node* NEXT[MAXN];    Pointer to each subtree bool exist; Whether the Mark node is a word}trie;

The trie tree can use the common prefix of strings to conserve storage space, as shown in the following illustration:

It has 3 basic properties:
(1) The root node does not contain characters, and each node outside the Roots node contains only one character.
(2) from the root node to a node, the path passed through the characters connected to the node corresponding to the string.
(3) All child nodes of each node contain different characters.
(iii) Basic operation of the trie tree
(1) Insert operation
Inserts the letter one by one, if the current letter is present, continues next, otherwise the current letter's node is new, so the time complexity of the insertion is only related to the length n of the string, and is O (n).

[CPP]  View Plain  copy Void insert (trie *root, char* s,char *add)    {        Trie *p=root;       while (*s!= ' ")        {           if (p->next [*s-' a ']==null)            {                p->next[*s-' a ']=createnode ();            }           p=p->next[ *s-' A '];          // p->count=add;            ++s;        }        p->exist=true;        strcpy (p->count,add);  }  &nbsP (2) Query operation
Similar to the insert operation, if the query on the way a node does not exist, then return directly. Otherwise continue, when the string ends, the trie tree also has a closing flag, then prove that the string exists, return true;

[CPP] view plain copy int Search (trie* root,const char* s) {Trie *p=root;           while (*s!= ') {p=p->next[*s-' a '];           if (P==null) return 0;       ++s;   Return p->count; }

(3) Delete operation

Generally, deletions to trie individual nodes are uncommon, so I'm only here to recursively delete the entire tree [CPP]   View plain  copy Void del ( Trie *root)    {       for (int i=0;i<maxn;i++)   

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.