【二叉树】208. 实现 Trie (前缀树)

208. 实现 Trie (前缀树)

解题思路

  • Node类:每个Node节点代表Trie树中的一个字符节点。它包含一个字符值val(虽然在这个实现中未直接使用),一个指向子节点的数组child,和一个布尔值end,表示该节点是否为某个单词的结尾。

  • child数组有26个元素,对应26个小写英文字母,用于存储当前节点的子节点。

  • end标志用来表示当前节点是否是某个单词的最后一个字符。
    Trie类:代表整个前缀树,包含一个根节点root。

  • insert(String word)方法用于向Trie树中插入一个新单词。它逐字符检查给定单词,如果当前字符在树中没有对应的子节点,则创建一个新的子节点。

  • search(String word)方法用于检查一个单词是否完整地存在于Trie树中。它按照单词的字符来遍历树,如果能够遍历完整个单词且最后一个字符节点的end为true,则表示单词存在。

  • startsWith(String prefix)方法用于检查Trie树中是否存在以给定前缀开始的任何单词。这个方法与search方法类似,但它只需要找到前缀即可,不需要验证最后一个字符的end值。

class Trie {

    private class Node{
        char val;
        Node[] child;
        boolean end;// 表示当前节点是不是一个单词的结尾

        // 构造函数
        public Node(char ch){
            val = ch;
            child = new Node[26];// 新建一个孩子节点
            end = false;
        }

        public Node(){
            child = new Node[26];
            end = false;
        }
    }

    Node root;

    public Trie() {
        root = new Node();// 创建根节点
    }
    
    public void insert(String word) {
        Node cur = root;
        for(int i = 0; i < word.length(); i++){
            // 取出当前字符
            char ch = word.charAt(i);

            if(cur.child[ch - 'a'] == null){
                // 当前节点的孩子节点为空节点  将当前字符插入
                cur.child[ch - 'a'] = new Node(ch);
            }
            // 更新当前节点cur
            cur = cur.child[ch - 'a'];
        }

        cur.end = true;// 如果一个单词的所有字符全部添加完毕  标记为true
    }
    
    public boolean search(String word) {
        Node cur = root;
        // 从根节点开始搜索  和插入的逻辑一样
        for(int i = 0; i < word.length(); i++){
            char ch = word.charAt(i);
            if(cur.child[ch - 'a'] == null){
                return false;//没有孩子节点
            }

            cur = cur.child[ch - 'a'];
        }

        return cur.end;

    }
    // 寻找是不是又该前缀字符串
    public boolean startsWith(String prefix) {
        Node cur = root;
        for(int i =0; i < prefix.length(); i++){
            char ch = prefix.charAt(i);
            if(cur.child[ch - 'a'] == null){
                return false;
            }
            cur = cur.child[ch - 'a'];
        }
        return true;
    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用二叉树实现Trie树(也称为字典树)。Trie树是一种用于高效存储和查找字符串的数据结构,它可以用于快速搜索和前缀匹配。 下面是一个使用二叉树实现Trie树的示例代码(C++): ```cpp #include <iostream> using namespace std; const int ALPHABET_SIZE = 26; // Trie节点 struct TrieNode { TrieNode* children[ALPHABET_SIZE]; bool isEndOfWord; }; // 初始化Trie节点 TrieNode* getNode() { TrieNode* node = new TrieNode; node->isEndOfWord = false; for (int i = 0; i < ALPHABET_SIZE; i++) { node->children[i] = nullptr; } return node; } // 插入字符串到Trie树 void insert(TrieNode* root, string key) { TrieNode* curr = root; for (int i = 0; i < key.length(); i++) { int index = key[i] - 'a'; if (curr->children[index] == nullptr) { curr->children[index] = getNode(); } curr = curr->children[index]; } curr->isEndOfWord = true; } // 查找字符串是否在Trie树中 bool search(TrieNode* root, string key) { TrieNode* curr = root; for (int i = 0; i < key.length(); i++) { int index = key[i] - 'a'; if (curr->children[index] == nullptr) { return false; } curr = curr->children[index]; } return (curr != nullptr && curr->isEndOfWord); } int main() { TrieNode* root = getNode(); insert(root, "apple"); insert(root, "banana"); insert(root, "orange"); cout << "Search 'apple': " << (search(root, "apple") ? "Found" : "Not Found") << endl; cout << "Search 'banana': " << (search(root, "banana") ? "Found" : "Not Found") << endl; cout << "Search 'orange': " << (search(root, "orange") ? "Found" : "Not Found") << endl; cout << "Search 'grape': " << (search(root, "grape") ? "Found" : "Not Found") << endl; return 0; } ``` 这段代码创建了一个Trie树,并演示了插入字符串和搜索字符串的过程。你可以根据需要修改和扩展它,以满足你的具体需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少写代码少看论文多多睡觉

求打赏,求关注,求点赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值