Skip to content

Commit 32e2f2c

Browse files
committed
feat: update dict tree
1 parent 4d81400 commit 32e2f2c

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

.templates/leetcode/json/implement_trie_prefix_tree.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
],
1515
"readme_constraints": "- `1 <= word.length, prefix.length <= 2000`\n- `word` and `prefix` consist only of lowercase English letters.\n- At most `3 * 10^4` calls **in total** will be made to `insert`, `search`, and `starts_with`.",
1616
"readme_additional": "",
17-
"solution_imports": "from leetcode_py.data_structures import DictTree",
17+
"solution_imports": "from leetcode_py.data_structures import DictTree, RecursiveDict",
1818
"solution_methods": [
19-
{ "name": "__init__", "parameters": "", "return_type": "None", "dummy_return": "" },
19+
{
20+
"name": "__init__",
21+
"parameters": "",
22+
"return_type": "None",
23+
"dummy_return": "super().__init__()\n self.root: RecursiveDict[str] = {}"
24+
},
2025
{ "name": "insert", "parameters": "word: str", "return_type": "None", "dummy_return": "" },
2126
{ "name": "search", "parameters": "word: str", "return_type": "bool", "dummy_return": "False" },
2227
{

leetcode/implement_trie_prefix_tree/playground.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
{
6161
"cell_type": "code",
6262
"execution_count": 5,
63-
"id": "f3bac41e",
63+
"id": "c8308208",
6464
"metadata": {},
6565
"outputs": [
6666
{
@@ -170,7 +170,7 @@
170170
"</svg>\n"
171171
],
172172
"text/plain": [
173-
"<solution.Trie at 0x10406a270>"
173+
"<solution.Trie at 0x10771dfd0>"
174174
]
175175
},
176176
"execution_count": 5,
@@ -207,7 +207,7 @@
207207
"file_extension": ".py",
208208
"mimetype": "text/x-python",
209209
"name": "python",
210-
"nbconvert_exporter": "python3",
210+
"nbconvert_exporter": "python",
211211
"pygments_lexer": "ipython3",
212212
"version": "3.13.7"
213213
}

leetcode/implement_trie_prefix_tree/solution.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from typing import Any
2-
3-
from leetcode_py.data_structures import DictTree
1+
from leetcode_py.data_structures import DictTree, RecursiveDict
42

53

64
class Trie(DictTree[str]):
@@ -9,7 +7,7 @@ class Trie(DictTree[str]):
97
# Time: O(1)
108
# Space: O(1)
119
def __init__(self) -> None:
12-
self.root: dict[str, Any] = {}
10+
self.root: RecursiveDict[str] = {}
1311

1412
# Time: O(m) where m is word length
1513
# Space: O(m)
@@ -19,7 +17,7 @@ def insert(self, word: str) -> None:
1917
if char not in node:
2018
node[char] = {}
2119
node = node[char]
22-
node[self.END_OF_WORD] = True # End of word marker
20+
node[self.END_OF_WORD] = True
2321

2422
# Time: O(m) where m is word length
2523
# Space: O(1)

leetcode/lowest_common_ancestor_of_a_binary_tree/tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ def setup_method(self):
1616
([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 5, 1, 3),
1717
([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 5, 4, 5),
1818
([1, 2], 1, 2, 1),
19+
([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 6, 7, 5),
20+
([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 7, 4, 2),
21+
([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 0, 8, 1),
22+
([1], 1, 1, 1),
23+
([2, 1, 3], 1, 3, 2),
24+
([6, 2, 8, 0, 4, 7, 9, None, None, 3, 5], 2, 8, 6),
25+
([6, 2, 8, 0, 4, 7, 9, None, None, 3, 5], 3, 5, 4),
26+
([6, 2, 8, 0, 4, 7, 9, None, None, 3, 5], 0, 3, 2),
1927
],
2028
)
2129
@logged_test
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from .dict_tree import DictTree
1+
from .dict_tree import DictTree, RecursiveDict
22
from .graph_node import GraphNode
33
from .list_node import ListNode
44
from .tree_node import TreeNode
55

6-
__all__ = ["DictTree", "GraphNode", "ListNode", "TreeNode"]
6+
__all__ = ["DictTree", "GraphNode", "ListNode", "RecursiveDict", "TreeNode"]

leetcode_py/data_structures/dict_tree.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
from typing import Any, Generic, Hashable, TypeVar
1+
from typing import Any, Generic, Hashable, TypeAlias, TypeVar
22

33
K = TypeVar("K", bound=Hashable)
4+
RecursiveDict: TypeAlias = dict[K, "RecursiveDict[K] | Any"]
45

56

67
class DictTree(Generic[K]):
78

89
def __init__(self) -> None:
9-
self.root: dict[K, Any] = {}
10+
self.root: RecursiveDict[K] = {}
1011

1112
def __str__(self) -> str:
1213
if not hasattr(self, "root") or not self.root:
1314
return "Empty"
1415
return self._render_dict_tree(self.root)
1516

16-
def _render_dict_tree(self, node: dict[K, Any], prefix: str = "", depth: int = 0) -> str:
17+
def _render_dict_tree(self, node: RecursiveDict[K], prefix: str = "", depth: int = 0) -> str:
1718
if not node:
1819
return ""
1920

@@ -49,7 +50,9 @@ def _repr_html_(self) -> str:
4950
self._add_dict_nodes(dot, self.root, "root")
5051
return dot.pipe(format="svg", encoding="utf-8")
5152

52-
def _add_dict_nodes(self, dot: Any, node: dict[K, Any], node_id: str, char: K | str = "") -> None:
53+
def _add_dict_nodes(
54+
self, dot: Any, node: RecursiveDict[K], node_id: str, char: K | str = ""
55+
) -> None:
5356
if not node:
5457
return
5558

0 commit comments

Comments
 (0)