10.5 Trie字典树和简单模式匹配

10.5 Trie字典树和简单模式匹配

Tip:本博客内容是通过学习慕课网bobo老师视频做的笔记总结,不用于任何商业用途,只用于帮助更多技术爱好者。

(1) Leetcode题目(添加与搜索单词 - 数据结构设计  第211号题目)

https://leetcode-cn.com/problemset/all/?search=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

 

说明:

你可以假设所有单词都是由小写字母 a-z 组成的。

 

(2) Trie字典树的Java语言实现案例

package com.wwl.trie;


import java.util.TreeMap;



class WordDictionary {

    private class Node {

        private boolean isWord;

        private TreeMap<Character, Node> next;



        public Node(boolean isWord) {

            this.isWord = isWord;

            next = new TreeMap<>();

        }



        public Node() {

            this(false);

        }

    }



    // Trie树根节点定义

    private Node root;

    // Trie树存储单词数

    private int size;



    // 获取Trie树存储的单词数

    public int getSize() {

        return size;

    }



    /**

     * Initialize your data structure here.

     */

    public WordDictionary() {

        root = new Node();

    }



    /**

     * Adds a word into the data structure.

     */

    public void addWord(String word) {

        // 字符串判空操作

        if (word == null) {

            return;

        }

        // 定义当前节点

        Node cur = root;

        // 对新加入Trie树的词汇进行识别

        // 第一:如果新加入的字符不存在,则在当前node节点创建一个新的节点树;

        // 第二:如果新加入的字符存在,则找到存在的字符节点,然后赋值给当前节点,继续下一步识别。

        for (int i = 0; i < word.length(); i++) {

            char c = word.charAt(i);

            if (cur.next.get(c) == null) {

                cur.next.put(c, new Node());

            }

            cur = cur.next.get(c);

        }



        // 第一:如果当前加入的单词不存在,则把isWord标志改成true,表示加入的单词最后一个字符表示单词结尾;

        // 第二:如果当前假如的字符存在,则不进行逻辑处理。

        if (!cur.isWord) {

            cur.isWord = true;

            size++;

        }

    }



    /**

     * Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.

     */

    public boolean search(String word) {

        return match(root, word, 0);

    }



    private boolean match(Node node, String word, int index) {

        // 字符串判空操作

        if (word == null) {

            return false;

        }



        // 如果单词的长度等于索引长度,则表示单词中所有字符都递归完毕

        if (word.length() == index) {

            return node.isWord;

        }



        char c = word.charAt(index);

        // 对非.字符的处理逻辑:递归匹配

        if (c != '.') {

            if (node.next.get(c) == null) {

                return false;

            } else {

                return match(node.next.get(c), word, index + 1);

            }

        }

        // 对.字符处理逻辑:遍历node节点下的所有节点,并对每个节点进行递归匹配。

        else {

            for (char nextChar : node.next.keySet()) {

                if (match(node.next.get(nextChar), word, index + 1)) {

                    return true;

                }

            }

            return false;

        }

    }

}



/**

 * Your WordDictionary object will be instantiated and called as such:

 * WordDictionary obj = new WordDictionary();

 * obj.addWord(word);

 * boolean param_2 = obj.search(word);

 */

(3) Trie字典树的Java测试代码

package com.wwl.trie;



class WordDictionaryTest {

    public static void main(String[] args) {

        WordDictionary wordDictionary = new WordDictionary();



        wordDictionary.addWord("bad");

        wordDictionary.addWord("dad");

        wordDictionary.addWord("mad");



        System.out.println("是否匹配成功:" + wordDictionary.search("pad"));

        System.out.println("是否匹配成功:" + wordDictionary.search("bad"));

        System.out.println("是否匹配成功:" + wordDictionary.search(".ad"));

        System.out.println("是否匹配成功:" + wordDictionary.search("b.."));

        System.out.println("是否匹配成功:" + wordDictionary.search("..."));

    }

}

程序运行结果如下:

是否匹配成功:false

是否匹配成功:true

是否匹配成功:true

是否匹配成功:true

是否匹配成功:true

 

如果感兴趣的童鞋,可以观看我下一篇博客:10.5 Trie字典树和简单模式匹配

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值