From 5978f55f7813499d68bdd8aa2d612948c8c74a82 Mon Sep 17 00:00:00 2001 From: Kartheek Kotha Date: Wed, 14 Jun 2023 20:16:14 +0530 Subject: [PATCH 1/2] Fixed bug in remove function --- .../binary_tree/binary_search_tree.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/data_structures/binary_tree/binary_search_tree.py b/data_structures/binary_tree/binary_search_tree.py index cd88cc10e697..389ab1ac72a1 100644 --- a/data_structures/binary_tree/binary_search_tree.py +++ b/data_structures/binary_tree/binary_search_tree.py @@ -34,7 +34,12 @@ def __str__(self) -> str: def __reassign_nodes(self, node: Node, new_children: Node | None) -> None: if new_children is not None: # reset its kids new_children.parent = node.parent - if node.parent is not None: # reset its parent + if(node == self.root):#This check if the node is the head and assign the root to the remaining + if self.is_right(node): # If it is the right children + self.root = new_children + else: + self.root = new_children + elif( node.parent is not None): # reset its parent if self.is_right(node): # If it is the right children node.parent.right = new_children else: @@ -194,6 +199,15 @@ def binary_search_tree() -> None: 8 3 1 6 4 7 10 14 13 >>> print(" ".join(repr(i.value) for i in t.traversal_tree(postorder))) 1 4 7 6 3 13 14 10 8 + >>> t.remove(10) + >>> print(" ".join(repr(i.value) for i in t.traversal_tree())) + 8 3 1 6 4 7 14 13 + >>> t.remove(8) + >>> print(" ".join(repr(i.value) for i in t.traversal_tree())) + 7 3 1 6 4 14 13 + >>> t.remove(14) + >>> print(" ".join(repr(i.value) for i in t.traversal_tree())) + 7 3 1 6 4 13 >>> BinarySearchTree().search(6) Traceback (most recent call last): ... @@ -203,10 +217,8 @@ def binary_search_tree() -> None: t = BinarySearchTree() for i in testlist: t.insert(i) - # Prints all the elements of the list in order traversal print(t) - if t.search(6) is not None: print("The value 6 exists") else: From f0f2c6cb704719bcc3cece7d54e63391595be42b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 14 Jun 2023 14:49:54 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/binary_search_tree.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/binary_search_tree.py b/data_structures/binary_tree/binary_search_tree.py index 389ab1ac72a1..defc82fdd1a4 100644 --- a/data_structures/binary_tree/binary_search_tree.py +++ b/data_structures/binary_tree/binary_search_tree.py @@ -34,12 +34,14 @@ def __str__(self) -> str: def __reassign_nodes(self, node: Node, new_children: Node | None) -> None: if new_children is not None: # reset its kids new_children.parent = node.parent - if(node == self.root):#This check if the node is the head and assign the root to the remaining + if ( + node == self.root + ): # This check if the node is the head and assign the root to the remaining if self.is_right(node): # If it is the right children self.root = new_children else: self.root = new_children - elif( node.parent is not None): # reset its parent + elif node.parent is not None: # reset its parent if self.is_right(node): # If it is the right children node.parent.right = new_children else: