前缀树结构

针对26个字母的字典树

  1. /**
  2. * Description 针对26个字母的字典树
  3. *
  4. * @ClassName dataStructure.others.TrieTree
  5. * @Version 1.0
  6. * @Date: 2022/2/18 16:07
  7. * @Author xuyi
  8. */
  9. public class TrieTree {
  10. public class Node1{
  11. public int pass;
  12. public int end;
  13. public Node1[] next;
  14. public Node1() {
  15. pass = 0;
  16. end = 0;
  17. next = new Node1[26];
  18. }
  19. }
  20. public class Trie1{
  21. public Node1 root;
  22. public Trie1() {
  23. root = new Node1();
  24. }
  25. public void insert(String word){
  26. if(word == null||word.equals("")){
  27. return;
  28. }
  29. Node1 node = root;
  30. node.pass++;
  31. char[] chars = word.toCharArray();
  32. int idx;
  33. for(int i=0;i<chars.length;i++){
  34. idx=chars[i]-'a';
  35. if(node.next[idx]==null){
  36. node.next[idx] = new Node1();
  37. }
  38. node = node.next[idx];
  39. node.pass++;
  40. }
  41. node.end++;
  42. }
  43. public void delete(String word){
  44. if(search(word)==0){
  45. return;
  46. }
  47. Node1 node = root;
  48. node.pass--;
  49. char[] chars = word.toCharArray();
  50. int idx;
  51. for(int i=0;i<chars.length;i++){
  52. idx = chars[i]-'a';
  53. if(--node.next[idx].pass==0){
  54. node.next[idx]=null;
  55. return;
  56. }
  57. node = node.next[idx];
  58. }
  59. node.end--;
  60. }
  61. /**
  62. * 查找指定字符串在前缀树中出现的次数
  63. */
  64. public int search(String word){
  65. if(word==null||word.equals("")){
  66. return 0;
  67. }
  68. Node1 node = root;
  69. char[] chars = word.toCharArray();
  70. int idx;
  71. for(int i=0;i<chars.length;i++){
  72. idx = chars[i]-'a';
  73. if(node.next[idx]==null){
  74. return 0;
  75. }
  76. node = node.next[idx];
  77. }
  78. return node.end;
  79. }
  80. // 所有加入的字符串中,有几个是以pre这个字符串作为前缀的
  81. public int prefixNumber(String pre) {
  82. if (pre == null) {
  83. return 0;
  84. }
  85. char[] chs = pre.toCharArray();
  86. Node1 node = root;
  87. int index = 0;
  88. for (int i = 0; i < chs.length; i++) {
  89. index = chs[i] - 'a';
  90. // 走不到最后,就没有
  91. if (node.next[index] == null) {
  92. return 0;
  93. }
  94. node = node.next[index];
  95. }
  96. // 顺利走到最后,返回的pass就是有多少个字符串以当前pre为前缀的
  97. return node.pass;
  98. }
  99. }
  100. }

针对各种字符串的字典树

  1. /**
  2. * Description 针对各种字符串的字典树,路径不仅仅是a~z对应的26个,用HashMap<Integer, Node2>表示ascii码值对应的node。
  3. *
  4. * @ClassName dataStructure.others.TrieTree2
  5. * @Version 1.0
  6. * @Date: 2022/2/18 16:53
  7. * @Author xuyi
  8. */
  9. public class TrieTree2 {
  10. public class Node2 {
  11. public int pass;
  12. public int end;
  13. public HashMap<Integer, Node2> next;
  14. public Node2() {
  15. pass = 0;
  16. end = 0;
  17. next = new HashMap<>();
  18. }
  19. }
  20. public class Trie2 {
  21. public Node2 root;
  22. public Trie2() {
  23. root = new Node2();
  24. }
  25. public void insert(String word) {
  26. if (word == null || word.equals("")) {
  27. return;
  28. }
  29. Node2 node = root;
  30. node.pass++;
  31. char[] chars = word.toCharArray();
  32. int idx;
  33. for (int i = 0; i < chars.length; i++) {
  34. idx = chars[i];
  35. if (!node.next.containsKey(idx)) {
  36. node.next.put(idx, new Node2());
  37. }
  38. node = node.next.get(idx);
  39. node.pass++;
  40. }
  41. node.end++;
  42. }
  43. public void delete(String word) {
  44. if (search(word) == 0) {
  45. return;
  46. }
  47. Node2 node = root;
  48. node.pass--;
  49. char[] chars = word.toCharArray();
  50. int idx;
  51. for (int i = 0; i < chars.length; i++) {
  52. idx = chars[i];
  53. if (--node.next.get(idx).pass == 0) {
  54. node.next.remove(idx);
  55. return;
  56. }
  57. node = node.next.get(idx);
  58. }
  59. node.end--;
  60. }
  61. /**
  62. * 查找指定字符串在前缀树中出现的次数
  63. */
  64. public int search(String word) {
  65. if (word == null || word.equals("")) {
  66. return 0;
  67. }
  68. Node2 node = root;
  69. char[] chars = word.toCharArray();
  70. int idx;
  71. for (int i = 0; i < chars.length; i++) {
  72. idx = chars[i];
  73. if (!node.next.containsKey(idx)) {
  74. return 0;
  75. }
  76. node = node.next.get(idx);
  77. }
  78. return node.end;
  79. }
  80. }
  81. }