1.. Trie通常被称为"字典树"或"前缀树"
  • Trie的形象化描述如下图:
  • Trie的优势和适用场景
2.. 实现Trie
  • 实现Trie的业务无逻辑如下:
    1. import java.util.TreeMap;
    2.  
    3. public class Trie {
    4.  
    5. private class Node {
    6.  
    7. public boolean isWord;
    8. public TreeMap<Character, Node> next;
    9.  
    10. // 构造函数
    11. public Node(boolean isWord) {
    12. this.isWord = isWord;
    13. next = new TreeMap<>();
    14. }
    15.  
    16. // 无参数构造函数
    17. public Node() {
    18. this(false);
    19. }
    20. }
    21.  
    22. private Node root;
    23. private int size;
    24.  
    25. // 构造函数
    26. public Trie() {
    27. root = new Node();
    28. size = 0;
    29. }
    30.  
    31. // 实现getSize方法,获得Trie中存储的单词数量
    32. public int getSize() {
    33. return size;
    34. }
    35.  
    36. // 实现add方法,向Trie中添加新的单词word
    37. public void add(String word) {
    38.  
    39. Node cur = root;
    40. for (int i = 0; i < word.length(); i++) {
    41. char c = word.charAt(i);
    42. if (cur.next.get(c) == null) {
    43. cur.next.put(c, new Node());
    44. }
    45. cur = cur.next.get(c);
    46. }
    47. if (!cur.isWord) {
    48. cur.isWord = true;
    49. size++;
    50. }
    51. }
    52.  
    53. // 实现contains方法,查询Trie中是否包含单词word
    54. public boolean contains(String word) {
    55.  
    56. Node cur = root;
    57. for (int i = 0; i < word.length(); i++) {
    58. char c = word.charAt(i);
    59. if (cur.next.get(c) == null) {
    60. return false;
    61. }
    62. cur = cur.next.get(c);
    63. }
    64. return cur.isWord; // 好聪明
    65. }
    66.  
    67. // 实现isPrefix方法,查询Trie中时候保存了以prefix为前缀的单词
    68. public boolean isPrefix(String prefix) {
    69.  
    70. Node cur = root;
    71. for (int i = 0; i < prefix.length(); i++) {
    72. char c = prefix.charAt(i);
    73. if (cur.next.get(c) == null) {
    74. return false;
    75. }
    76. cur = cur.next.get(c);
    77. }
    78. return true;
    79. }
    80. }

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

  • 实现的业务逻辑如下:
    1. import java.util.TreeMap;
    2.  
    3. class WordDictionary {
    4.  
    5. private class Node {
    6.  
    7. public boolean isWord;
    8. public TreeMap<Character, Node> next;
    9.  
    10. public Node(boolean isWord) {
    11. this.isWord = isWord;
    12. next = new TreeMap<>();
    13. }
    14.  
    15. public Node() {
    16. this(false);
    17. }
    18.  
    19. }
    20.  
    21. /**
    22. * Initialize your data structure here.
    23. */
    24. private Node root;
    25.  
    26. public WordDictionary() {
    27. root = new Node();
    28. }
    29.  
    30. /**
    31. * Adds a word into the data structure.
    32. */
    33. public void addWord(String word) {
    34. Node cur = root;
    35. for (int i = 0; i < word.length(); i++) {
    36. char c = word.charAt(i);
    37. if (cur.next.get(c) == null) {
    38. cur.next.put(c, new Node());
    39. }
    40. cur = cur.next.get(c);
    41. }
    42. cur.isWord = true;
    43. }
    44.  
    45. /**
    46. * Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
    47. */
    48. public boolean search(String word) {
    49. return match(root, word, 0);
    50. }
    51.  
    52. private boolean match(Node node, String word, int index) {
    53. if (index == word.length()) {
    54. return node.isWord;
    55. }
    56.  
    57. char c = word.charAt(index);
    58. if (c != '.') {
    59. if (node.next.get(c) == null) {
    60. return false;
    61. }
    62. return match(node.next.get(c), word, index + 1);
    63. } else {
    64. for (char nextChar : node.next.keySet()) {
    65. if (match(node.next.get(nextChar), word, index + 1)) {
    66. return true;
    67. }
    68. }
    69. return false;
    70. }
    71. }
    72. }

