அல்கோரிதம்trie data structure
Trie — முன்னொட்டு மரம்
TT
Testlaa Team
May 14, 2026•1 min read
Trie — எழுத்துகள் path-ஆக; பொது prefix — பொது path.
Try this in Python
class TrieNode:
__slots__ = ("children", "end")
def __init__(self) -> None:
self.children: dict[str, TrieNode] = {}
self.end = False
class Trie:
def __init__(self) -> None:
self.root = TrieNode()
def insert(self, word: str) -> None:
n = self.root
for ch in word:
n = n.children.setdefault(ch, TrieNode())
n.end = True
def search(self, word: str) -> bool:
n = self.root
for ch in word:
if ch not in n.children:
return False
n = n.children[ch]
return n.end
def starts_with(self, prefix: str) -> bool:
n = self.root
for ch in prefix:
if ch not in n.children:
return False
n = n.children[ch]
return True
t = Trie()
t.insert("apple")
print(t.search("apple"), t.starts_with("app"), t.search("ap"))
Key takeaways
- Node — அடுத்த char map.
end— முழு சொல்.- Depth — string length.
Tags:
TriePythonமாணவர்கள்
