208. 实现 Trie (前缀树)&211. 添加与搜索单词 - 数据结构设计

208.实现一个 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
211.设计一个支持以下两种操作的数据结构:

void addWord(word)
bool search(word)
search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。

示例:

addWord(“bad”)
addWord(“dad”)
addWord(“mad”)
search(“pad”) -> false
search(“bad”) -> true
search(".ad") -> true
search(“b…”) -> true

解题思路:
新建一个26叉树,按照a-z的顺序编号,每个节点均有26个子节点,若当前节点root其后的第一个字母为’c’,即第三个子树有字符c,即root->next[3]=‘c’,其余均为NULL。
1.插入单词操作
即寻找到单词的第一个字符在26叉中的编号,插入当前节点的子节点的相应位置即可,并且需在单词的结尾处予以结尾标记。
2.寻找单词操作
挨个把单词中的字符所对应编号找出,深入每一个子节点的子节点的相应位置查找是否为空,若为空就返回错误,同时需注意判断单词最后一个字符在26叉树中是否有结尾标记。
3.起始判断
与2大同小异,只是无需再加判断结尾标记。

211:代码中有相应注释

208.C++代码如下:
struct TreeNode26
{
char val;
TreeNode26* next[26];
bool isLast=false;
TreeNode26(char v)
{
val=v;
for(int i=1;i<=26;i++)
next[i-1]=NULL;
}

bool hasChild26()
{
    for(int i=1;i<=26;i++)
 {
      if(next[i-1])return true;
    }
    return false;
}

};
class Trie {
public:
TreeNode26* root;
/** Initialize your data structure here. */
Trie()
{
root=new TreeNode26(’ ');
}

/** Inserts a word into the trie. */
void insert(string word) 
{
    int size=word.size();
    TreeNode26* node=root;
    char ch;
    for(int i=1;i<=size;i++)
    {
        ch=word[i-1];
        if(!node->next[int(ch-'a')])
            node->next[int(ch-'a')]=new TreeNode26(ch);
        node=node->next[int(ch-'a')];
        node->val=ch;
    }
    node->isLast=true;
}

/** Returns if the word is in the trie. */
bool search(string word) 
{
    int size=word.size();
    TreeNode26* node=root;
    char ch;
    for(int i=1;i<=size;i++)
    {
        ch=word[i-1];
        if(node->next[int(ch-'a')])
            node=node->next[int(ch-'a')];
        else return false;
    }
    return node->isLast;
}

/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) 
{
    int size=prefix.size();
    TreeNode26* node=root;
    char ch;
    for(int i=1;i<=size;i++)
    {
        ch=prefix[i-1];
        if(node->next[int(ch-'a')])
            node=node->next[int(ch-'a')];
        else 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);
    /
    211.C++代码如下
    struct TreeNode26
    {
    char val;
    TreeNode26
    next[26];
    bool isLast=false;
    TreeNode26(char v)
    {
    val=v;
    for(int i=1;i<=26;i++)
    next[i-1]=NULL;
    }
    bool hasChild26()
    {
    for(int i=1;i<=26;i++)
    if(next[i-1])return true;
    return false;
    }
    };
    class WordDictionary
    {
    public:
    /** Initialize your data structure here. /
    TreeNode26
    root;
    string data;
    WordDictionary()
    {
    root=new TreeNode26(’ ');
    }

    /** Adds a word into the data structure. /
    void addWord(string word)
    {
    int size=word.size();
    TreeNode26
    node=root;
    for(int i=1;i<=size;i++)
    {
    int pos=word[i-1]-‘a’;//找到当前字符的序号
    if(node->next[pos]==NULL)
    node->next[pos]=new TreeNode26(word[i-1]);//接续到当前后面
    node=node->next[pos];
    }
    node->isLast=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)
    {
    data=word;
    return DFS(root,0);
    }
    bool DFS(TreeNode26
    node,int pos)
    {
    char ch=data[pos];//挨个提取字符
    if(posint(data.size()))//判断是否到达单词的末尾字符
    return node->isLast;
    if(ch
    ’.’)
    {
    for(int i=1;i<=26;i++)
    if(node->next[i-1]&&DFS(node->next[i-1],pos+1))return true;//遇到’.'即可继续DFS下一层
    return false;
    }else
    {
    if(node->next[ch-‘a’])return DFS(node->next[ch-‘a’],pos+1);
    return false;
    }
    }
    };

/**

  • Your WordDictionary object will be instantiated and called as such:
  • WordDictionary obj = new WordDictionary();
  • obj.addWord(word);
  • bool param_2 = obj.search(word);
    */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值