C++字典树的实现

本文章参考力扣208与211,建议未学过的小伙伴去看看,本文章只附上字典树的建立,查找等

class Trie {
private:
    bool isEnd;
    Trie* next[26];//对于字典树 将字母映射为下标 字典树本质上是N叉树
public:
    Trie() {
        isEnd = false;
        memset(next, 0, sizeof(next));//初始化字典类时 初始化数组和判断变量
    }
    
    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;//最后完成插入后 对当前单词最后末尾进行设施是true
    }
    
    bool search(string word) {
        Trie* node = this;
        for (char c : word) {
            node = node->next[c - 'a'];
            if (node == NULL) {
                return false;//发现当前为空 直接返回false
            }
        }
        return node->isEnd;//递归到单词所在最后一层 查找的字母都存在 所以返回变量
    }
    
    bool startsWith(string prefix) {
        Trie* node = this;
        for (char c : prefix) {
            node = node->next[c-'a'];
            if (node == NULL) {
                return false;
            }
        }//前缀查找 一样  前缀查找如果都存在 则循环结束 最后直接返回true
        return true;
    }
};

力扣211 是上类字典树的变种,即单词中如果有".",其可以代表任何的字母 所以在匹配的时候遇见是要遍历当前层的所有结点 只要找到一个就可以满足

class TrieNode{
public:
    vector<TrieNode*> child;
    bool isWord;
    TrieNode() : child(26, nullptr), isWord(false) {};
    ~TrieNode() {
        for (auto c : child) delete c;
    }
};//此是实现字典的结构 不同于原始字典树 其用指针数组实现 这个注意
class WordDictionary {
public:
    /** Initialize your data structure here. */
    WordDictionary() {
        root = new TrieNode();//创建一个总的字典搜索树
    }
    ~WordDictionary() {
        delete root;
    }
    /** Adds a word into the data structure. */
    void addWord(string word) {
        TrieNode* p = root;
        for (char c : word) {
            int i = c - 'a';
            if (!p->child[i])
                p->child[i] = new TrieNode();
            p = p->child[i];
        }
        p->isWord = true;
    }//插入部分和原始字典树类似的
    
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    bool search(string word) {
        return match(word, root, 0);
    }
    //对于查找 是以单词每个字母都存在且递归次数等于单词长度一起来判断的
    bool match(string& word, TrieNode* p, int start) {
        if (!p) return false;//说明当前单词所在位是空 查找不存在 直接返回
        if (start == word.size()) return p->isWord;//每个字母都存在且长度一样
        char c = word[start];  //记录当前所查找单词的字母
        if (c != '.') { //如果不是'.' 则继续递归
            return match(word, p->child[c - 'a'], start + 1);
        } else {//如果是 则遍历当前层 只要有一个满足即可
            for (const auto& child : p->child) {
                if (match(word, child, start + 1))
                    return true;
            }
        }
        return false;
    }
private:
    TrieNode* root;
};

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * bool param_2 = obj.search(word);
 */

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值