字典树实现

字典树实现

字典树

#include <iostream>
#include <vector>
using namespace std;
typedef struct TireNode{
    bool isWord;
    TireNode* next[26]{};
    TireNode()
    {
        isWord = false;
        for (auto & i : next) {
            i = nullptr;
        }
    }
}TireNode;

void insert(TireNode* root,string str)
{
    TireNode* ptr = root;
    char ch;
    for(int i=0;i<str.size();i++)
    {
        ch = str.at(i);
        if(ptr->next[ch-'a'] == nullptr)
        {
            ptr->next[ch-'a'] = new TireNode();
        }
        ptr = ptr->next[ch-'a'];
        if(i==str.size()-1)
        {
            ptr->isWord = true;
            break;
        }
    }
}

bool search(TireNode* root,string str)
{
    TireNode* ptr = root;
    char ch;
    for(int i=0;i<str.size();i++)
    {
        ch = str.at(i);
        if(ptr->next[ch-'a']!=nullptr)
        {
            ptr = ptr->next[ch-'a'];
            if(i==(str.size()-1)&&ptr->isWord)
                return true;
        }
    }
    return false;
}

string maxProfix(TireNode* root,string str)
{
    TireNode* ptr = root;
    string s;
    char ch;
    for (int i = 0; i < str.size(); ++i) {
        ch = str.at(i);
        if(ptr->next[ch-'a']!= nullptr)
        {
            s.push_back(ch);
            ptr = ptr->next[ch-'a'];
        }
        else
        {
            break;
        }
    }
    return s;
}

int main() {
    auto * root = new TireNode();
    vector<string> words;
    words.emplace_back("abc");
    words.emplace_back("bcd");
    words.emplace_back("bcde");
    words.emplace_back("bcdef");
    for (const auto& word:words) {
        insert(root,word);
    }
    string s;
    while(cin>>s)
    {
        cout<<"搜索结果:"<<search(root,s)<<endl;
        cout<<"最大前缀:"<<maxProfix(root,s)<<endl;
    }

}
### 使用字典树 (Trie) 实现平衡树的概念分析 字典树(Trie)是一种特殊的树形数据结构,主要用于高效存储和检索字符串集合。然而,Trie 并不是一种天然支持平衡特性的数据结构[^4]。 #### Trie 和平衡二叉树的区别 - **Trie 的特性** - 字典树通过字符路径来表示单词,在节点处并不保存完整的关键词。 - 每个节点可以有多个子节点,通常对应于字母表中的不同字符。 - **平衡二叉树的特点** - 平衡二叉树旨在保持树的高度尽可能低,从而确保高效的查找性能。 - 插入新元素时需要执行旋转等操作以维持树的平衡状态[^1]。 由于这两种类型的树设计目的不同,直接将 Trie 改造成具备自平衡功能的数据结构存在理论上的困难: - Trie 主要关注前缀匹配问题; - 自平衡机制则更侧重于维护整体高度的一致性。 因此,不存在标准做法能够使普通的 Trie 成为具有自动调整能力的平衡树。 不过,如果目标是在基于 Trie 结构上实现某种形式的“平衡”,可以通过以下方式间接达成: - 对每层的最大宽度加以限制,防止某一层过度扩展而影响其他层次; - 定期重构整个 Trie 或部分子树,使其趋于更加紧凑的形式; 但是这些方法并不能严格意义上定义为传统意义上的平衡策略。 ```python class TrieNode: def __init__(self): self.children = {} self.is_end_of_word = False def insert(root, word): node = root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_end_of_word = True # 构建一个简单的 Trie 示例 root = TrieNode() insert(root, 'apple') insert(root, 'app') ``` 尽管如此,对于大多数应用场景而言,没有必要追求让 Trie 达到像 AVL 或红黑树那样的精确平衡效果。相反,应该根据具体需求选择合适的数据结构——当涉及到频繁插入删除并希望保证良好性能时,建议优先考虑使用已有的平衡二叉搜索树实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值