C + + Dictionary tree

Source: Internet
Author: User

Most of the time, learning always ends with the realization, because there are a lot of problems like that or that. Even if you understand this structure, but realize it is another world, practice, look at the source code, and then continue to achieve is at this stage I should do things.

This time to learn the dictionary tree.

A dictionary tree is a special treatment of a tree structure. For the English Dictionary, 26 English letters can be any combination, then the tree must be 26 fork. So what does this tree do,

1. The application of the dictionary tree in the fast retrieval of strings.
Give a cooked word list of n words, and an article written in lowercase English, please write all the new words that are not in the cooked vocabulary in the first order of occurrence. In this problem, we can use the dictionary tree, first put the cooked word to build a tree, and then read into the article to compare, this method of efficiency is relatively high.

2. The application of the Dictionary tree in "string" sorting
Given n a different English name consisting of only one word, let you sort them in dictionary order from small to large output with a dictionary tree, using an array of ways to create a dictionary tree, and all the sons of each node of the tree are clearly sorted by their letter size. This tree is preceded by a sequential traversal.

3. Application of the dictionary tree in the longest common prefix problem
To create a dictionary tree for all strings, the length of the longest public prefix for two strings is the number of common ancestors of their nodes, so the problem is transformed into the nearest common ancestor problem.

In the process of looking at someone else's code, I have a lot of questions. The first is the invocation of a class method in the encapsulated class, which should be divided into static class methods and instance class methods. The instance class method needs to be instantiated first. Another problem is the difference between struct and class in C + +, the difference is not only the default keyword public and private, the struct is pub, and class is a PRI.

  

////main.cpp//yy////Created by madmarical on 15/11/25.//Copyright (c) 2015 COM. All rights reserved.//#include<iostream>#include<string>#include<vector>#include<algorithm>#include<stdexcept>using namespacestd;structnode{BOOLIsword;//determines whether the current letter is the last letter of the wordNode *next[ -];//26 ForksNode () {Isword=false;  for(inti =0; I! = -; ++i) {Next[i]= NULL;//Empty 26 Forks        }    }};classdictree{ Public: Node*root;//root nodeDictree () {root= NULL;//initializing in Constructors    }    voidInsert (stringstr) {        if(!root) {Root=NewNode;//If Root is not now empty, you need to re-open a root node} Node*head = root;//root node can not change, find a new pointer to the root node convenient operation         for(Long inti =0; I! = Str.length (); ++i) {intnum = str[i]-'a';//gets the location where the current letter should be stored            if(Head->next[num] = =NULL) {Head->next[num] =NewNode; } head= head->Next[num]; } head->isword =true; }    BOOLSearch (stringstr) {Node*head =Root;  for(Long inti =0; I! = Str.length (); ++i) {intnum = str[i]-'a'; if(Head->next[num] = =NULL) {                return false; }            Else{Head= head->Next[num]; }        }        returnHead->Isword; }};intMainintargcConst Char*argv[]) {    stringINP1; CIN>>INP1;    Dictree A;        A.insert (INP1); stringinp2; CIN>>inp2;    A.search (INP2); return 0;}

Reflection:

1. The book to time side hate less ... Quick reading ...

  

  

C + + Dictionary tree

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.