码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • java 实现字典树(Trie)


    TreeNode.java

    1. package Trie;
    2. public class TreeNode {
    3. public TreeNode[] slot = new TreeNode[26];
    4. public char c;
    5. public boolean isWord;
    6. public int prefix;
    7. public String word;
    8. public String explain;
    9. }

    Trie.java

    1. package Trie;
    2. import java.util.ArrayList;
    3. import java.util.Collections;
    4. import java.util.List;
    5. public class Trie {
    6. public static final TreeNode wordsTree = new TreeNode();
    7. public void insert(String words, String explain) {
    8. var root = wordsTree;
    9. var chars = words.toCharArray();
    10. for (var c : chars) {
    11. var idx = c - 'a';
    12. if (root.slot[idx] == null) {
    13. root.slot[idx] = new TreeNode();
    14. }
    15. root = root.slot[idx];
    16. root.c = c;
    17. root.prefix++;
    18. }
    19. root.explain = explain;
    20. root.isWord = true;
    21. }
    22. public List searchPrefix(String prefix){
    23. var root = wordsTree;
    24. var chars = prefix.toCharArray();
    25. var cache = new StringBuilder();
    26. for (var c : chars) {
    27. var idx = c - 'a';
    28. if (idx > root.slot.length || idx < 0 || root.slot[idx] == null) {
    29. return Collections.emptyList();
    30. }
    31. cache.append(c);
    32. root = root.slot[idx];
    33. }
    34. var list = new ArrayList();
    35. if (root.prefix != 0) {
    36. for (int i = 0; i < root.slot.length; i++) {
    37. if (root.slot[i] != null) {
    38. var c = (char) (i + 'a');
    39. collect(root.slot[i], String.valueOf(cache) + c, list, 15);
    40. if (list.size() >= 15) {
    41. return list;
    42. }
    43. }
    44. }
    45. }
    46. return list;
    47. }
    48. protected void collect(TreeNode treeNode, String pre, List queue, int resultLimit) {
    49. if (treeNode.isWord) {
    50. treeNode.word = pre;
    51. queue.add(treeNode.word + "->" + treeNode.explain);
    52. if (queue.size() >= resultLimit) {
    53. return;
    54. }
    55. }
    56. for (int i = 0; i < treeNode.slot.length; i++) {
    57. var c = (char) ('a' + i);
    58. if (treeNode.slot[i] != null) {
    59. collect(treeNode.slot[i], pre + c, queue, resultLimit);
    60. }
    61. }
    62. }
    63. }

    测试代码

    1. public static void main(String[] args) {
    2. var trie = new Trie();
    3. trie.insert("bat", "大厂");
    4. trie.insert("batch", "批量");
    5. trie.insert("bitch", "彪子");
    6. trie.insert("battle", "战斗");
    7. System.out.println(trie);
    8. List trieNodes = trie.searchPrefix("ba");
    9. System.out.println(trieNodes);
    10. }

  • 相关阅读:
    WPF 附加属性+控件模板,完成自定义控件。建议观看HandyControl源码
    vector的模拟实现
    rdkit Recap、BRICS将smiles切分为片段(fragment)
    Python爬虫基础(一):urllib库的使用详解
    网安入门18-XSS(靶场实战)
    极智AI | 算一算大模型显存占用
    java计算机毕业设计跨境电商网站源码+系统+数据库+lw文档+mybatis+运行部署
    python中使用pytest框架集成allure测试报告
    【0148】System V Semaphores(信号量)
    智能转码技巧大揭秘,轻松实现视频转码!
  • 原文地址:https://blog.csdn.net/fanghailiang2016/article/details/138104819
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号