Created
December 18, 2019 18:18
-
-
Save davidrft/14c4110f67760fd8a326f7f644a7f7a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any, Dict | |
class Trie: | |
def __init__(self) -> None: | |
self.children : Dict[str, Trie] = {} | |
self.value : Any = None | |
def find(self, key: str) -> Any: | |
node = self | |
for char in key: | |
if char in node.children: | |
node = node.children[char] | |
else: | |
return None | |
return node.value | |
def insert(self, key: str, value: Any) -> None: | |
node = self | |
for char in key: | |
if char not in node.children: | |
node.children[char] = Trie() | |
node = node.children[char] | |
node.value = value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment