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;
    }
    }
    }

第三十篇 玩转数据结构——字典树(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. dubbox的小案例

    什么是Dubbox: Dubbo是一个被国内很多互联网公司广泛使用的开源分布式服务框架,即使从国际视野来看应该也是一个非常全面的SOA基础框架.作为一个重要的技术研究课题,在当当网根据自身的需求,为D ...

  2. 设置datagridview隔行变色

    /// <summary> /// 设置datagridview隔行变色 /// </summary> /// <param name="e"> ...

  3. JS:JS判断提交表单不能为空等验证

    这段代码在<form>中有οnsubmit="return on_submit()",如果 onsubmit ()返回 fasle,表单的元素就不会提交,即action ...

  4. Cows Of The Round Table【DFS】

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAz0AAAKiCAIAAABzTSUAAAAgAElEQVR4Aey9C5RnWVXff4GBUR6j8u ...

  5. Android_下方弹出菜单的实现

    这一功能要用到动画相关知识 实现点击按钮弹出下方输入框,这里点击可弹出一个输入界面,其中包括一个小型计算器. 点击date可弹出datedialog设置date. 1.编写弹出框的布局文件 <? ...

  6. python的优先级

    在编写程序时,我遇到麻烦!怎么找都找不到bug 最终我发现了是我搞错了运算符优先级 位运算要在加减后面(这可真奇怪) eg 10-10^11=11!!! 还是多加括号的好

  7. [CF1303G] Sum of Prefix Sums - 点分治,李超线段树

    给定一棵 \(n\) 个点的带点权的树,求树上的路径 \(x_1,...,x_k\) ,最大化 \(\sum_{i=1}^k ia_{x_i}\) Solution 树上路径问题可用点分治. 考虑如何 ...

  8. PL/SQL快键键——自动替换(输入sf直接跳出来select * from)

    PL/SQL Developer使用技巧.快捷键 1.类SQL PLUS窗口:File->New->Command Window,这个类似于oracle的客户端工具sql plus,但比它 ...

  9. linux中Jenkins启动/重启/停止命令

    简要记录一下Linux 中Jenkins启动/重启/停止命令 启动service jenkins start1重启service jenkins restart1停止service jenkins s ...

  10. Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1 SpringBoot发送邮件

    解决方案 换端口 QQ邮箱可以把端口换成587 设置属性 spring.mail.properties.mail.smtp.ssl.enable=true 原因 465端口是为SMTPS(SMTP-o ...