ZBLOG

将python里的trie = Trie(hotwords_set)用C++实现,包括具体实现及头文件

下面是一个用C++实现的Trie树,包含基本的插入和查找功能。这个示例可以被用于实现类似于Python中的trie = Trie(hotwords_set)的功能。

Trie.h

#ifndef TRIE_H
#define TRIE_H

#include <unordered_map>
#include <string>

class TrieNode {
public:
    std::unordered_map<char, TrieNode*> children;
    bool isEndOfWord;

    TrieNode() : isEndOfWord(false) {}
};

class Trie {
private:
    TrieNode* root;

public:
    Trie();
    ~Trie();
    
    void insert(const std::string& word);
    bool search(const std::string& word);
    bool startsWith(const std::string& prefix);

private:
    void destroy(TrieNode* node);
};

#endif // TRIE_H

Trie.cpp

#include "Trie.h"

// Constructor to initialize the root of the trie
Trie::Trie() {
    root = new TrieNode();
}

// Destructor to clean up memory
Trie::~Trie() {
    destroy(root);
}

// Recursive function to delete nodes in the trie
void Trie::destroy(TrieNode* node) {
    for (auto it : node->children) {
        destroy(it.second);
    }
    delete node;
}

// Insert a word into the trie
void Trie::insert(const std::string& word) {
    TrieNode* current = root;
    
    for (char c : word) {
        if (current->children.find(c) == current->children.end()) {
            current->children[c] = new TrieNode();
        }
        current = current->children[c];
    }
    
    current->isEndOfWord = true; // Mark the end of the word
}

// Search for a complete word in the trie
bool Trie::search(const std::string& word) {
    TrieNode* current = root;
    
    for (char c : word) {
        if (current->children.find(c) == current->children.end()) {
            return false; // Not found
        }
        current = current->children[c];
    }
    
    return current->isEndOfWord; // Return true only if it's an end of a valid word
}

// Check if any words in the trie start with the given prefix
bool Trie::startsWith(const std::string& prefix) {
    TrieNode* current = root;
    
    for (char c : prefix) {
        if (current->children.find(c) == current->children.end()) {
            return false; // Not found
        }
        current = current->children[c];
    }

    return true; // Prefix exists in the trie
}

使用示例

#include <iostream>
#include "Trie.h"

int main() {
   // 创建一个空的trie树实例 
   Trie trie;

   // 假设我们有一些热词 
   std::vector<std::string> hotwords_set = {"hello", "world", "trie", "data", "structure"};

   // 将热词插入到trie中 
   for (const auto &word : hotwords_set) {
       trie.insert(word);
   }

   // 测试搜索单词 
   std::cout << "Search 'hello': " << trie.search("hello") << std::endl;  // 输出: 1(true)
   std::cout << "Search 'world': " << trie.search("world") << std::endl;  // 输出: 1(true)
   std::cout << "Search 'hi': " << trie.search("hi") << std::endl;        // 输出: 0(false)

   // 测试前缀 
   std::cout << "Starts with 'da': " << trie.startsWith("da") << std::endl;  // 输出: 1(true)
   std::cout << "Starts with 'tri': " << trie.startsWith("tri") << std::endl; // 输出: 1(true)
   
   return 0;
}

以上代码展示了如何在C++中实现一个简单的Trie数据结构,包括插入、搜索和前缀检查功能。希望这对你有所帮助!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://sdn.0voice.com/?id=7731

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?