Data Structure-Trie tree

Source: Internet
Author: User

[Cpp]/********************************** * ********************** data structure: the Trie tree, also known as the word search tree or dictionary tree, is a tree structure and a variant of the hash tree. Basic principle: the core idea of the Trie tree is to change space time, the common prefix of a string is used to reduce the query time overhead to improve efficiency. Application: used to count and sort 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: to minimize unnecessary string comparisons, the query efficiency is higher than that of hash tables. Basic Features: (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, it is the character string corresponding to the node. (3) The characters in all subnodes of each node are different; **************************************** ***************** **/# Include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> # include <climits> # include <algorithm> using namespace std; const int MAX = 26; struct Trie // Trie node declaration {bool isStr; // mark whether a Trie * next [MAX] string is formed at the node; // a pointer array, hold pointer to each son node}; void insert (Trie * root, const char * s) // insert word s into the dictionary tree {if (root = NULL | * s = '\ 0') return; Trie * p = root; while (* s) {if (p-> next [* s-'a'] = NULL) // if the section containing this character does not exist Node {Trie * temp = new Trie; for (int I = 0; I <MAX; I ++) {temp-> next [I] = NULL ;} temp-> isStr = false; p-> next [* s-'a'] = temp; p = p-> next [* s-'a'];} else {p = p-> next [* s-'a'];} s ++; // Let the pointer s point to the next character} p-> isStr = true; // mark the end of a word here to form a string} int search (Trie * root, const char * s) // query whether a word s already exists {Trie * p = root; while (p & * s) {p = p-> next [* s-'a']; s ++;} return (p & p-> isStr); // The word exists only when it is marked as true at the end of the word} void del (Trie * r Oot) // release the heap space occupied by the entire dictionary tree {for (int I = 0; I <MAX; I ++) {if (root-> next [I]! = NULL) {del (root-> next [I]) ;}} delete root;} int main () {// freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt "," r ", stdin); char s [100]; Trie * root = new Trie; for (int I = 0; I <MAX; I ++) {root-> next [I] = NULL;} root-> isStr = false; int n, m; // n is the number of words entered in the Trie tree, and m is the number of words to be searched scanf ("% d", & n); getchar (); for (int I = 0; I <n; I ++) // create a dictionary tree {scanf ("% s", s); insert (root, s );} while (~ Scanf ("% d", & m) {if (! M) break; for (int I = 0; I <m; I ++) // query {scanf ("% s", s); if (search (root, s) printf ("YES \ n"); else printf ("NO \ n");} printf ("\ n");} del (root ); // It is important to release space. return 0 ;}

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.