Skip to content

Commit

Permalink
Merge pull request #29 from shunsvineyard/bug_fix
Browse files Browse the repository at this point in the history
Bug fix: delete on left threaded binary search tree.
  • Loading branch information
Shun Huang authored Jul 10, 2021
2 parents 585e924 + 0fafb14 commit f6dfd27
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
15 changes: 5 additions & 10 deletions forest/binary_trees/single_threaded_binary_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,17 +582,12 @@ def delete(self, key: Any) -> None:
elif deleting_node.right and deleting_node.left:
replacing_node: Node = self.get_leftmost(node=deleting_node.right)
successor = self.get_successor(node=replacing_node)
# the minmum node is not the direct child of the deleting node
# the leftmost node is not the direct child of the deleting node
if replacing_node.parent != deleting_node:
if replacing_node.is_thread:
self._transplant(
deleting_node=replacing_node, replacing_node=None
)
else:
self._transplant(
deleting_node=replacing_node,
replacing_node=replacing_node.right,
)
self._transplant(
deleting_node=replacing_node,
replacing_node=replacing_node.right,
)
replacing_node.right = deleting_node.right
replacing_node.right.parent = replacing_node

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# This call to setup() does all the work
setuptools.setup(
name="forest-python",
version="0.5.2",
version="0.5.3",
description="The Forest Project in Python",
long_description=README,
long_description_content_type="text/x-rst",
Expand Down
31 changes: 31 additions & 0 deletions tests/test_single_threaded_binary_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,34 @@ def test_deletion_left_threaded_case(basic_tree):
(4, "4"),
(1, "1"),
] == [item for item in tree.reverse_inorder_traverse()]


def test_deletion_left_threaded_case_2():
"""Test the deletion of a left threaded binary tree."""
tree = single_threaded_binary_trees.LeftThreadedBinaryTree()

test_tree = [
(4, "4"),
(1, "1"),
(7, "7"),
(3, "3"),
(5, "5"),
(8, "8"),
(2, "2"),
(6, "6"),
]

for key, data in test_tree:
tree.insert(key=key, data=data)

tree.delete(4)

assert [
(8, "8"),
(7, "7"),
(6, "6"),
(5, "5"),
(3, "3"),
(2, "2"),
(1, "1"),
] == [item for item in tree.reverse_inorder_traverse()]

0 comments on commit f6dfd27

Please sign in to comment.