当前位置: 首页 > news >正文

数据结构与算法-Trie树添加与搜索

trie树的使用场景

我们若需要制作一个通讯录的软件,使用常规树结构查询的复杂度为O(logn),但trie树的复杂度确与数据多少无关,与单词长度有关,这就大大缩减的查询的时间复杂度。

trie树的基本实现

基础结构

package com.study.trieDemo;import java.util.TreeMap;/*** Created by Zsy on 2020/8/21.*/
public class Trie {private class Node {private boolean isWord;private TreeMap<Character, Node> next;public Node(boolean isWord) {this.isWord = isWord;next = new TreeMap<Character, Node>();}public Node() {this(false);}}private Node root;private int size;public int getSize() {return size;}}

添加

   /*** 向tire中添加一个单词* @param 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++;}}

搜索单词

  /*** 查找单词word是否在trie树中* @param word* @return*/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;}

leetcode中关于trie的题目

208. 实现 Trie (前缀树)

208. 实现 Trie (前缀树)

实现

package com.study.leetcode;import java.util.TreeMap;/*** Created by Zsy on 2020/8/21.*/
public class Trie_208 {private Node root;private class Node {private boolean isWord;private TreeMap<Character, Node> next;public Node(boolean isWord) {this.isWord = isWord;next = new TreeMap<Character, Node>();}public Node() {this(false);}}/*** Initialize your data structure here.*/public Trie_208() {root = new Node();}/*** Inserts a word into the trie.*/public void insert(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 trie.*/public boolean search(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;}/*** Returns if there is any word in the trie that starts with the given prefix.*/public boolean startsWith(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;}
}

211. 添加与搜索单词 - 数据结构设计

题目链接

211. 添加与搜索单词 - 数据结构设计

实现

package com.study.leetcode;import java.util.TreeMap;/*** Created by Zsy on 2020/8/21.*/
public class WordDictionary_211 {private Node root;private class Node {private boolean isWord;private TreeMap<Character, Node> next;public Node(boolean isWord) {this.isWord = isWord;next = new TreeMap<Character, Node>();}public Node() {this(false);}}/*** Initialize your data structure here.*/public WordDictionary_211() {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.*//*** 使用match进行递归查询* @param word* @return*/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;}}}

677. 键值映射

题目链接

677. 键值映射

题解

package com.study.leetcode;import java.util.TreeMap;/*** Created by Zsy on 2020/8/21.*/
public class MapSum_677 {private class Node {public int value;public TreeMap<Character, Node> next;public Node(int value) {this.value = value;next = new TreeMap<Character, Node>();}public Node() {this(0);}}private Node root;/*** Initialize your data structure here.*/public MapSum_677() {root = new Node();}/*** 非递归法插入节点,通过查询next中是否存在对应key的节点,进行相应插入操作* @param key* @param val*/public void insert(String key, int val) {Node cur = root;for (int i = 0; i < key.length(); i++) {char c = key.charAt(i);if (cur.next.get(c) == null)cur.next.put(c, new Node());cur = cur.next.get(c);}cur.value = val;}/*** 找到匹配节点的指针,将地址传给sum进行递归计算* @param prefix* @return*/public int sum(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 0;cur = cur.next.get(c);}return sum(cur);}private int sum(Node node) {//省略了if(node==null) return 0;int value = node.value;for (char c : node.next.keySet()) {value+=sum(node.next.get(c));}return value;}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • IDEA甚至前进后退跳转键
  • 【第十三章:Sentosa_DSML社区版-机器学习聚类】
  • 携手阿里云CEN:共创SD-WAN融合广域网
  • 吃透这本大语言模型入门指南,LLM就拿下了
  • python脚本编译为.so速度对比
  • 使用LangGPT提示词让大模型比较浮点数
  • 如何查看Android设备的dpi
  • Springboot+Shiro+Mybatis+mysql实现权限安全认证
  • Webpack:现代前端项目的强大打包工具
  • redis分布式锁(看门枸机制)
  • linux如何对c++进行内存分析
  • Davinci 大数据可视化分析
  • 数字电子技术-编码器
  • gevent + flask 接口会卡住
  • Python--数据格式转换
  • “大数据应用场景”之隔壁老王(连载四)
  • 「译」Node.js Streams 基础
  • angular组件开发
  • gitlab-ci配置详解(一)
  • Java方法详解
  • NSTimer学习笔记
  • OSS Web直传 (文件图片)
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • SpiderData 2019年2月23日 DApp数据排行榜
  • Spring Cloud Feign的两种使用姿势
  • swift基础之_对象 实例方法 对象方法。
  • Travix是如何部署应用程序到Kubernetes上的
  • Vim 折腾记
  • 包装类对象
  • 不发不行!Netty集成文字图片聊天室外加TCP/IP软硬件通信
  • 高度不固定时垂直居中
  • 关于List、List?、ListObject的区别
  • 基于游标的分页接口实现
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 类orAPI - 收藏集 - 掘金
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 如何选择开源的机器学习框架?
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 入口文件开始,分析Vue源码实现
  • 实现菜单下拉伸展折叠效果demo
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 小程序 setData 学问多
  • 用jQuery怎么做到前后端分离
  • ​第20课 在Android Native开发中加入新的C++类
  • # Java NIO(一)FileChannel
  • $jQuery 重写Alert样式方法
  • (9)目标检测_SSD的原理
  • (Matlab)使用竞争神经网络实现数据聚类
  • (二开)Flink 修改源码拓展 SQL 语法
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (算法)Travel Information Center
  • (转)重识new
  • (总结)Linux下的暴力密码在线破解工具Hydra详解
  • .NET 8 跨平台高性能边缘采集网关