Implement Trie (Prefix Tree)

tire tree 实现

定义:(wiki)

In computer science, a trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes), is a kind of search tree—an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings. Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree defines the key with which it is associated. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string. Values are not necessarily associated with every node. Rather, values tend only to be associated with leaves, and with some inner nodes that correspond to keys of interest. For the space-optimized presentation of prefix tree, see compact prefix tree.

In the example shown, keys are listed in the nodes and values below them. Each complete English word has an arbitrary integer value associated with it. A trie can be seen as a tree-shapeddeterministic finite automaton. Each finite language is generated by a trie automaton, and each trie can be compressed into a deterministic acyclic finite state automaton.

Though tries are usually keyed by character strings, they need not be. The same algorithms can be adapted to serve similar functions of ordered lists of any construct, e.g. permutations on a list of digits or shapes. In particular, a bitwise trie is keyed on the individual bits making up any fixed-length binary datum, such as an integer or memory address.


LeetCode :208. Implement Trie (Prefix Tree)

Implement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

Subscribe to see which companies asked this question.

class Trie {
public:
    /** Initialize your data structure here. */
    Trie() {
        root = new Trie_node;
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        if(root == NULL) root = new Trie_node; 
        int position=0; // indexes letters of new Trie
        char next_char;
        Trie_node * location = root; // moves through the trie
        while(location != NULL &&
        	(next_char = key_letter(word, position)) !=0){
        				//Terminate for a NULL location or a blank in the target
        	int next_position = alphabetic_order(next_char);
        	if(location->branch[next_position] == NULL){
        		location->branch[next_position] = new Trie_node;
        	}
        	location = location->branch[next_position];
        	position++;
        }

        if(location->data == NULL) {
            location->data = new string(word);
            
        } 
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        int position = 0;
        char next_char;
        Trie_node * location = root;
        while(location != NULL &&
        	(next_char = key_letter(word, position)) != 0){
        	location = location->branch[alphabetic_order(next_char)];
        		//move down the appropriate branch of the trie
        	position++; //next character of the target
        }
        if(location!=NULL && location->data != NULL){
        	return true;
        }
        else return false;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        int position = 0;
        char next_char;
        Trie_node * location = root;
        while(location != NULL &&
        	(next_char = key_letter(prefix, position)) != 0){
        	location = location->branch[alphabetic_order(next_char)];
        		//move down the appropriate branch of the trie
        	position++; //next character of the target
        }
        if(location != NULL)return true;
        else return false;
    }
    
    struct Trie_node {
        string *data;
        Trie_node * branch[26];
        
        Trie_node(){
            data = NULL;
            for(int i=0;i<26;i++){
                branch[i]=NULL;
            }
        }//all null

        


    };

private:
    Trie_node * root;

    int alphabetic_order(char symbol){
   		return (int)symbol - 97;
   	}

   	char key_letter(string data,int position){
    	if(data.length()<position) return 0;
    	else return data[position];
   	}
    
};

/**
 * 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);
 */


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值