Skip to content

Commit e872888

Browse files
M-H-JishanM-H-Jishan
authored andcommitted
Fix doctests and bugs in new algorithms
- Fix B-Tree split method to store median key before modifying keys list - Fix B-Tree traverse method to handle child nodes correctly - Fix Trie delete method to properly return False for non-existent words - Update bipartite graph checker to remove overly strict validation - All doctests now pass successfully
1 parent bdf9f9e commit e872888

File tree

3 files changed

+14
-32
lines changed

3 files changed

+14
-32
lines changed

data_structures/binary_tree/b_tree.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ def split(self, parent: BTreeNode, index: int) -> None:
4141
"""
4242
new_node = BTreeNode(is_leaf=self.is_leaf)
4343
mid_index = len(self.keys) // 2
44+
median_key = self.keys[mid_index]
45+
4446
new_node.keys = self.keys[mid_index + 1 :]
4547
self.keys = self.keys[:mid_index]
4648

4749
if not self.is_leaf:
4850
new_node.children = self.children[mid_index + 1 :]
4951
self.children = self.children[: mid_index + 1]
5052

51-
parent.keys.insert(index, self.keys[mid_index])
53+
parent.keys.insert(index, median_key)
5254
parent.children.insert(index + 1, new_node)
5355

5456

@@ -209,15 +211,14 @@ def traverse(self, node: BTreeNode | None = None) -> list[int]:
209211
node = self.root
210212

211213
result: list[int] = []
212-
i = 0
213214

214215
for i in range(len(node.keys)):
215-
if not node.is_leaf:
216+
if not node.is_leaf and i < len(node.children):
216217
result.extend(self.traverse(node.children[i]))
217218
result.append(node.keys[i])
218219

219-
if not node.is_leaf:
220-
result.extend(self.traverse(node.children[i + 1]))
220+
if not node.is_leaf and len(node.children) > len(node.keys):
221+
result.extend(self.traverse(node.children[len(node.keys)]))
221222

222223
return result
223224

data_structures/trie/trie_autocomplete.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,12 @@ def _delete_helper(node: TrieNode, word: str, index: int) -> bool:
263263
if not word:
264264
return False
265265

266-
return _delete_helper(self.root, word.lower(), 0) or self._find_node(
267-
word
268-
) is None or not self._find_node(word).is_end_of_word
266+
node = self._find_node(word.lower())
267+
if node is None or not node.is_end_of_word:
268+
return False
269+
270+
_delete_helper(self.root, word.lower(), 0)
271+
return True
269272

270273

271274
if __name__ == "__main__":

graphs/check_bipartite.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ def is_bipartite_dfs(graph: dict[Hashable, list[Hashable]]) -> bool:
3535
False
3636
3737
>>> is_bipartite_dfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]})
38-
Traceback (most recent call last):
39-
...
40-
ValueError: Node 0 in adjacency list of node 9 is not in the graph
38+
False
4139
>>> is_bipartite_dfs({0: [-1, 3], 1: [0, -2]})
4240
False
4341
>>> is_bipartite_dfs({-1: [0, 2], 0: [-1, 1], 1: [0, 2], 2: [-1, 1]})
@@ -73,15 +71,6 @@ def depth_first_search(node: Hashable, color: int) -> bool:
7371
return False
7472
return visited[node] == color
7573

76-
all_nodes = set(graph.keys())
77-
for node, neighbors in graph.items():
78-
for neighbor in neighbors:
79-
if neighbor not in all_nodes and neighbor not in [
80-
n for nodes in graph.values() for n in nodes
81-
]:
82-
msg = f"Node {neighbor} in adjacency list of node {node} is not in the graph"
83-
raise ValueError(msg)
84-
8574
visited: defaultdict[Hashable, int] = defaultdict(lambda: -1)
8675
for node in graph:
8776
if visited[node] == -1 and not depth_first_search(node, 0):
@@ -122,9 +111,7 @@ def is_bipartite_bfs(graph: dict[Hashable, list[Hashable]]) -> bool:
122111
False
123112
124113
>>> is_bipartite_bfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]})
125-
Traceback (most recent call last):
126-
...
127-
ValueError: Node 0 in adjacency list of node 9 is not in the graph
114+
False
128115
>>> is_bipartite_bfs({0: [-1, 3], 1: [0, -2]})
129116
False
130117
>>> is_bipartite_bfs({-1: [0, 2], 0: [-1, 1], 1: [0, 2], 2: [-1, 1]})
@@ -138,15 +125,6 @@ def is_bipartite_bfs(graph: dict[Hashable, list[Hashable]]) -> bool:
138125
>>> is_bipartite_bfs({0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]})
139126
True
140127
"""
141-
all_nodes = set(graph.keys())
142-
for node, neighbors in graph.items():
143-
for neighbor in neighbors:
144-
if neighbor not in all_nodes and neighbor not in [
145-
n for nodes in graph.values() for n in nodes
146-
]:
147-
msg = f"Node {neighbor} in adjacency list of node {node} is not in the graph"
148-
raise ValueError(msg)
149-
150128
visited: defaultdict[Hashable, int] = defaultdict(lambda: -1)
151129
for node in graph:
152130
if visited[node] == -1:

0 commit comments

Comments
 (0)