第三十篇 玩转数据结构——字典树(Trie)的更多相关文章

  1. 第三十二篇 玩转数据结构——AVL树(AVL Tree)

          1.. 平衡二叉树 平衡二叉树要求,对于任意一个节点,左子树和右子树的高度差不能超过1. 平衡二叉树的高度和节点数量之间的关系也是O(logn) 为二叉树标注节点高度并计算平衡因子 AVL ...

  2. 第三十三篇 玩转数据结构——红黑树(Read Black Tree)

    1.. 图解2-3树维持绝对平衡的原理: 2.. 红黑树与2-3树是等价的 3.. 红黑树的特点 简要概括如下: 所有节点非黑即红:根节点为黑:NULL节点为黑:红节点孩子为黑:黑平衡 4.. 实现红 ...

  3. 第三十一篇 玩转数据结构——并查集(Union Find)

    1.. 并查集的应用场景 查看"网络"中节点的连接状态,这里的网络是广义上的网络 数学中的集合类的实现   2.. 并查集所支持的操作 对于一组数据,并查集主要支持两种操作:合并两 ...

  4. 第二十九篇 玩转数据结构——线段树(Segment Tree)

          1.. 线段树引入 线段树也称为区间树 为什么要使用线段树:对于某些问题,我们只关心区间(线段) 经典的线段树问题:区间染色,有一面长度为n的墙,每次选择一段墙进行染色(染色允许覆盖),问 ...

  5. Java数据结构——字典树TRIE

    又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种. 典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计. 它的优点是:利用字符串的公共 ...

  6. 模板 - 字符串/数据结构 - 字典树/Trie

    使用静态数组的nxt指针的设计,大概比使用map作为nxt指针的设计要快1倍,但空间花费大概也大1倍.在数据量小的情况下,时间和空间效率都不及map<vector,int>.map< ...

  7. [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序

    一. 题目 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 274040   Accepted: 48891 ...

  8. Delphi 泛型(三十篇)

    Delphi 泛型(三十篇)http://www.cnblogs.com/jxgxy/category/216671.html

  9. 字典树(Trie)详解

    详解字典树(Trie) 本篇随笔简单讲解一下信息学奥林匹克竞赛中的较为常用的数据结构--字典树.字典树也叫Trie树.前缀树.顾名思义,它是一种针对字符串进行维护的数据结构.并且,它的用途超级广泛.建 ...

随机推荐

  1. python--终端工具之subprocess

    一. subprocess.getstatusoutput import subprocess cmd = 'ifconfig' def cmds(cmd,print_msg=True): statu ...

  2. C# compare different Encoding pattern between UTF8 and UTF32 based on Md5

    using System; using System.Text; using System.IO; using System.Security.Cryptography; static void Ma ...

  3. hextorgb

    function hexToRgb(hex) { // By Tim Down - http://stackoverflow.com/a/5624139/3493650 // Expand short ...

  4. beego控制器介绍

    控制器介绍 提示:在 v1.6 中,此文档所涉及的 API 有重大变更,this.ServeJson() 更改为 this.ServeJSON(),this.TplNames 更改为 this.Tpl ...

  5. window服务session隔离

    在window服务中抓取窗体是做不到的,因为window系统的session隔离机制:如果想要调用外部程序,可以通过 创建代理进程 进行操作(通过非托管代码CreateProcessAsUser函数进 ...

  6. C语言 小技巧函数方法总结

    1.使用^(异或) 不引入第三变量交换两个变量的值. /* 交换 int a 和 int b 的值*/ #include <stdio.h> int main(int argc, char ...

  7. 我的第一个原生Web Components——滑块(SingleSlider)

    写着写着,就会跑偏,没错又走上了一个岔道……就是不知道这条岔道以后会不会越来越宽,有的说他是未来,有的说…… 这里不知道,也不做什么评断.减少一些重复性的工作,提高开发效率这是最根本的.说白了就是偷懒 ...

  8. 查看Spark与Hadoop等其他组件的兼容版本

    安装与Spark相关的其他组件的时候,例如JDK,Hadoop,Yarn,Hive,Kafka等,要考虑到这些组件和Spark的版本兼容关系.这个对应关系可以在Spark源代码的pom.xml文件中查 ...

  9. Windows7下Docker的安装

    转自  https://blog.csdn.net/xiangxiezhuren/article/details/79698913 无法打开图3,打开属性.给其添加git路径 无法使用图2下载   h ...

  10. linux命令解压压缩rar文件的详细步骤

    参考文件:https://www.cnblogs.com/qinglin/p/9007939.html