leetcode208. 实现 Trie (前缀树)

实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。

示例:

Trie trie = new Trie();

trie.insert(“apple”); trie.search(“apple”); // 返回 true
trie.search(“app”); // 返回 false trie.startsWith(“app”); // 返回 true
trie.insert(“app”); trie.search(“app”); // 返回 true 说明:

你可以假设所有的输入都是由小写字母 a-z 构成的。 保证所有输入均为非空字符串。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Trie树(前缀树)也可以叫做字典树,Trie树的每个节点都有若干个字符指针,在插入操作和检索操作时扫描到一个字符c,就沿着当前结点继续走下去
Trie树有如下操作

初始化

一棵空的Trie树只含有一个头结点,该头结点的字符指针均为空

插入字符串

插入字符串word的时候,先令工作指针p指向头结点,然后依次遍历word中的每一个字符c
1,若p的c字符指针已存在,则p走到该结点
2,若p的c字符指针不存在,则新建该结点,然后让p的c字符指针指向该结点,然后p走到该结点
3,当word中的字符遍历完毕后,需要将最后一个字符的结点的end值设为true,表示这个字母是一个单词的结尾

检索字符串

检索字符串word时,先令工作指针p指向头结点,然后遍历字符串word中的每一个字符c
1,若p的c字符节点存在,则p沿着该字符节点走下去
2,若p的c字符节点不存在,则该单词不在Trie中,返回false
当最后一个字符节点的时候,需要判断这个字符是不是一个单词的末尾,查看该节点的end值即可

检索前缀

思路和检索字符串相似,只是少了需要判断是否是单词最后一个字母的操作
在这里插入图片描述
图片来自于李煜东著作的《算法竞赛进阶指南》
灰色表示该结点的字符为一个单词的末尾

该题目的cpp代码实现

class Trie {
public:

    struct node{
        node *next[26];//字符指针
        bool end;//该字母是否是单词最后一个字母
        node(){
            for(int i=0;i<26;i++)
                next[i]=nullptr; //初始化为空
            end=false;
        }
    };

    node *head;//头结点

    /** Initialize your data structure here. */
    Trie() {
        head=new node;
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        node *p=head;//工作指针p
        for(char c:word){
            //遍历字符串word
            c=c-'a';
            if(p->next[c]) //如果该结点存在
                p=p->next[c];
            else{
                p->next[c]=new node;//不存在建立该结点
                p=p->next[c];
            }
        }
        p->end=true;//最后一个字母
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        node *p=head;//p为工作指针
        for(char c:word){
            //遍历字符串word
            c=c-'a';
            if(p->next[c])//存在该结点,则继续往下查找
                p=p->next[c];
            else        //单词不在trie中
                return false;
        }
        return p->end;//判断是一个单词的前缀还是一个完整的单词
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        node *p=head;//p为工作指针
        for(char c:prefix){
            //遍历字符串word
            c=c-'a';
            if(p->next[c])//存在该结点,则继续往下查找
                p=p->next[c];
            else        //单词不在trie中
                return false;
        }
        return true;
    }
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值