LeetCode 208. 实现 Trie (前缀树)

1 篇文章 0 订阅
1 篇文章 0 订阅
  • 很捞的写法,主要测试下对没对。就不写题解了
//
// Created by Anti on 2022/11/10.
//
#include <iostream>
#include <map>
#include <string>
using std::map;
using std::string;
class TrieNode
{
private:
    map<char, TrieNode *> children;
    char key;
    bool end_;

public:
    TrieNode(char c) : key(c), end_(false){};
    TrieNode *GetByKey(char key_)
    {
        auto it = children.find(key_);
        if (it == children.cend())
        {
            return nullptr;
        }
        return it->second;
    }
    char GetKey()
    {
        return key;
    }
    void AddChild(char key_)
    {
        children[key_] = new TrieNode(key_);
    }
    bool IsEnd()
    {
        return end_;
    }
    void SetEnd(bool end)
    {
        end_ = end;
    }
};
class Trie
{
public:
    Trie()
    {
        root_ = new TrieNode('\0');
    }

    void insert(string word)
    {
        auto current = root_;
        auto length = word.length();
        decltype(length) now = 0;
        while (now < length)
        {
            auto child = current->GetByKey(word[now]);
            if (child == nullptr)
            {
                current->AddChild(word[now]);
            }
            current = current->GetByKey(word[now]);
            now++;
        }
        current->SetEnd(true);
    }

    bool search(string word)
    {
        auto current = root_;
        auto length = word.length();
        decltype(length) now = 0;
        while (now < length)
        {
            auto child = current->GetByKey(word[now]);
            if (child == nullptr)
            {
                break;
            }
            current = child;
            now++;
        }
        return (now == length && current->IsEnd());
    }

    bool startsWith(string prefix)
    {
        auto current = root_;
        auto length = prefix.length();
        decltype(length) now = 0;
        while (now < length)
        {
            auto child = current->GetByKey(prefix[now]);
            if (child == nullptr)
            {
                break;
            }
            current = child;
            now++;
        }
        return (now == length);
    }

private:
    TrieNode *root_;
};
void test1()
{
    char c;
    string s;
    using std::cin;
    using std::cout;
    using std::endl;
    auto tree = new Trie();
    while (cin >> c)
    {
        cin >> s;
        switch (c)
        {
        case 'i': // insert
            tree->insert(s);
            break;
        case 's': // search
            cout << tree->search(s) << endl;
            break;
        case 'w': // startwith
            cout << tree->startsWith(s) << endl;
            break;
        default:
            cout << "no matching action";
            break;
        }
    }
}

void test2()
{
    char c;
    string s;
    using std::cin;
    using std::cout;
    using std::endl;
    auto tree = new Trie();
    Trie *obj = new Trie();
    string word = "apple";
    string prefix = "app";
    obj->insert(word);
    bool param_2 = obj->search(word);
    bool param_3 = obj->startsWith(prefix);
    cout << param_2 << " " << param_3 << endl;
}
int main()
{
    test1();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值