From bf44f090d5fcda90a81cb3138c17f789b7f8dca6 Mon Sep 17 00:00:00 2001 From: Adarshsidnal <01fe21bcs288@kletech.ac.in> Date: Thu, 5 Oct 2023 11:03:19 +0530 Subject: [PATCH 1/5] Added an algorithm transfrom bst to greater sum tree --- .../binary_tree/transform_bst_sum_tree.py | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 data_structures/binary_tree/transform_bst_sum_tree.py diff --git a/data_structures/binary_tree/transform_bst_sum_tree.py b/data_structures/binary_tree/transform_bst_sum_tree.py new file mode 100644 index 000000000000..7b102c0c565f --- /dev/null +++ b/data_structures/binary_tree/transform_bst_sum_tree.py @@ -0,0 +1,126 @@ +from __future__ import annotations +class Node: + """ +prints the inorder Traversal of transformed tree +>>> sum = 0 +>>> root = Node(11) +>>> root.left = Node(2) +>>> root.right = Node(29) +>>> root.left.left = Node(1) +>>> root.left.right = Node(7) +>>> root.right.left = Node(15) +>>> root.right.right = Node(40) +>>> root.right.right.left = Node(35) +>>> printInorder(root) +1 2 7 11 15 29 35 40 + +>>> transformTree(root) + +>>> printInorder(root) +139 137 130 119 104 75 40 0 + +""" + + def __init__(self, number:int) -> None: + self.data = number + self.left = None + self.right = None + +# Recursive function to transform a BST to sum tree. +# This function traverses the tree in reverse inorder so +# that we have visited all greater key nodes of the currently +# visited node +def transform_tree_util(root:Node | None) -> None: + """ + Transform a binary tree into a sum tree. + + Example: + >>> root = Node(11) + >>> root.left = Node(2) + >>> root.right = Node(29) + >>> transformTree(root) + >>> root.data + 60 + >>> root.left.data + 31 + >>> root.right.data + 29 + """ + + # Base case + if (root == None): + return + + # Recur for right subtree + transform_tree_util(root.right) + + # Update sum + global sum + sum = sum + root.data + + # Store old sum in the current node + root.data = sum - root.data + + # Recur for left subtree + transform_tree_util(root.left) + +# A wrapper over transformTreeUtil() +def transform_tree(root:Node | None) -> None: + """ + Transform a binary tree into a sum tree. + + Example: + >>> root = Node(11) + >>> root.left = Node(2) + >>> root.right = Node(29) + >>> transformTree(root) + >>> root.data + 60 + >>> root.left.data + 31 + >>> root.right.data + 29 + """ + + # sum = 0 #Initialize sum + transform_tree_util(root) + +# A utility function to prindorder traversal of a +# binary tree +def print_inorder(root:Node | None): + """ + Perform an inorder traversal of a binary tree and print the nodes. + + Example: + >>> root = Node(11) + >>> root.left = Node(2) + >>> root.right = Node(29) + >>> printInorder(root) + 2 11 29 + """ + + if (root == None): + return + + print_inorder(root.left) + print(root.data, end = " ") + print_inorder(root.right) + +# Driver Program to test above functions +if __name__ == '__main__': + + sum = 0 + root = Node(11) + root.left = Node(2) + root.right = Node(29) + root.left.left = Node(1) + root.left.right = Node(7) + root.right.left = Node(15) + root.right.right = Node(40) + root.right.right.left = Node(35) + + print_inorder(root) + + transform_tree_util(root) + print_inorder(root) + From 56377fdba75342f9bb1b4c32e0b32e01b8d3b2f9 Mon Sep 17 00:00:00 2001 From: Adarshsidnal <01fe21bcs288@kletech.ac.in> Date: Thu, 5 Oct 2023 11:37:43 +0530 Subject: [PATCH 2/5] Added an algorithm to convert camel case string into snake case --- .../binary_tree/transform_bst_sum_tree.py | 126 ------------------ strings/camel_case_to_snake_case.py | 39 ++++++ 2 files changed, 39 insertions(+), 126 deletions(-) delete mode 100644 data_structures/binary_tree/transform_bst_sum_tree.py create mode 100644 strings/camel_case_to_snake_case.py diff --git a/data_structures/binary_tree/transform_bst_sum_tree.py b/data_structures/binary_tree/transform_bst_sum_tree.py deleted file mode 100644 index 7b102c0c565f..000000000000 --- a/data_structures/binary_tree/transform_bst_sum_tree.py +++ /dev/null @@ -1,126 +0,0 @@ -from __future__ import annotations -class Node: - """ -prints the inorder Traversal of transformed tree ->>> sum = 0 ->>> root = Node(11) ->>> root.left = Node(2) ->>> root.right = Node(29) ->>> root.left.left = Node(1) ->>> root.left.right = Node(7) ->>> root.right.left = Node(15) ->>> root.right.right = Node(40) ->>> root.right.right.left = Node(35) ->>> printInorder(root) -1 2 7 11 15 29 35 40 - ->>> transformTree(root) - ->>> printInorder(root) -139 137 130 119 104 75 40 0 - -""" - - def __init__(self, number:int) -> None: - self.data = number - self.left = None - self.right = None - -# Recursive function to transform a BST to sum tree. -# This function traverses the tree in reverse inorder so -# that we have visited all greater key nodes of the currently -# visited node -def transform_tree_util(root:Node | None) -> None: - """ - Transform a binary tree into a sum tree. - - Example: - >>> root = Node(11) - >>> root.left = Node(2) - >>> root.right = Node(29) - >>> transformTree(root) - >>> root.data - 60 - >>> root.left.data - 31 - >>> root.right.data - 29 - """ - - # Base case - if (root == None): - return - - # Recur for right subtree - transform_tree_util(root.right) - - # Update sum - global sum - sum = sum + root.data - - # Store old sum in the current node - root.data = sum - root.data - - # Recur for left subtree - transform_tree_util(root.left) - -# A wrapper over transformTreeUtil() -def transform_tree(root:Node | None) -> None: - """ - Transform a binary tree into a sum tree. - - Example: - >>> root = Node(11) - >>> root.left = Node(2) - >>> root.right = Node(29) - >>> transformTree(root) - >>> root.data - 60 - >>> root.left.data - 31 - >>> root.right.data - 29 - """ - - # sum = 0 #Initialize sum - transform_tree_util(root) - -# A utility function to prindorder traversal of a -# binary tree -def print_inorder(root:Node | None): - """ - Perform an inorder traversal of a binary tree and print the nodes. - - Example: - >>> root = Node(11) - >>> root.left = Node(2) - >>> root.right = Node(29) - >>> printInorder(root) - 2 11 29 - """ - - if (root == None): - return - - print_inorder(root.left) - print(root.data, end = " ") - print_inorder(root.right) - -# Driver Program to test above functions -if __name__ == '__main__': - - sum = 0 - root = Node(11) - root.left = Node(2) - root.right = Node(29) - root.left.left = Node(1) - root.left.right = Node(7) - root.right.left = Node(15) - root.right.right = Node(40) - root.right.right.left = Node(35) - - print_inorder(root) - - transform_tree_util(root) - print_inorder(root) - diff --git a/strings/camel_case_to_snake_case.py b/strings/camel_case_to_snake_case.py new file mode 100644 index 000000000000..ebbd1c342b50 --- /dev/null +++ b/strings/camel_case_to_snake_case.py @@ -0,0 +1,39 @@ +import re + +def camel_to_snake_case(input_str: str) -> str: + """ + Transforms a camelCase or PascalCase given string to snake_case + + >>> camel_to_snake_case("someRandomString") + 'some_random_string' + + >>> camel_to_snake_case("SomeRandomString") + 'some_random_string' + + >>> camel_to_snake_case("someRandomStringWithNumbers123") + 'some_random_string_with_numbers_123' + + >>> camel_to_snake_case("SomeRandomStringWithNumbers123") + 'some_random_string_with_numbers_123' + + >>> camel_to_snake_case(123) + Traceback (most recent call last): + ... + ValueError: Expected string as input, found + """ + + if not isinstance(input_str, str): + msg = f"Expected string as input, found {type(input_str)}" + raise ValueError(msg) + + # Use regular expression to split words on capital letters and numbers + words = re.findall(r'[A-Z][a-z]*|\d+|[a-z]+', input_str) + + # Join the words with underscores and convert to lowercase + return '_'.join(words).lower() + + +if __name__ == "__main__": + from doctest import testmod + + testmod() \ No newline at end of file From fb0fdb71c156d99a64b4ce0a4f446a95e620a718 Mon Sep 17 00:00:00 2001 From: Adarshsidnal <01fe21bcs288@kletech.ac.in> Date: Sat, 7 Oct 2023 06:56:02 +0530 Subject: [PATCH 3/5] Made the changes as mentioned by the member --- .../binary_tree/transform_bst_sum_tree.py | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 data_structures/binary_tree/transform_bst_sum_tree.py diff --git a/data_structures/binary_tree/transform_bst_sum_tree.py b/data_structures/binary_tree/transform_bst_sum_tree.py new file mode 100644 index 000000000000..2dba7cf5f492 --- /dev/null +++ b/data_structures/binary_tree/transform_bst_sum_tree.py @@ -0,0 +1,81 @@ +from __future__ import annotations + +class Node: + def __init__(self, number: int) -> None: + self.data = number + self.left = None + self.right = None + +class BinaryTree: + def __init__(self, root=None) -> None: + self.root = root + + def __iter__(self) -> int: + if self.root is not None: + yield from self._traverse_inorder(self.root) + + def __str__(self) -> str: + return " ".join(str(data) for data in self) + + def _traverse_inorder(self, node: Node) -> int: + if node is not None: + yield from self._traverse_inorder(node.left) + yield node.data + yield from self._traverse_inorder(node.right) + + def build_a_tree(self) -> Node: + # Create a binary tree with the specified structure + self.root = Node(11) + self.root.left = Node(2) + self.root.right = Node(29) + self.root.left.left = Node(1) + self.root.left.right = Node(7) + self.root.right.left = Node(15) + self.root.right.right = Node(40) + self.root.right.right.left = Node(35) + return self.root + +def transform_tree_util(root: Node | None) -> None: + if root is None: + return + + # Recur for right subtree + transform_tree_util(root.right) + + # Update sum + global total + total = total + root.data + + # Store old sum in the current node + root.data = total - root.data + + # Recur for left subtree + transform_tree_util(root.left) + +def binary_tree_to_sum_tree(root: Node | None) -> None: + # Call the utility function to transform the tree + transform_tree_util(root) + +if __name__ == '__main__': + total = 0 + tree = BinaryTree() + root = tree.build_a_tree() + # Transform the tree + binary_tree_to_sum_tree(root) + print("Transformed Tree:") + print(tree) + + """ + Test Cases: + + >>> root = Node(11) + >>> root.left = Node(2) + >>> root.right = Node(29) + >>> transform_tree(root) + >>> root.data + 60 + >>> root.left.data + 31 + >>> root.right.data + 29 + """ From 066902c1343cbf27b62e347838a711216d809c25 Mon Sep 17 00:00:00 2001 From: Adarshsidnal <01fe21bcs288@kletech.ac.in> Date: Sat, 7 Oct 2023 06:57:59 +0530 Subject: [PATCH 4/5] Made the changes as mentioned by the member --- strings/camel_case_to_snake_case.py | 39 ----------------------------- 1 file changed, 39 deletions(-) delete mode 100644 strings/camel_case_to_snake_case.py diff --git a/strings/camel_case_to_snake_case.py b/strings/camel_case_to_snake_case.py deleted file mode 100644 index ebbd1c342b50..000000000000 --- a/strings/camel_case_to_snake_case.py +++ /dev/null @@ -1,39 +0,0 @@ -import re - -def camel_to_snake_case(input_str: str) -> str: - """ - Transforms a camelCase or PascalCase given string to snake_case - - >>> camel_to_snake_case("someRandomString") - 'some_random_string' - - >>> camel_to_snake_case("SomeRandomString") - 'some_random_string' - - >>> camel_to_snake_case("someRandomStringWithNumbers123") - 'some_random_string_with_numbers_123' - - >>> camel_to_snake_case("SomeRandomStringWithNumbers123") - 'some_random_string_with_numbers_123' - - >>> camel_to_snake_case(123) - Traceback (most recent call last): - ... - ValueError: Expected string as input, found - """ - - if not isinstance(input_str, str): - msg = f"Expected string as input, found {type(input_str)}" - raise ValueError(msg) - - # Use regular expression to split words on capital letters and numbers - words = re.findall(r'[A-Z][a-z]*|\d+|[a-z]+', input_str) - - # Join the words with underscores and convert to lowercase - return '_'.join(words).lower() - - -if __name__ == "__main__": - from doctest import testmod - - testmod() \ No newline at end of file From ab7c64df9e3bc8409dd43a37160a38bab2ce53ff Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 01:31:57 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/transform_bst_sum_tree.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/data_structures/binary_tree/transform_bst_sum_tree.py b/data_structures/binary_tree/transform_bst_sum_tree.py index 2dba7cf5f492..c21dddd4628e 100644 --- a/data_structures/binary_tree/transform_bst_sum_tree.py +++ b/data_structures/binary_tree/transform_bst_sum_tree.py @@ -1,11 +1,13 @@ from __future__ import annotations + class Node: def __init__(self, number: int) -> None: self.data = number self.left = None self.right = None + class BinaryTree: def __init__(self, root=None) -> None: self.root = root @@ -35,6 +37,7 @@ def build_a_tree(self) -> Node: self.root.right.right.left = Node(35) return self.root + def transform_tree_util(root: Node | None) -> None: if root is None: return @@ -52,11 +55,13 @@ def transform_tree_util(root: Node | None) -> None: # Recur for left subtree transform_tree_util(root.left) + def binary_tree_to_sum_tree(root: Node | None) -> None: # Call the utility function to transform the tree transform_tree_util(root) -if __name__ == '__main__': + +if __name__ == "__main__": total = 0 tree = BinaryTree() root = tree.build_a_tree() @@ -64,10 +69,10 @@ def binary_tree_to_sum_tree(root: Node | None) -> None: binary_tree_to_sum_tree(root) print("Transformed Tree:") print(tree) - + """ Test Cases: - + >>> root = Node(11) >>> root.left = Node(2) >>> root.right = Node(29)