Oh My Algorithm
Algorithm Guidecomplexity: O(m) (문자열 길이)

트라이 (Trie)

문자열을 글자 단위로 가지에 저장하는 접두사 트리입니다. 공통 접두사를 공유해 자동완성·사전·접두사 검색을 문자열 길이 O(m)에 처리합니다.

01 Explore How It Works

Interactive Step-by-Step
Trie · 접두사 트리

트라이 시작. 문자열을 글자 단위로 가지에 저장하고, 공통 접두사는 같은 길을 함께 씁니다.

Logic Node1 / 6

02 Understand It Simply

For Everyone
🔑Analogy

사전의 가지치기 색인 — 같은 첫 글자로 시작하는 단어들은 한 길을 공유합니다.

💡In Plain Words

문자열을 글자 단위로 가지에 저장해 공통 접두사를 함께 쓰는 트리예요.

단어 길이 O(m)에 찾고, 특정 접두사로 시작하는 단어들을 빠르게 모읍니다.

📍Where It's Used
  • 검색어 자동완성
  • 사전·맞춤법 검사
  • IP 라우팅

03 Python Implementation

A clean, readable reference implementation of the core logic of 트라이 (Trie).

core_implementation.py
class TrieNode:
    def __init__(self):
        self.children = {}
        self.is_end = False

class Trie:
    def __init__(self):
        self.root = TrieNode()

    def insert(self, word):
        node = self.root
        for ch in word:
            node = node.children.setdefault(ch, TrieNode())
        node.is_end = True

    def search(self, word):
        node = self.root
        for ch in word:
            if ch not in node.children:
                return False
            node = node.children[ch]
        return node.is_end

04 Frequently Asked Questions

FAQ
What is 트라이 (Trie)?+

문자열을 글자 단위로 가지에 저장하는 접두사 트리입니다. 공통 접두사를 공유해 자동완성·사전·접두사 검색을 문자열 길이 O(m)에 처리합니다.

What is the time complexity of 트라이 (Trie)?+

The time complexity of 트라이 (Trie) is O(m) (문자열 길이). Follow the step-by-step visualization to see exactly why.

Where is 트라이 (Trie) used?+

검색어 자동완성, 사전·맞춤법 검사, IP 라우팅.

What's a simple analogy for 트라이 (Trie)?+

사전의 가지치기 색인 — 같은 첫 글자로 시작하는 단어들은 한 길을 공유합니다.

Guide Progress0%