[LeetCode]208. Implement Trie (Prefix Tree)

本文介绍了一种实现Trie(字典树)的方法,包括插入、搜索和查找前缀的功能。通过定义TrieNode类来构建字典树结构,实现了高效的字符串匹配算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[LeetCode]208. Implement Trie (Prefix Tree)

题目描述

这里写图片描述

思路

实现字典树的插入、搜索和查找过程
详见代码中的注释

代码

#include <iostream>
#include <vector>

using namespace std;

//定义字典树节点
class TrieNode {
public:
    char content;
    bool isEnd;
    int childrenNum;
    //子节点vector 不止一个子节点
    vector<TrieNode*> children;

    //构造函数
    TrieNode() : content(' '), isEnd(false), childrenNum(0) {}
    TrieNode(char ch) : content(ch), isEnd(false), childrenNum(0) {}

    //查找字符是否存在子节点中,若存在,则返回节点,不存在返回null
    TrieNode* findChild(char ch) {
        if (children.size())
            for (auto p : children)
                if (ch == p->content)
                    return p;
        return nullptr;
    }

    //析构
    ~TrieNode() {
        for (auto child : children)
            delete child;
    }
};

class Trie {
public:
    //构造函数,初始化根节点
    Trie() {
        root = new TrieNode();
    }

    ~Trie() {
        delete root;
    }

    //插入一个单词
    void insert(string word) {
        //首先查找单词是否存在字典树,若存在直接返回
        if (search(word))
            return;
        //初始化当前节点为根节点
        TrieNode* cur = root;
        //遍历单词中的字符
        for (char ch : word) {
            //查找当前节点的子节点中是否含有字符
            TrieNode* child = cur->findChild(ch);
            if (child == nullptr) {
                //若不存在,新建节点,将节点联入字典树中,并更新当前节点为新建的节点
                TrieNode* newNode = new TrieNode(ch);
                cur->children.push_back(newNode);
                ++cur->childrenNum;
                cur = newNode;
            }
            else {
                //若存在,将当前节点更新为子节点
                cur = child;
            }
        }
        //将word的最后一个字符设置isend为true,表明根节点到当前节点存储了一个单词
        cur->isEnd = true;
    }

    bool search(string word) {
        //初始化当前节点为根节点
        TrieNode* cur = root;
        //遍历查找
        for (char ch : word) {
            TrieNode* node = cur->findChild(ch);
            //如果字符不存在,返回false
            if (node == nullptr)
                return false;
            //更新当前节点
            cur = node;
        }
        //检查最后字符的isend,如果不为true,则表明查找到的只是其他单词的前缀
        return cur->isEnd == true;
    }

    bool startsWith(string prefix) {
        TrieNode* cur = root;
        for (char ch : prefix) {
            TrieNode* node = cur->findChild(ch);
            if (node == nullptr)
                return false;
            cur = node;
        }
        return true;
    }

private:
    TrieNode* root;
};

int main() {
    Trie* trie = new Trie();
    trie->insert("test");
    cout << "search(test) " << trie->search("test") << endl;
    cout << "search(tes) " << trie->search("tes") << endl;
    cout << "startWith(tes) " <<trie->startsWith("tes") << endl;

    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值