1.. Trie通常被称为"字典树"或"前缀树"
  • Trie的形象化描述如下图:
  • Trie的优势和适用场景
2.. 实现Trie
  • 实现Trie的业务无逻辑如下:
  • import java.util.TreeMap;
    
    public class Trie {
    
        private class Node {
    
            public boolean isWord;
    public TreeMap<Character, Node> next; // 构造函数
    public Node(boolean isWord) {
    this.isWord = isWord;
    next = new TreeMap<>();
    } // 无参数构造函数
    public Node() {
    this(false);
    }
    } private Node root;
    private int size; // 构造函数
    public Trie() {
    root = new Node();
    size = 0;
    } // 实现getSize方法,获得Trie中存储的单词数量
    public int getSize() {
    return size;
    } // 实现add方法,向Trie中添加新的单词word
    public void add(String word) { Node cur = root;
    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);
    }
    if (!cur.isWord) {
    cur.isWord = true;
    size++;
    }
    } // 实现contains方法,查询Trie中是否包含单词word
    public boolean contains(String word) { Node cur = root;
    for (int i = 0; i < word.length(); i++) {
    char c = word.charAt(i);
    if (cur.next.get(c) == null) {
    return false;
    }
    cur = cur.next.get(c);
    }
    return cur.isWord; // 好聪明
    } // 实现isPrefix方法,查询Trie中时候保存了以prefix为前缀的单词
    public boolean isPrefix(String prefix) { Node cur = root;
    for (int i = 0; i < prefix.length(); i++) {
    char c = prefix.charAt(i);
    if (cur.next.get(c) == null) {
    return false;
    }
    cur = cur.next.get(c);
    }
    return true;
    }
    }

3.. Trie和简单的模式匹配

  • 实现的业务逻辑如下:
  • import java.util.TreeMap;
    
    class WordDictionary {
    
        private class Node {
    
            public boolean isWord;
    public TreeMap<Character, Node> next; public Node(boolean isWord) {
    this.isWord = isWord;
    next = new TreeMap<>();
    } public Node() {
    this(false);
    } } /**
    * Initialize your data structure here.
    */
    private Node root; public WordDictionary() {
    root = new Node();
    } /**
    * Adds a word into the data structure.
    */
    public void addWord(String word) {
    Node cur = root;
    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);
    }
    cur.isWord = true;
    } /**
    * 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 (index == word.length()) {
    return node.isWord;
    } char c = word.charAt(index);
    if (c != '.') {
    if (node.next.get(c) == null) {
    return false;
    }
    return match(node.next.get(c), word, index + 1);
    } else {
    for (char nextChar : node.next.keySet()) {
    if (match(node.next.get(nextChar), word, index + 1)) {
    return true;
    }
    }
    return false;
    }
    }
    }

最新文章

  1. Ubuntu安装SSH服务器故障分析及解决办法(错误1:E:软件包 openssh-server 还没有可供安装的候选者,错误2:E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系)
  2. Spring-boot-admin功能说明
  3. winform学习笔记02
  4. C++ 之引用
  5. 编辑器Ultraedit快捷键
  6. 原生JS中apply()方法的一个值得注意的用法
  7. Spark入门实战系列--6.SparkSQL(下)--Spark实战应用
  8. 【BZOJ-4568】幸运数字 树链剖分 + 线性基合并
  9. Django笔记-字符编码相关问题整理
  10. 谈谈我的编程之路---WAMP(三)
  11. paip.Log4j配置不起作用的解决
  12. WCF 入门 (17)
  13. 【学习笔记】【C语言】逻辑运算符
  14. Children of the Candy Corn (bfs+dfs)
  15. debian linux 中如何查看软件包是否已经安装和如何安装、卸载软件
  16. Unix/Linux环境C编程入门教程(25) C/C++字符测试那些事儿
  17. C++的运算符
  18. FFmpeg任意文件读取漏洞分析
  19. 我只是想获取access_token而已
  20. Windows Server 2016-三种方法备份还原DHCP服务器

热门文章

  1. python 字典 day6
  2. 与soul上的一个妹子聊天有感
  3. 【Vue2.x笔记3】从源码看watch对象
  4. window服务session隔离
  5. dva-loading 实践用法
  6. Nginx+uWSGI部署flask项目
  7. Arcgis runtime sdk .net 二次开发
  8. JavaScript-事件类型
  9. MySQL进阶之存储引擎MyISAM与InnoDB的区别
  10. cmdb实现三种方式