java前缀树----HashMap实现(算法,数据结构,力扣208)
创始人
2025-05-30 02:26:31
0

实现前缀树

Trie(发音类似 “try”)或者说 前缀树
是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。

力扣208 https://leetcode-cn.com/problems/implement-trie-prefix-tree/
在这里插入图片描述

许多搜索引擎都是使用这种方式实现的

在这里插入图片描述

总的来说是,root节点不存值,其他每个节点中都存储单一字符。

这里我使用HashMap实现,因为这种实现方法添加节点更灵活。

class TrieNode {public Character vaCharacter;public Map children = new HashMap<>();private boolean isEnd;public TrieNode(char val) {vaCharacter = val;}public TrieNode() {}public boolean existStr(){return isEnd;}public void setTrue(){isEnd = true;}public TrieNode getChildStr(char val) {if(children.containsKey(val)) {return children.get(val);}return null;}};

参数解释:

1. children:所有子节点组成的val - val的对象的map,查找更方便
2. vaCharacter:该节点的值
3. isEnd:在改节点是否是字符串结尾(是否存在到目前为止前缀形成的字符串,方便区分前缀和字符串)

  1. 添加操作
 /** Inserts a word into the trie. */public void insert(String word) {TrieNode exangeNode = root;for(int i = 0; i < word.length(); i++){TrieNode temp = exangeNode.getChildStr(word.charAt(i));if(temp == null){exangeNode.children.put(word.charAt(i),new TrieNode());}else{}exangeNode = exangeNode.getChildStr(word.charAt(i));}exangeNode.setTrue();}
  1. 查串操作(查询字符串是否存在于前缀树)
public boolean search(String word) {TrieNode exangeNode = root;for(int i = 0; i < word.length(); i++){TrieNode temp = exangeNode.getChildStr(word.charAt(i));if(temp == null){return false;}else{exangeNode = exangeNode.getChildStr(word.charAt(i));}}return exangeNode.existStr();}
  1. 查前缀操作(查询前缀是否存在)
    public boolean startsWith(String prefix) {TrieNode exangeNode = root;for(int i = 0; i < prefix.length(); i++){TrieNode temp = exangeNode.getChildStr(prefix.charAt(i));if(temp == null){return false;}else{exangeNode = exangeNode.getChildStr(prefix.charAt(i));}}return true;}

整体实现代码:

class Trie {TrieNode root;/** Initialize your data structure here. */public Trie() {root = new TrieNode();}/** Inserts a word into the trie. */public void insert(String word) {TrieNode exangeNode = root;for(int i = 0; i < word.length(); i++){TrieNode temp = exangeNode.getChildStr(word.charAt(i));if(temp == null){exangeNode.children.put(word.charAt(i),new TrieNode());}else{}exangeNode = exangeNode.getChildStr(word.charAt(i));}exangeNode.setTrue();}/** Returns if the word is in the trie. */public boolean search(String word) {TrieNode exangeNode = root;for(int i = 0; i < word.length(); i++){TrieNode temp = exangeNode.getChildStr(word.charAt(i));if(temp == null){return false;}else{exangeNode = exangeNode.getChildStr(word.charAt(i));}}return exangeNode.existStr();}/** Returns if there is any word in the trie that starts with the given prefix. */public boolean startsWith(String prefix) {TrieNode exangeNode = root;for(int i = 0; i < prefix.length(); i++){TrieNode temp = exangeNode.getChildStr(prefix.charAt(i));if(temp == null){return false;}else{exangeNode = exangeNode.getChildStr(prefix.charAt(i));}}return true;}
}
class TrieNode {public Character vaCharacter;public Map children = new HashMap<>();private boolean isEnd;public TrieNode(char val) {vaCharacter = val;}public TrieNode() {}public boolean existStr(){return isEnd;}public void setTrue(){isEnd = true;}public TrieNode getChildStr(char val) {if(children.containsKey(val)) {return children.get(val);}return null;}};

相关内容

热门资讯

linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
C++ 机房预约系统(六):学... 8、 学生模块 8.1 学生子菜单、登录和注销 实现步骤: 在Student.cpp的...
JAVA多线程知识整理 Java多线程基础 线程的创建和启动 继承Thread类来创建并启动 自定义Thread类的子类&#...
【洛谷 P1090】[NOIP... [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G ...
国民技术LPUART介绍 低功耗通用异步接收器(LPUART) 简介 低功耗通用异步收发器...
城乡供水一体化平台-助力乡村振... 城乡供水一体化管理系统建设方案 城乡供水一体化管理系统是运用云计算、大数据等信息化手段࿰...
程序的循环结构和random库...   第三个参数就是步长     引入文件时记得指明字符格式,否则读入不了 ...
中国版ChatGPT在哪些方面... 目录 一、中国巨大的市场需求 二、中国企业加速创新 三、中国的人工智能发展 四、企业愿景的推进 五、...
报名开启 | 共赴一场 Flu... 2023 年 1 月 25 日,Flutter Forward 大会在肯尼亚首都内罗毕...
汇编00-MASM 和 Vis... Qt源码解析 索引 汇编逆向--- MASM 和 Visual Studio入门 前提知识ÿ...
【简陋Web应用3】实现人脸比... 文章目录🍉 前情提要🌷 效果演示🥝 实现过程1. u...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
windows安装JDK步骤 一、 下载JDK安装包 下载地址:https://www.oracle.com/jav...
分治法实现合并排序(归并排序)... 🎊【数据结构与算法】专题正在持续更新中,各种数据结构的创建原理与运用✨...
在linux上安装配置node... 目录前言1,关于nodejs2,配置环境变量3,总结 前言...
Linux学习之端口、网络协议... 端口:设备与外界通讯交流的出口 网络协议:   网络协议是指计算机通信网...
Linux内核进程管理并发同步... 并发同步并发 是指在某一时间段内能够处理多个任务的能力,而 并行 是指同一时间能够处理...
opencv学习-HOG LO... 目录1. HOG(Histogram of Oriented Gradients,方向梯度直方图)1...
EEG微状态的功能意义 导读大脑的瞬时全局功能状态反映在其电场结构上。聚类分析方法一致地提取了四种头表面脑电场结构ÿ...
【Unity 手写PBR】Bu... 写在前面 前期积累: GAMES101作业7提高-实现微表面模型你需要了解的知识 【技...