Skip to content

add a test case for red-black tree #4028

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

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 23 additions & 0 deletions data_structures/binary_tree/red_black_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,28 @@ def test_insert_and_search() -> bool:
return True


def test_insert_and_search_bug() -> bool:
"""A test case for debug."""
tree = RedBlackTree(10)
tree.insert(20)
tree.insert(5)
tree.insert(2)
tree.insert(3)
tree.insert(30)
tree.insert(35)
tree.insert(21)
if len(tree) != 8:
return False
tree.remove(3)
tree.remove(2)
tree.remove(5)
if len(tree) != 5:
# please check the output of:
# print(list(tree.inorder_traverse()))
return False
return True


def test_insert_delete() -> bool:
"""Test the insert() and delete() method of the tree, verifying the
insertion and removal of elements, and the balancing of the tree.
Expand Down Expand Up @@ -688,6 +710,7 @@ def pytests() -> None:
assert test_floor_ceil()
assert test_tree_traversal()
assert test_tree_chaining()
assert test_insert_and_search_bug()


def main() -> None:
Expand Down