博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Implement Trie (Prefix Tree)
阅读量:4987 次
发布时间:2019-06-12

本文共 2925 字,大约阅读时间需要 9 分钟。

Implement a trie with insertsearch, and startsWith methods.

Note:

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

 

Analyse: For each node, it has 26 children, from a-z. 

1. For the string insertion operation, the procedure is:

    --Set root as the temp node;

    --For every character in the string, compute its corresponding place at the branch, and mark them.

    --After all characters are marked, set isVal to indicate it's a value rather than a prefix.

2. For the search operation, the procedure is:

    --Set root as the temp node;

  --For every character in the string, continuously check the existance of it. If NULL node encountered, jump out the of loop.

    --If the node is empty, then return false; Else, we need to judge whether this string is a value or a prefix.

3. For the startWith operation, it's quite similiar to the search operation except that all search operations with true result returned can return true in the start operation. 

 

Runtime: 64ms.

1 class TrieNode { 2 public: 3     // Initialize your data structure here. 4     TrieNode* childNode[26]; 5     bool isVal;//to indicate whether the leaf node is the target word or the prefix 6     TrieNode() { 7         isVal = false; 8         for(int i = 0; i < 26; i++) 9             childNode[i] = NULL;10     }11 };12 13 class Trie {14 public:15     Trie() {16         root = new TrieNode();17     }18 19     // Inserts a word into the trie.20     void insert(string word) {21         TrieNode* temp = root;22         for(int i = 0; i < word.size(); i++){23             if(temp->childNode[word[i] - 'a'] == NULL)24                 temp->childNode[word[i] - 'a'] = new TrieNode();25             temp = temp->childNode[word[i] - 'a'];26         }27         temp->isVal = true; //if the word is inserted, then isVal is true28     }29 30     // Returns if the word is in the trie.31     bool search(string word) {32         TrieNode* temp = root;33         for(int i = 0; i < word.size(); i++){34             if(temp == NULL) break;35             temp = temp->childNode[word[i] - 'a'];36         }37         if(!temp) return false; //if the route does not exist, return false38         return temp->isVal;  39     }40 41     // Returns if there is any word in the trie42     // that starts with the given prefix.43     bool startsWith(string prefix) {44         TrieNode* temp = root;45         for(int i = 0; i < prefix.size(); i++){46             if(temp == NULL) break;47             temp = temp->childNode[prefix[i] - 'a'];48         }49         if(temp) 50             return true; //if the route exists, return true51         else 52             return false; //if the route doesn't exists, return false53     }54 55 private:56     TrieNode* root;57 };58 59 // Your Trie object will be instantiated and called as such:60 // Trie trie;61 // trie.insert("somestring");62 // trie.search("key");

 

转载于:https://www.cnblogs.com/amazingzoe/p/4738251.html

你可能感兴趣的文章
启动Genymotion时报错Failed to initialize backend EGL display
查看>>
JavaScript与Objective-C之间的通信
查看>>
linux目录的读(r)、写(w)、执行(x)权限说明
查看>>
让Apache支持shtml实现include文件解析的配置方法
查看>>
Beta 冲刺(6/7)
查看>>
PHP之string之ltrim()函数使用
查看>>
行为型模式之中介者模式
查看>>
解题:洛谷3674 小清新人渣的本愿
查看>>
Python 3中字符串可以被改变吗?
查看>>
浅析Linux内核调度
查看>>
[转] Makefile 基础 (9) —— Makefile 使用make更新函数库文件
查看>>
Div自适应高度的方法
查看>>
Mha-Atlas-MySQL高可用
查看>>
集合与函数
查看>>
700. Search in a Binary Search Tree
查看>>
Mysql 存储引擎中InnoDB与Myisam的主要区别
查看>>
无刷新分页 jquery.pagination.js
查看>>
jQuery插件开发(转)
查看>>
Nova 无法向虚机注入密钥
查看>>
js清除浏览器缓存的几种方法
查看>>