c++:3.单词查询。能实现模糊查询。如输入字母a,ab,则能展示a或ab为前缀的所有单词。基于树的先序遍历展示。
时间: 2024-03-19 19:45:39 浏览: 26
好的,以下是基于 Trie 树的 C++ 代码实现,可以实现单词查询和模糊查询:
```cpp
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class TrieNode {
public:
unordered_map<char, TrieNode*> children;
bool is_word;
TrieNode() {
is_word = false;
}
};
class Trie {
public:
TrieNode* root;
Trie() {
root = new TrieNode();
}
void insert(string word) {
TrieNode* node = root;
for (char c : word) {
if (node->children.find(c) == node->children.end()) {
node->children[c] = new TrieNode();
}
node = node->children[c];
}
node->is_word = true;
}
bool search(string word) {
TrieNode* node = root;
for (char c : word) {
if (node->children.find(c) == node->children.end()) {
return false;
}
node = node->children[c];
}
return node->is_word;
}
vector<string> starts_with(string prefix) {
vector<string> res;
TrieNode* node = root;
for (char c : prefix) {
if (node->children.find(c) == node->children.end()) {
return res;
}
node = node->children[c];
}
dfs(node, prefix, res);
return res;
}
void dfs(TrieNode* node, string prefix, vector<string>& res) {
if (node->is_word) {
res.push_back(prefix);
}
for (auto child : node->children) {
dfs(child.second, prefix + child.first, res);
}
}
};
int main() {
Trie trie;
trie.insert("apple");
trie.insert("banana");
trie.insert("orange");
trie.insert("pear");
trie.insert("peach");
cout << "Search 'apple': " << trie.search("apple") << endl; // true
cout << "Search 'grape': " << trie.search("grape") << endl; // false
vector<string> res1 = trie.starts_with("a");
cout << "Starts with 'a': ";
for (string s : res1) {
cout << s << " ";
}
cout << endl;
vector<string> res2 = trie.starts_with("pe");
cout << "Starts with 'pe': ";
for (string s : res2) {
cout << s << " ";
}
cout << endl;
return 0;
}
```
在这个实现中,我们定义了 TrieNode 类来表示 Trie 树中的节点。每个节点都有一个 children 哈希表,其中键是字符,值是对应的子节点。我们还在每个节点上存储一个布尔值 is_word,以指示该节点是否表示单词的结尾。
然后,我们定义了一个 Trie 类来管理 Trie 树。它有一个根节点 root,并定义了 insert、search 和 starts_with 三个方法。insert 方法用于将单词插入 Trie 树中,search 方法用于检查 Trie 树中是否存在给定的单词,starts_with 方法则用于查找以给定前缀开头的所有单词。
在 starts_with 方法中,我们首先遍历 Trie 树,直到找到与给定前缀匹配的节点。然后,我们调用 dfs 方法进行深度优先搜索,以查找以该节点为根的子树中的所有单词。如果节点的 is_word 属性为 true,则将当前前缀添加到结果列表中。最后,我们返回结果列表。
使用上述代码,您可以实现您所需的单词查询功能。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)