Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Trie를 리팩토링한다. #108

Merged
merged 3 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions src/main/java/com/seong/shoutlink/domain/common/Trie.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,35 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.Getter;

@Getter
public class Trie {

static class Node {

private final char c;
private final Map<Character, Node> children = new ConcurrentHashMap<>();
private boolean isWord;

public Node(char c) {
this.c = c;
}
private final AtomicBoolean isWord = new AtomicBoolean(false);

public void settingWord() {
isWord = true;
isWord.compareAndSet(false, true);
}

public boolean hasChildren(char c) {
return children.containsKey(c);
}

public Node nextNode(char c) {
Node nextNode = children.putIfAbsent(c, new Node(c));
return Optional.ofNullable(nextNode)
.orElseGet(() -> children.get(c));
return children.computeIfAbsent(c, key -> new Node());
}

public void addSuggestions(String word, List<String> suggestions, int count) {
if(isWord) {
if (isWord.get()) {
suggestions.add(word);
}
if(suggestions.size() >= count) {
if (suggestions.size() >= count) {
return;
}
children.forEach((character, childNode) -> {
Expand All @@ -53,7 +46,7 @@ public void addSuggestions(String word, List<String> suggestions, int count) {
private static final int MAX_PREFIX_LENGTH = 30;
public static final int ZERO = 0;

private final Node root = new Node(' ');
private final Node root = new Node();

public void insert(String word) {
if (word.length() > MAX_WORD_LENGTH) {
Expand Down
19 changes: 14 additions & 5 deletions src/test/java/com/seong/shoutlink/domain/common/TrieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@

class TrieTest {

private final String[] words = {
"apple", "ant", "arm", "anchor", "angle",
"alphabet", "album", "alien", "apron", "atom",
"axe", "art", "architect", "area", "arena",
"altitude", "almond", "aroma", "arrow", "ash",
"auction", "audio", "attic", "author", "award",
"charge", "crown", "cross", "cup", "cap",
"crop", "card", "chip", "case", "camp",
"club", "court", "crash", "class", "corner"
};

@Nested
@DisplayName("insert 호출 시")
class InsertTest {
Expand Down Expand Up @@ -58,8 +69,8 @@ void insertableWordMaxLength_35(int wordLength, int resultSize) {
void concurrentInsert() throws InterruptedException {
//given
Trie trie = new Trie();
String prefix = "asdf";
int threadPoolSize = 26;
String prefix = "";
int threadPoolSize = words.length;
ExecutorService service = Executors.newFixedThreadPool(threadPoolSize);
CountDownLatch latch = new CountDownLatch(threadPoolSize);

Expand All @@ -68,9 +79,7 @@ void concurrentInsert() throws InterruptedException {
int finalI = i;
service.submit(() -> {
try {
StringBuilder sb = new StringBuilder(prefix);
sb.append((char) ('a' + finalI));
trie.insert(sb.toString());
trie.insert(words[finalI]);
} finally {
latch.countDown();
}
Expand Down
Loading