자료구조 트라이란?
●자료구조 트라이란?

●트라이는 어떻게 구현할 수 있을까?
class TrieTest {
@Test
void trieTest() {
Trie trie = new Trie();
trie.insert("maeilmail");
assertThat(trie.has("ma")).isTrue();
assertThat(trie.has("maeil")).isTrue();
assertThat(trie.has("maeilmail")).isTrue();
assertThat(trie.has("mail")).isFalse();
}
class Trie {
private final Node root = new Node("");
public void insert(String str) {
Node current = root;
for (String ch : str.split("")) {
if (!current.children.containsKey(ch)) {
current.children.put(ch, new Node(current.value + ch));
}
current = current.children.get(ch);
}
}
public boolean has(String str) {
Node current = root;
for (String ch : str.split("")) {
if (!current.children.containsKey(ch)) {
return false;
}
current = current.children.get(ch);
}
return true;
}
}
class Node {
public String value;
public Map<String, Node> children;
public Node(String value) {
this.value = value;
this.children = new HashMap<>();
}
}
}