-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.java
More file actions
76 lines (68 loc) · 1.61 KB
/
Copy pathTrie.java
File metadata and controls
76 lines (68 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* File Name:Trie is created on 2020/9/21 10:57 下午 by lite
*
* Copyright (c) 2020, xiaoyujiaoyu technology All Rights Reserved.
*
*/
/**
* @author lite
* @Description: 下一节点的next数组 + 表示最后一级的isEnd标志
* @date: 2020/9/21 10:57 下午
* @since JDK 1.8
*/
public class Trie {
private final Trie[] next;
private boolean isEnd;
public Trie() {
isEnd = false;
next = new Trie[26];
}
public void insert(String word) {
if (null == word || 0 == word.length()) {
return;
}
Trie curr = this;
char[] chars = word.toCharArray();
for (char c : chars) {
int n = c - 'a';
if (null == curr.next[n]) {
curr.next[n] = new Trie();
}
curr = curr.next[n];
}
curr.isEnd = true;
}
/**
* word 是否在trie树内
*
* @param word
*
* @return
*/
public boolean search(String word) {
Trie node = searchPrefix(word);
return null != node && node.isEnd;
}
/**
* trie树里是否有prefix开头的任意词
*
* @param prefix
*
* @return
*/
public boolean startWith(String prefix) {
Trie node = searchPrefix(prefix);
return null != node;
}
public Trie searchPrefix(String word) {
Trie node = this;
char[] chars = word.toCharArray();
for (char c : chars) {
node = node.next[c - 'a'];
if (null == node) {
return null;
}
}
return node;
}
}