第三十篇 玩转数据结构——字典树(Trie)
- Trie的形象化描述如下图:
- 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)的更多相关文章
- 第三十二篇 玩转数据结构——AVL树(AVL Tree)
1.. 平衡二叉树 平衡二叉树要求,对于任意一个节点,左子树和右子树的高度差不能超过1. 平衡二叉树的高度和节点数量之间的关系也是O(logn) 为二叉树标注节点高度并计算平衡因子 AVL ...
- 第三十三篇 玩转数据结构——红黑树(Read Black Tree)
1.. 图解2-3树维持绝对平衡的原理: 2.. 红黑树与2-3树是等价的 3.. 红黑树的特点 简要概括如下: 所有节点非黑即红:根节点为黑:NULL节点为黑:红节点孩子为黑:黑平衡 4.. 实现红 ...
- 第三十一篇 玩转数据结构——并查集(Union Find)
1.. 并查集的应用场景 查看"网络"中节点的连接状态,这里的网络是广义上的网络 数学中的集合类的实现 2.. 并查集所支持的操作 对于一组数据,并查集主要支持两种操作:合并两 ...
- 第二十九篇 玩转数据结构——线段树(Segment Tree)
1.. 线段树引入 线段树也称为区间树 为什么要使用线段树:对于某些问题,我们只关心区间(线段) 经典的线段树问题:区间染色,有一面长度为n的墙,每次选择一段墙进行染色(染色允许覆盖),问 ...
- Java数据结构——字典树TRIE
又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种. 典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计. 它的优点是:利用字符串的公共 ...
- 模板 - 字符串/数据结构 - 字典树/Trie
使用静态数组的nxt指针的设计,大概比使用map作为nxt指针的设计要快1倍,但空间花费大概也大1倍.在数据量小的情况下,时间和空间效率都不及map<vector,int>.map< ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- Delphi 泛型(三十篇)
Delphi 泛型(三十篇)http://www.cnblogs.com/jxgxy/category/216671.html
- 字典树(Trie)详解
详解字典树(Trie) 本篇随笔简单讲解一下信息学奥林匹克竞赛中的较为常用的数据结构--字典树.字典树也叫Trie树.前缀树.顾名思义,它是一种针对字符串进行维护的数据结构.并且,它的用途超级广泛.建 ...
随机推荐
- python--终端工具之subprocess
一. subprocess.getstatusoutput import subprocess cmd = 'ifconfig' def cmds(cmd,print_msg=True): statu ...
- 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 ...
- hextorgb
function hexToRgb(hex) { // By Tim Down - http://stackoverflow.com/a/5624139/3493650 // Expand short ...
- beego控制器介绍
控制器介绍 提示:在 v1.6 中,此文档所涉及的 API 有重大变更,this.ServeJson() 更改为 this.ServeJSON(),this.TplNames 更改为 this.Tpl ...
- window服务session隔离
在window服务中抓取窗体是做不到的,因为window系统的session隔离机制:如果想要调用外部程序,可以通过 创建代理进程 进行操作(通过非托管代码CreateProcessAsUser函数进 ...
- C语言 小技巧函数方法总结
1.使用^(异或) 不引入第三变量交换两个变量的值. /* 交换 int a 和 int b 的值*/ #include <stdio.h> int main(int argc, char ...
- 我的第一个原生Web Components——滑块(SingleSlider)
写着写着,就会跑偏,没错又走上了一个岔道……就是不知道这条岔道以后会不会越来越宽,有的说他是未来,有的说…… 这里不知道,也不做什么评断.减少一些重复性的工作,提高开发效率这是最根本的.说白了就是偷懒 ...
- 查看Spark与Hadoop等其他组件的兼容版本
安装与Spark相关的其他组件的时候,例如JDK,Hadoop,Yarn,Hive,Kafka等,要考虑到这些组件和Spark的版本兼容关系.这个对应关系可以在Spark源代码的pom.xml文件中查 ...
- Windows7下Docker的安装
转自 https://blog.csdn.net/xiangxiezhuren/article/details/79698913 无法打开图3,打开属性.给其添加git路径 无法使用图2下载 h ...
- linux命令解压压缩rar文件的详细步骤
参考文件:https://www.cnblogs.com/qinglin/p/9007939.html