Basically Trie (Prefix Tree) C ++ is

0x01. Description

  • Is a prefix tree data structure is a tree, is an atypical multi-tree structure, the number of branches of each node may have several.
  • Prefix tree is mainly used to retrieve a string of key data set.
  • Prefix tree also called tree dictionary prefix tree, trie and so on.
  • Trie [traɪ]Pronunciation and trythe same

0x02. Application prefix tree

This efficient data structures very versatile:

  • Search engine completion.
  • Spell Check.
  • Typing forecast.

0x03. Advantages prefix tree

Balanced trees and hash tables also allows us to focus the search word in the string data, but functional limitations, and with the increase in the hash table, conflicts will increase, efficiency will be degraded.

  • Prefix tree to find all the keys have the same prefix.
  • Prefix tree data sets can be enumerated by a string dictionary order.

0x04. Structure

Prefix tree has two main fields:

  • Links to child nodes, wherein each link corresponding to a letter of the alphabet in the data set. (Typically 26)
  • Boolean field, specify the key corresponding to the node is the end or just the key prefix.
struct TrieNode {
    bool isEnd; //该结点是否是一个串的结束
    TrieNode* next[26]; //字母映射表
};

0x05. Basic Operations

insert

Insert a word to the prefix tree.

Started from the sub-root node of the first character of the word match, no character has been matched to the corresponding prefix chain, and constantly open up new node, until you insert word of the last character, plus isEndthe value of .

  void insert(string word) {
        Trie*node=this;
        for(char c:word){
            if(node->next[c-'a']==NULL){
                node->next[c-'a']=new Trie();               
            }
            node=node->next[c-'a'];
        }
        node->isEnd=true;
    }
    

Inquire

Finding Triethe existence of the word word.

The root of the child nodes, has been matching down, if empty node appears on the return false, if the match to the last character, just judge node->isEnd.

bool search(string word) {
        Trie*node=this;
        for(char c:word){
            node=node->next[c-'a'];
            if(node==NULL) return false;
        }
        return node->isEnd;
    }
    

Prefix match

Analyzing Trieis or in a prefixprefixed words.

bool startsWith(string prefix) {
        Trie*node=this;
        for(char c:prefix){
            node=node->next[c-'a'];
            if(node==NULL) return false;
        }
        return true;
    }
    

0x06. Complete class

class Trie {
private:
    bool isEnd;
    Trie*next[26];
public:
    /** Initialize your data structure here. */
    Trie() {
        isEnd=false;
        memset(next, 0, sizeof(next));
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        Trie*node=this;
        for(char c:word){
            if(node->next[c-'a']==NULL){
                node->next[c-'a']=new Trie();               
            }
            node=node->next[c-'a'];
        }
        node->isEnd=true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        Trie*node=this;
        for(char c:word){
            node=node->next[c-'a'];
            if(node==NULL) return false;
        }
        return node->isEnd;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        Trie*node=this;
        for(char c:prefix){
            node=node->next[c-'a'];
            if(node==NULL) return false;
        }
        return true;
    }
};
/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */

ATFWUS --Writing By 2020–03–21

Published 130 original articles · won praise 146 · views 10000 +

Guess you like

Origin blog.csdn.net/ATFWUS/article/details/105016723