From f2710a825d1def55c33557f4bfc52d22f5fe2ea6 Mon Sep 17 00:00:00 2001 From: Anoop <140630377+daichianoop@users.noreply.github.com> Date: Wed, 9 Oct 2024 22:31:05 +0530 Subject: [PATCH 1/3] Add files via upload --- .../binary_tree/binary-tree-deletion.py | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 data_structures/binary_tree/binary-tree-deletion.py diff --git a/data_structures/binary_tree/binary-tree-deletion.py b/data_structures/binary_tree/binary-tree-deletion.py new file mode 100644 index 000000000000..e97fe7deb9f3 --- /dev/null +++ b/data_structures/binary_tree/binary-tree-deletion.py @@ -0,0 +1,83 @@ +class Node: + def __init__(self, data): + self.data = data + self.left = None + self.right = None + +# Function to insert a new node in the BST +def insert(root, data): + if root is None: + return Node(data) + if data < root.data: + root.left = insert(root.left, data) + else: + root.right = insert(root.right, data) + return root + +# Function to perform in-order traversal of the BST +def in_order_traversal(root): + if root is not None: + in_order_traversal(root.left) + print(root.data, end=' ') + in_order_traversal(root.right) + +# Function to find the minimum value node in a given subtree +def min_value_node(node): + current = node + while current and current.left is not None: + current = current.left + return current + +# Function to delete a node from the BST +def delete_node(root, key): + if root is None: + return root + + if key < root.data: + root.left = delete_node(root.left, key) + elif key > root.data: + root.right = delete_node(root.right, key) + else: + # Node with only one child or no child + if root.left is None: + temp = root.right + root = None # Clear the node + return temp + elif root.right is None: + temp = root.left + root = None # Clear the node + return temp + + # Node with two children: get the inorder successor + temp = min_value_node(root.right) + root.data = temp.data + root.right = delete_node(root.right, temp.data) + + return root + +def main(): + root = None + + # Inserting nodes into the BST + root = insert(root, 50) + insert(root, 30) + insert(root, 20) + insert(root, 40) + insert(root, 70) + insert(root, 60) + insert(root, 80) + + print("Inorder Traversal of the BST before deletion:") + in_order_traversal(root) + print("\n") + + # Deleting a node + key = int(input("Enter the Key Element to be deleted: ")) + root = delete_node(root, key) + print(f"Element ({key}) Deleted") + print(f"Inorder Traversal of the BST after deletion of {key}:") + in_order_traversal(root) + print("\n") + +if __name__ == "__main__": + main() \ No newline at end of file From 62b6d301dc27c57829d77297578805765b7ec49e Mon Sep 17 00:00:00 2001 From: daichianoop Date: Wed, 9 Oct 2024 17:01:18 +0000 Subject: [PATCH 2/3] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553946..324591e349a7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -195,6 +195,7 @@ * Binary Tree * [Avl Tree](data_structures/binary_tree/avl_tree.py) * [Basic Binary Tree](data_structures/binary_tree/basic_binary_tree.py) + * [Binary-Tree-Deletion](data_structures/binary_tree/binary-tree-deletion.py) * [Binary Search Tree](data_structures/binary_tree/binary_search_tree.py) * [Binary Search Tree Recursive](data_structures/binary_tree/binary_search_tree_recursive.py) * [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py) From 543201c5fba73093196a9fbe9ccbabeda78e144a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Sep 2026 05:50:08 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../binary_tree/binary-tree-deletion.py | 172 +++++++++--------- 1 file changed, 89 insertions(+), 83 deletions(-) diff --git a/data_structures/binary_tree/binary-tree-deletion.py b/data_structures/binary_tree/binary-tree-deletion.py index e97fe7deb9f3..c8db305a56dc 100644 --- a/data_structures/binary_tree/binary-tree-deletion.py +++ b/data_structures/binary_tree/binary-tree-deletion.py @@ -1,83 +1,89 @@ -class Node: - def __init__(self, data): - self.data = data - self.left = None - self.right = None - -# Function to insert a new node in the BST -def insert(root, data): - if root is None: - return Node(data) - if data < root.data: - root.left = insert(root.left, data) - else: - root.right = insert(root.right, data) - return root - -# Function to perform in-order traversal of the BST -def in_order_traversal(root): - if root is not None: - in_order_traversal(root.left) - print(root.data, end=' ') - in_order_traversal(root.right) - -# Function to find the minimum value node in a given subtree -def min_value_node(node): - current = node - while current and current.left is not None: - current = current.left - return current - -# Function to delete a node from the BST -def delete_node(root, key): - if root is None: - return root - - if key < root.data: - root.left = delete_node(root.left, key) - elif key > root.data: - root.right = delete_node(root.right, key) - else: - # Node with only one child or no child - if root.left is None: - temp = root.right - root = None # Clear the node - return temp - elif root.right is None: - temp = root.left - root = None # Clear the node - return temp - - # Node with two children: get the inorder successor - temp = min_value_node(root.right) - root.data = temp.data - root.right = delete_node(root.right, temp.data) - - return root - -def main(): - root = None - - # Inserting nodes into the BST - root = insert(root, 50) - insert(root, 30) - insert(root, 20) - insert(root, 40) - insert(root, 70) - insert(root, 60) - insert(root, 80) - - print("Inorder Traversal of the BST before deletion:") - in_order_traversal(root) - print("\n") - - # Deleting a node - key = int(input("Enter the Key Element to be deleted: ")) - root = delete_node(root, key) - print(f"Element ({key}) Deleted") - print(f"Inorder Traversal of the BST after deletion of {key}:") - in_order_traversal(root) - print("\n") - -if __name__ == "__main__": - main() \ No newline at end of file +class Node: + def __init__(self, data): + self.data = data + self.left = None + self.right = None + + +# Function to insert a new node in the BST +def insert(root, data): + if root is None: + return Node(data) + if data < root.data: + root.left = insert(root.left, data) + else: + root.right = insert(root.right, data) + return root + + +# Function to perform in-order traversal of the BST +def in_order_traversal(root): + if root is not None: + in_order_traversal(root.left) + print(root.data, end=" ") + in_order_traversal(root.right) + + +# Function to find the minimum value node in a given subtree +def min_value_node(node): + current = node + while current and current.left is not None: + current = current.left + return current + + +# Function to delete a node from the BST +def delete_node(root, key): + if root is None: + return root + + if key < root.data: + root.left = delete_node(root.left, key) + elif key > root.data: + root.right = delete_node(root.right, key) + else: + # Node with only one child or no child + if root.left is None: + temp = root.right + root = None # Clear the node + return temp + elif root.right is None: + temp = root.left + root = None # Clear the node + return temp + + # Node with two children: get the inorder successor + temp = min_value_node(root.right) + root.data = temp.data + root.right = delete_node(root.right, temp.data) + + return root + + +def main(): + root = None + + # Inserting nodes into the BST + root = insert(root, 50) + insert(root, 30) + insert(root, 20) + insert(root, 40) + insert(root, 70) + insert(root, 60) + insert(root, 80) + + print("Inorder Traversal of the BST before deletion:") + in_order_traversal(root) + print("\n") + + # Deleting a node + key = int(input("Enter the Key Element to be deleted: ")) + root = delete_node(root, key) + print(f"Element ({key}) Deleted") + print(f"Inorder Traversal of the BST after deletion of {key}:") + in_order_traversal(root) + print("\n") + + +if __name__ == "__main__": + main()