Skip to content

Commit e8a5387

Browse files
committed
Add study notes for Trie implementation
1 parent 585e7fd commit e8a5387

File tree

1 file changed

+66
-4
lines changed

1 file changed

+66
-4
lines changed

β€Žimplement-trie-prefix-tree/KwonNayeon.pyβ€Ž

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,85 @@
55
3. At most 3 * 10^4 calls in total will be made to insert, search, and startsWith.
66
77
Time Complexity:
8-
-
8+
- insert: O(m), m은 μž…λ ₯ λ‹¨μ–΄μ˜ 길이
9+
- search: O(m), m은 검색 λ‹¨μ–΄μ˜ 길이
10+
- startsWith: O(m), m은 μ ‘λ‘μ‚¬μ˜ 길이
911
1012
Space Complexity:
11-
-
13+
- O(N*M), N은 μ €μž₯된 λ‹¨μ–΄μ˜ 개수, M은 평균 단어 길이
1214
1315
μ΄ν•΄ν•œ λ‚΄μš©:
1416
1. Prefix(접두사)의 의미
15-
- 문법적 접두사(un-, pre- λ“±)κ°€ μ•„λ‹˜, λ‹¨μˆœνžˆ λ‹¨μ–΄μ˜ μ•žλΆ€λΆ„μ— μžˆλŠ” λ¬Έμžμ—΄μ„ 의미
17+
- 문법적 접두사(un-, pre- λ“±)κ°€ μ•„λ‹˜
18+
- λ‹¨μˆœνžˆ λ‹¨μ–΄μ˜ μ•žλΆ€λΆ„μ— μžˆλŠ” λ¬Έμžμ—΄μ„ 의미
1619
- 예: "apple"의 접두사 -> "a", "ap", "app", "appl", "apple"
1720
1821
2. μ‹€ν–‰ 방식
1922
- μž…λ ₯으둜 μ£Όμ–΄μ§„ λͺ…λ Ήμ–΄λ₯Ό 순차적으둜 μ‹€ν–‰
2023
- 예: ["Trie", "insert", "search"] μˆœμ„œλŒ€λ‘œ μ‹€ν–‰
2124
- 각 λͺ…령어에 ν•΄λ‹Ήν•˜λŠ” μΈμžκ°’λ„ μˆœμ„œλŒ€λ‘œ 적용
2225
23-
3. 자료ꡬ쑰 선택
26+
3. 자료ꡬ쑰 선택 (Trie with Hash Table)
2427
- Trie의 각 λ…Έλ“œμ—μ„œ μžμ‹ λ…Έλ“œλ₯Ό μ €μž₯ν•  λ•Œ ν•΄μ‹œ ν…Œμ΄λΈ” μ‚¬μš©
2528
- ν‚€: λ‹€μŒ 문자, κ°’: ν•΄λ‹Ή 문자의 TrieNode
2629
- μž₯점: λ‹€μŒ 문자 검색이 O(1)둜 κ°€λŠ₯
30+
31+
Notes:
32+
- 혼자 κ΅¬ν˜„ν•˜κΈ° μ–΄λ €μ›Œμ„œ, κ³΅λΆ€ν•œ λ‚΄μš©μ„ μ •λ¦¬λ§Œ ν–ˆμŠ΅λ‹ˆλ‹€.
33+
"""
34+
35+
# 트리의 각 "λ…Έλ“œ(λ§ˆλ””)"λ₯Ό ν‘œν˜„ν•˜λŠ” 클래슀
36+
class TrieNode:
37+
def __init__(self):
38+
# children은 "λ‹€μŒ κΈ€μžλ“€μ„ μ €μž₯ν•˜λŠ” 곡간"
39+
# 예: 'a' λ‹€μŒμ— 'p'κ°€ 올 수 μžˆλ‹€λ©΄ children['p']에 μ €μž₯
40+
self.children = {}
41+
42+
# is_endλŠ” "μ—¬κΈ°κΉŒμ§€κ°€ ν•˜λ‚˜μ˜ 단어가 λ˜λŠ”μ§€"λ₯Ό ν‘œμ‹œ
43+
# 예: "app"을 μ €μž₯ν–ˆλ‹€λ©΄, pλ…Έλ“œμ˜ is_endκ°€ True
44+
self.is_end = False
45+
46+
# 트리 전체λ₯Ό κ΄€λ¦¬ν•˜λŠ” 클래슀
47+
class Trie:
48+
def __init__(self):
49+
# 트리의 μ‹œμž‘μ  생성
50+
# μ‹€μ œ κΈ€μžλ“€μ€ root λ°‘μ—μ„œλΆ€ν„° μ‹œμž‘
51+
self.root = TrieNode()
52+
53+
# μƒˆλ‘œμš΄ 단어λ₯Ό νŠΈλ¦¬μ— μΆ”κ°€ν•˜λŠ” ν•¨μˆ˜
54+
def insert(self, word: str) -> None:
55+
node = self.root
56+
for char in word:
57+
if char not in node.children:
58+
node.children[char] = TrieNode()
59+
node = node.children[char]
60+
node.is_end = True
61+
62+
# 단어가 νŠΈλ¦¬μ— μžˆλŠ”μ§€ μ°ΎλŠ” ν•¨μˆ˜
63+
def search(self, word: str) -> bool:
64+
node = self.root
65+
for char in word:
66+
if char in node.children:
67+
node = node.children[char]
68+
else:
69+
return False
70+
return node.is_end
71+
72+
# ν•΄λ‹Ή μ ‘λ‘μ‚¬λ‘œ μ‹œμž‘ν•˜λŠ” 단어가 μžˆλŠ”μ§€ ν™•μΈν•˜λŠ” ν•¨μˆ˜
73+
def startsWith(self, prefix: str) -> bool:
74+
node = self.root
75+
for char in prefix:
76+
if char in node.children:
77+
node = node.children[char]
78+
else:
79+
return False
80+
return True
81+
82+
"""
83+
μ‚¬μš© μ˜ˆμ‹œ:
84+
trie = Trie()
85+
trie.insert("apple") # "apple" μ €μž₯
86+
trie.search("apple") # True λ°˜ν™˜ (apple이 있음)
87+
trie.search("app") # False λ°˜ν™˜ (app은 μ—†μŒ)
88+
trie.startsWith("app") # True λ°˜ν™˜ (app으둜 μ‹œμž‘ν•˜λŠ” 단어가 있음)
2789
"""

0 commit comments

Comments
Β (0)