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

news/2024/9/24 2:53:32/

trie树的使用场景

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

trie树的基本实现

基础结构

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

添加

java">   /*** 向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++;}}

搜索单词

java">  /*** 查找单词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;}

leetcodetrie_91">leetcode中关于trie的题目

208. 实现 Trie (前缀树)

208. 实现 Trie (前缀树)

实现

java">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. 添加与搜索单词 - 数据结构设计

实现

java">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. 键值映射

题解

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

http://www.ppmy.cn/news/1529613.html

相关文章

某恩加密数据爬虫逆向分析

目标网站 aHR0cHM6Ly93d3cuZW5kYXRhLmNvbS5jbi9pbmRleC5odG1s 一、抓包分析 响应数据加密 二、逆向分析 下断点&#xff0c;刷新页面 一直往下跟栈&#xff0c;发现是在这进行的加密 内部实现逻辑 本地数据获取 本文章仅提供技术分享交流学习&#xff0c;不可对目标服务器造…

【洛谷】AT_abc371_e [ABC371E] I Hate Sigma Problems 的题解

【洛谷】AT_abc371_e [ABC371E] I Hate Sigma Problems 的题解 洛谷传送门 AT传送门 题解 I Hate Sigma Problems!!! 意思很简单就是求序列中每一个子区间内含有不同数字的个数之和。 暴力的话时间复杂度是 O ( n 2 ) O(n ^ 2) O(n2)&#xff0c;是肯定不行的&#xff0…

DSP学习00-F28379D学习准备(了解一个工程的构成)

叠甲 我也算初学F28379D&#xff0c;不对之处请大家斧正。不同型号的DSP在外设配置的函数上有一些区别&#xff0c;但是掌握一种对其他型号的来说则难度不大。对于我们而言学习DSP最终还是要用于算法验证&#xff0c;而DSP资源的最大化利用、代码效率提升等则是后话。 软件准…

macOS使用brew安装并配置python环境

1.确认已安装brew环境,如没有安装,参考: macOS系统Homebrew工具安装及使用-CSDN博客 2.安装python python安装成功 3.添加pip路径到/etc/paths 4.查看python与pip默认安装版本

Django 创建好的模块怎么在后台显示

1、配置模型及其需要显示的数据 刚才创建好的tests的增删改查&#xff0c;在后台是不显示的&#xff0c;所以需要进行配置,在刚才创建好的模块里找到admin.py文件&#xff0c;在里面进行如下配置 from django.contrib import adminfrom . import models from .models import …

新能源汽车数据大全(产销数据\充电桩\专利等)

新能源汽车数据大全&#xff08;产销数据\充电桩\专利等&#xff09; 来源&#xff1a;全国各省市统计年鉴、统计公报、国家能源署、中国汽车行业协会&#xff0c;各类汽车统计年鉴、中国电动汽车充电基础设施促进联盟等 1、汽车分品牌产销(95家车企&#xff0c;768个车型&am…

Spring Boot-依赖冲突问题

Spring Boot 依赖冲突问题及其解决方案 1. 引言 在Spring Boot项目中&#xff0c;依赖管理是一个至关重要的环节。Spring Boot通过自动配置和强大的依赖管理简化了应用开发&#xff0c;但随着项目规模扩大和依赖数量的增加&#xff0c;依赖冲突问题常常会浮现。依赖冲突不仅会…

Qwen2.5 本地部署的实战教程

大家好,我是herosunly。985院校硕士毕业,现担任算法研究员一职,热衷于大模型算法的研究与应用。曾担任百度千帆大模型比赛、BPAA算法大赛评委,编写微软OpenAI考试认证指导手册。曾获得阿里云天池比赛第一名,CCF比赛第二名,科大讯飞比赛第三名。授权多项发明专利。对机器学…