Skip to content

Instantly share code, notes, and snippets.

@davidrft
Created December 18, 2019 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidrft/14c4110f67760fd8a326f7f644a7f7a1 to your computer and use it in GitHub Desktop.
Save davidrft/14c4110f67760fd8a326f7f644a7f7a1 to your computer and use it in GitHub Desktop.
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