[20 of the algorithm series] Trie, Series II trie

Source: Internet
Author: User

[20 of the algorithm series] Trie, Series II trie

I. Overview

The word search tree, also known as the Trie tree, is a tree structure and a variant of the hash tree. A typical application is used for statistics, sorting, and saving a large number of strings (but not limited to strings). Therefore, it is often used by the search engine system for text word frequency statistics.

Advantage 2

Use stringPublic prefixTo reduce the query time and minimize unnecessary string comparisons. The query efficiency is higher than that of hash tables.

Tri-Nature

(1) The root node does not contain any character. 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.

The word lists are "apps", "apply", "apple", "append", "back", "backen", and "basic", as shown in.

For example, when you save "apple" and "apply", you want them to share the same four letters and store the remaining parts separately. It is obvious that the letter tree makes good use of the common prefix of strings and saves storage space.

Application 4

(1) Fast string SEARCH

A Word Table consisting of N words and an article written in lowercase English are provided. Please write all the words not in the word list in the earliest order.
In this question, we can use array enumeration, hash, and dictionary tree to create a tree of mature words and then read the articles for comparison. This method is highly efficient.

(2) "string" sorting

Given N English names that are composed of only one word, you can use the dictionary tree to sort them from small to large output in Lexicographic Order and create a dictionary tree using arrays, all the sons at each node of the tree are clearly sorted by their letter size. Traverse the tree in sequence.

(3) longest public prefix

Create a dictionary tree for all strings. for the length of the longest common prefix of the two strings, that is, the number of common ancestor of the node where they are located, the problem is converted to the public ancestor problem at that time.

5. Implementation

The Insert, Delete, and Find operations of the dictionary tree are very simple. You can use a loop, that is, find the child tree corresponding to the first letter in the I cycle, and then perform the corresponding operation. To implement this dictionary tree, we can save it with the most common array. Of course, we can also use dynamic pointer types. There are three methods for pointing a node to a son:
(1) open a small array of letters for each node. The corresponding subscript is the letter represented by the Son, and the content is the position of the son corresponding to the big array, that is, the number;
(2) create a linked list for each node, and record who each son is in a certain order;
(3) use the expression of the Left son and right brother to record the tree.
Each of the three methods has its own merits. The first method is easy to implement, but the actual space requirements are large; the second method is,
It is easy to implement and has relatively small space requirements, but it is relatively time-consuming. Third, it requires the least space, but it is relatively time-consuming and not easy to write. But in general, several implementation methods are relatively simple, as long as you make a reasonable choice when doing the question.

/* ------------------------------------------- * Date: 2015-02-21 * Author: SJF0115 * question: 20. dictionary tree * Source: algorithm series * blog: ----------------------------------------------- */# include <iostream> # include <vector> # include <algorithm> using namespace std; # define MAX 26 struct TrieNode {// Number of words ending with this node int count; TrieNode * next [MAX]; TrieNode (int x): count (x) {for (int I = 0; I <MAX; ++ I) {next [I] = NULL;} // }}; // Insert void Insert (TrieNode * & root, string str) {int size = str. size (); int val; TrieNode * p = root; // insert for (int I = 0; I <size; ++ I) {val = str [I]-'A'; // if (p-> next [val] = NULL) {p-> next [val] = new TrieNode (0);} // if p = p-> next [val];} // for // end with this character p-> count ++;} // Delete void Delete (TrieNode * & root, string str) {int size = str. size (); int val; TrieNode * p = root; // insert for (int I = 0; I <size; ++ I) {val = str [I]-'A'; // The deleted string is not in the dictionary. if (p-> next [val] = NULL) {return ;} // if p = p-> next [val];} // for // end with this character p-> count --;} // Search for bool Search (TrieNode * root, string str) {if (root = NULL) {return false;} // if int size = str. size (); TrieNode * p = root; int val; for (int I = 0; I <size; ++ I) {val = str [I]-'A '; // cannot be transferred to the next character if (p-> next [val] = NULL) {return false ;} // if // continue with the next character p = p-> next [val];} // for return p-> count> 0 ;} // print the dictionary void PrintDic (TrieNode * root, vector <char> & words, vector <char> & word) {if (root = NULL) {return ;} // if (root-> count> 0) {words. push_back (word) ;}// if for (int I = 0; I <26; ++ I) {if (root-> next [I]) {word. push_back ('A' + I); PrintDic (root-> next [I], words, word); word. pop_back ();} // if} // for} int main () {TrieNode * root = new TrieNode (0 ); // insert string strs [] = {"OK", "applition", "app", "apple", "apply"}; for (int I = 0; I <5; ++ I) {Insert (root, strs [I]);} // for string str ("apple "); cout <"delete word [" <str <"] Query Result:" <endl; // query if (Search (root, str )) {cout <"word [" <str <"] in Dictionary" <endl ;} // if else {cout <"word [" <str <"] not in the dictionary" <endl ;} cout <"Delete word [" <str <"]" <endl; // Delete (root, str ); cout <"delete word [" <str <"]:" <endl; // query if (Search (root, str )) {cout <"word [" <str <"] in Dictionary" <endl ;} // if else {cout <"word [" <str <"] not in the dictionary" <endl ;}// dictionary list cout <"dictionary list: "<endl; vector <char> words; vector <char> word; PrintDic (root, words, word); for (int I = 0; I <words. size (); ++ I) {for (int j = 0; j <words [I]. size (); ++ j) {cout <words [I] [j];} // for cout <endl ;}// for return 0 ;}

Six references

Dictionary tree Trie

An Analysis of the Application of letter tree in the competition of Informatics

Talking about the suffix tree from the Trie tree (Dictionary tree) (revised in 10.28)

Please correct me if you have any questions. Thank you .......

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.