Skip to content

Commit ba1752e

Browse files
Fix PR review comments
1 parent 63d4cfb commit ba1752e

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

data_structures/advanced_trie.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,26 @@ def _delete_helper(self, node: TrieNode, word: str, index: int) -> bool:
172172
return len(node.children) == 0 and not node.is_end_of_word
173173

174174
def _find_node(self, word: str) -> Optional[TrieNode]:
175-
"""Find the node corresponding to the given word."""
175+
"""
176+
Find the node corresponding to the given word.
177+
178+
Args:
179+
word: Word to find node for
180+
181+
Returns:
182+
TrieNode if found, None otherwise
183+
184+
Examples:
185+
>>> trie = Trie()
186+
>>> trie.insert("hello")
187+
>>> node = trie._find_node("hello")
188+
>>> node is not None
189+
True
190+
>>> node.is_end_of_word
191+
True
192+
>>> trie._find_node("world") is None
193+
True
194+
"""
176195
node = self.root
177196
for char in word:
178197
if char not in node.children:
@@ -209,7 +228,23 @@ def get_all_words_with_prefix(self, prefix: str) -> List[str]:
209228
def _collect_words(
210229
self, node: TrieNode, current_word: str, words: List[str]
211230
) -> None:
212-
"""Collect all words from a given node."""
231+
"""
232+
Collect all words from a given node.
233+
234+
Args:
235+
node: Current trie node
236+
current_word: Current word being built
237+
words: List to collect words into
238+
239+
Examples:
240+
>>> trie = Trie()
241+
>>> trie.insert("hello")
242+
>>> trie.insert("help")
243+
>>> words = []
244+
>>> trie._collect_words(trie._find_node("hel"), "hel", words)
245+
>>> sorted(words)
246+
['hello', 'help']
247+
"""
213248
if node.is_end_of_word:
214249
words.append(current_word)
215250

data_structures/segment_tree.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,35 @@ def query(self, left: int, right: int) -> int:
9494
>>> st = SegmentTree([1, 2, 3, 4, 5])
9595
>>> st.query(1, 3)
9696
9
97+
>>> st.query(0, 4)
98+
15
99+
>>> st.query(2, 2)
100+
3
97101
"""
98102
if left < 0 or right >= self.size or left > right:
99103
raise ValueError(f"Invalid range [{left}, {right}]")
100104

101105
return self._query(0, 0, self.size - 1, left, right)
102106

103107
def _query(self, node: int, start: int, end: int, left: int, right: int) -> int:
104-
"""Internal query method."""
108+
"""
109+
Internal query method.
110+
111+
Args:
112+
node: Current node index
113+
start: Start of current segment
114+
end: End of current segment
115+
left: Left boundary of query
116+
right: Right boundary of query
117+
118+
Returns:
119+
Result of the operation over the range
120+
121+
Examples:
122+
>>> st = SegmentTree([1, 2, 3, 4, 5])
123+
>>> st._query(0, 0, 4, 1, 3)
124+
9
125+
"""
105126
if right < start or left > end:
106127
return self.default_value
107128

@@ -238,7 +259,23 @@ def range_update(self, left: int, right: int, delta: int) -> None:
238259
def _range_update(
239260
self, node: int, start: int, end: int, left: int, right: int, delta: int
240261
) -> None:
241-
"""Internal range update method."""
262+
"""
263+
Internal range update method with lazy propagation.
264+
265+
Args:
266+
node: Current node index
267+
start: Start of current segment
268+
end: End of current segment
269+
left: Left boundary of update
270+
right: Right boundary of update
271+
delta: Value to add to the range
272+
273+
Examples:
274+
>>> lst = LazySegmentTree([1, 2, 3, 4, 5])
275+
>>> lst._range_update(0, 0, 4, 1, 3, 2)
276+
>>> lst.query(1, 3)
277+
11
278+
"""
242279
self._push_lazy(node, start, end)
243280

244281
if right < start or left > end:
@@ -300,14 +337,14 @@ def _query(self, node: int, start: int, end: int, left: int, right: int) -> int:
300337
class MinSegmentTree(SegmentTree):
301338
"""Segment Tree for range minimum queries."""
302339

303-
def __init__(self, data: List[int]):
340+
def __init__(self, data: List[int]) -> None:
304341
super().__init__(data, min, float("inf"))
305342

306343

307344
class MaxSegmentTree(SegmentTree):
308345
"""Segment Tree for range maximum queries."""
309346

310-
def __init__(self, data: List[int]):
347+
def __init__(self, data: List[int]) -> None:
311348
super().__init__(data, max, float("-inf"))
312349

313350

0 commit comments

Comments
 (0)