Hyemi Lee

Hyemi Lee

주니어 개발자의 삽질과 기록

Data Structure, Trie & JAVA구현

TrieNode.java

package org.dataStructure;
import java.util.HashMap;
import java.util.Map;
// 자식노드맵 + 현재 노드가 마지막 글자인지 여부 (한 단어가 완성되는 시점임을 알 수 있다)
public class TrieNode {
	// 자식 노드 맵
	private Map<Character, TrieNode> childNodes = new HashMap<>();
	// 마지막 글자인지 여부
	private boolean isLastChar;
	Map<Character, TrieNode> getChildNodes(){
		return this.childNodes;
	}
	boolean isLastChar() {
		return this.isLastChar;
	}
	void setIsLastChar(boolean isLastChar) {
		this.isLastChar = isLastChar;
	}
}

Trie.java

package org.dataStructure;
// Trie는 기본적으로 빈 문자열을 가지는 루트노드만 가지고 있다.
// insert메서드를 통해 단어를 입력하면서 그에 맞게 자식 노드가 생성된다.
public class Trie {
	// 루트 노드
	private TrieNode rootNode;
	Trie() {
		rootNode = new TrieNode();
	}
	// 자식 노드 추가
	void insert(String word) {
		TrieNode thisNode = this.rootNode;
		for (int i = 0; i < word.length(); ++i) {
			thisNode = thisNode.getChildNodes().computeIfAbsent(word.charAt(i), c -> new TrieNode());
		}
		// 계속해서 thisNode를 갱신하기 때문에 마지막 글자가 되므로 true.
		thisNode.setIsLastChar(true);
	}
	// 특정 단어가 Trie에 존재하는지 확인
	// 1. 루트노드 부터 순서대로 알파벡이 일치하는 자식노드들이 존재 할것
	// 2. 해당 단어의 마지막 글자에 해당하는 노드의 isLastChar == true 일것
	boolean contains(String word) {
		TrieNode thisNode = this.rootNode;
		for (int i = 0; i < word.length(); ++i) {
			char character = word.charAt(i);
			TrieNode node = thisNode.getChildNodes().get(character);
			if (node == null)
				return false;
			thisNode = node;
		}
		return thisNode.isLastChar();
	}
	// 삭제
	// 탐색 : 부모노드 -> 자식노드
	// 삭제 : 자식노드 -> 부모노드
	// 1. 자식 노드를 가지고 있지 않아야 합니다.
	// 삭제시, 자식노드까지 지우면 안된다.
	// 2. 삭제 시작 첫 노드는 isLastChar == true여야한다.
	// false이면 존재하지 않는 단어이기 때문에 삭제 불가능.
	// 3. 삭제 진행 중에는 isLastChar == false 여야한다.
	// true이면 다른 단어가 존재하기 때문에 삭제하면 안된다.
	void delete(String word) {
		delete(this.rootNode, word, 0); // 최초로 delete 시작.
	}
	private void delete(TrieNode thisNode, String word, int index) {
		char character = word.charAt(index);
		// 단어가 아예존재 하지 않는경우 , ex) ABC , DEF
		if (!thisNode.getChildNodes().containsKey(character))
			throw new Error("There is no [" + word + "] in this Trie.");
		// 글자가 존재하는 trieNode를 가져온다
		TrieNode childNode = thisNode.getChildNodes().get(character);
		index++;
		// 탐색완료
		if (index == word.length()) {
			// 삭제 조건 2번 항목
			if (!childNode.isLastChar())
				throw new Error("There is no [" + word + "] in this Trie.");
			childNode.setIsLastChar(false);
			// 삭제 조건 1번 항목
			if (childNode.getChildNodes().isEmpty())
				thisNode.getChildNodes().remove(childNode);
		} else {
			// 콜백함수
			delete(childNode, word, index);
			// 콜백함수로 인해서 단어 탐색이 완료된 후에 이부분이 불린다.
			// 삭제 조건 1, 3번 항목
			// 자식노드가 없고, 현재 노드로 끝나는 단어가 없는 경우 이 노드 삭제
			if (!childNode.isLastChar() && childNode.getChildNodes().isEmpty())
				thisNode.getChildNodes().remove(character);
		}
	}
	boolean isRootEmpty() {
		return this.rootNode.getChildNodes().isEmpty();
	}
}

TrieTest.java

package org.dataStructure;
public class TrieTest {
	public static void main(String[] args) {
		Trie trie = new Trie();
		trie.insert("DEV");
		trie.insert("DEAR");
		trie.insert("DESERVE");
		trie.insert("DRIVE");
		System.out.println(trie.contains("DEAR"));
		System.out.println(trie.contains("DESERT"));
		//trie.delete("DESERT");
		//trie.delete("DE");
		trie.delete("DEAR");
		System.out.println(trie.contains("DEAR"));
	}
}

참고

Share on

Twitter Facebook LinkedIn

You may also enjoy

Redis Stream

2021年04月28日

Stream Stream은 로그 데이터를 처리하게위해 5.0에서 새로 도입된 데이터 타입입니다. 대량의 데이터가 연속적으로 발생할때 처리하기 위해 등장했습니다. 기존 데이터를 수정하지 않고 오직 추가로 발생합니다. 이런 종류의 데이터를 stream or log데이터...

Study, Object, chapter2&3 presentation

2021年04月20日

chapter03. 역할, 책임, 협력 객체지향 설계란, 올바른 객체에게 올바른 책임을 할당하면서 낮은 결합도와 높은 응집도를 가진 구조를 창조하는 활동이다.

Spring, chatting 프로그램 만들기, Reactive란?

2020年06月16日

Reactive 막힘없이 흘러다니는 data(event)를 통해 사용자에게 자연스러운 응답을 주고 규모 탄력적으로 리소스를 사용하며 실패에 있어서 유연하게 대처한다 모든 지점에서 블럭 되지 않게 하자 oop와 같은 패러다임 모든 것을 비동기적인 data의 strea...