Skip to content

[pull] master from TheAlgorithms:master #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions data_structures/binary_tree/mirror_binary_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def mirror(self) -> Node:
def make_tree_seven() -> Node:
r"""
Return a binary tree with 7 nodes that looks like this:
::

1
/ \
2 3
Expand All @@ -81,13 +83,15 @@ def make_tree_seven() -> Node:
def make_tree_nine() -> Node:
r"""
Return a binary tree with 9 nodes that looks like this:
1
/ \
2 3
/ \ \
4 5 6
/ \ \
7 8 9
::

1
/ \
2 3
/ \ \
4 5 6
/ \ \
7 8 9

>>> tree_nine = make_tree_nine()
>>> len(tree_nine)
Expand Down Expand Up @@ -117,23 +121,25 @@ def main() -> None:
>>> tuple(tree.mirror())
(6, 3, 1, 9, 5, 2, 8, 4, 7)

nine_tree:
1
/ \
2 3
/ \ \
4 5 6
/ \ \
7 8 9

The mirrored tree looks like this:
nine_tree::

1
/ \
2 3
/ \ \
4 5 6
/ \ \
7 8 9

The mirrored tree looks like this::

1
/ \
3 2
/ / \
6 5 4
/ / \
9 8 7
/ \
3 2
/ / \
6 5 4
/ / \
9 8 7
"""
trees = {"zero": Node(0), "seven": make_tree_seven(), "nine": make_tree_nine()}
for name, tree in trees.items():
Expand Down
24 changes: 14 additions & 10 deletions graphs/check_bipatrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ def is_bipartite_dfs(graph: defaultdict[int, list[int]]) -> bool:
Check if a graph is bipartite using depth-first search (DFS).

Args:
graph: Adjacency list representing the graph.
`graph`: Adjacency list representing the graph.

Returns:
True if bipartite, False otherwise.
``True`` if bipartite, ``False`` otherwise.

Checks if the graph can be divided into two sets of vertices, such that no two
vertices within the same set are connected by an edge.

Examples:
# FIXME: This test should pass.

>>> # FIXME: This test should pass.
>>> is_bipartite_dfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 4]}))
Traceback (most recent call last):
...
Expand All @@ -37,7 +38,7 @@ def is_bipartite_dfs(graph: defaultdict[int, list[int]]) -> bool:
...
KeyError: 0

# FIXME: This test should fails with KeyError: 4.
>>> # FIXME: This test should fails with KeyError: 4.
>>> is_bipartite_dfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]})
False
>>> is_bipartite_dfs({0: [-1, 3], 1: [0, -2]})
Expand All @@ -51,7 +52,8 @@ def is_bipartite_dfs(graph: defaultdict[int, list[int]]) -> bool:
...
KeyError: 0

# FIXME: This test should fails with TypeError: list indices must be integers or...
>>> # FIXME: This test should fails with
>>> # TypeError: list indices must be integers or...
>>> is_bipartite_dfs({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]})
True
>>> is_bipartite_dfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]})
Expand Down Expand Up @@ -95,16 +97,17 @@ def is_bipartite_bfs(graph: defaultdict[int, list[int]]) -> bool:
Check if a graph is bipartite using a breadth-first search (BFS).

Args:
graph: Adjacency list representing the graph.
`graph`: Adjacency list representing the graph.

Returns:
True if bipartite, False otherwise.
``True`` if bipartite, ``False`` otherwise.

Check if the graph can be divided into two sets of vertices, such that no two
vertices within the same set are connected by an edge.

Examples:
# FIXME: This test should pass.

>>> # FIXME: This test should pass.
>>> is_bipartite_bfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 4]}))
Traceback (most recent call last):
...
Expand All @@ -126,7 +129,7 @@ def is_bipartite_bfs(graph: defaultdict[int, list[int]]) -> bool:
...
KeyError: 0

# FIXME: This test should fails with KeyError: 4.
>>> # FIXME: This test should fails with KeyError: 4.
>>> is_bipartite_bfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]})
False
>>> is_bipartite_bfs({0: [-1, 3], 1: [0, -2]})
Expand All @@ -140,7 +143,8 @@ def is_bipartite_bfs(graph: defaultdict[int, list[int]]) -> bool:
...
KeyError: 0

# FIXME: This test should fails with TypeError: list indices must be integers or...
>>> # FIXME: This test should fails with
>>> # TypeError: list indices must be integers or...
>>> is_bipartite_bfs({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]})
True
>>> is_bipartite_bfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]})
Expand Down