From 672cb3bbd9b2fdd0685e3a294eff7dd466b04149 Mon Sep 17 00:00:00 2001 From: Shaurya Bisht Date: Sat, 12 Oct 2024 19:05:40 +0530 Subject: [PATCH 1/4] Improved del_node function in avl_tree.py In previous version of code balance factor was not taken in account So, I updated the code using balance factor --- data_structures/binary_tree/avl_tree.py | 33 +++++++++++++++++-------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 9fca72374..9468b5230 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -195,9 +195,15 @@ def get_left_most(root: MyNode) -> Any: return root.get_data() + def del_node(root: MyNode, data: Any) -> MyNode | None: + if root is None: + print("Node is empty, nothing to delete") + return None + left_child = root.get_left() right_child = root.get_right() + if root.get_data() == data: if left_child is not None and right_child is not None: temp_data = get_left_most(right_child) @@ -209,32 +215,39 @@ def del_node(root: MyNode, data: Any) -> MyNode | None: root = right_child else: return None - elif root.get_data() > data: + elif data < root.get_data(): if left_child is None: - print("No such data") + print(f"No such data ({data}) exists in the left subtree.") return root else: root.set_left(del_node(left_child, data)) - # root.get_data() < data - elif right_child is None: - return root else: - root.set_right(del_node(right_child, data)) + if right_child is None: + print(f"No such data ({data}) exists in the right subtree.") + return root + else: + root.set_right(del_node(right_child, data)) - if get_height(right_child) - get_height(left_child) == 2: + # Update the height of the node + root.set_height(1 + my_max(get_height(root.get_left()), get_height(root.get_right()))) + + # Get the balance factor + balance_factor = get_height(root.get_right()) - get_height(root.get_left()) + + # Balance the tree + if balance_factor == 2: assert right_child is not None if get_height(right_child.get_right()) > get_height(right_child.get_left()): root = left_rotation(root) else: root = rl_rotation(root) - elif get_height(right_child) - get_height(left_child) == -2: + elif balance_factor == -2: assert left_child is not None if get_height(left_child.get_left()) > get_height(left_child.get_right()): root = right_rotation(root) else: root = lr_rotation(root) - height = my_max(get_height(root.get_right()), get_height(root.get_left())) + 1 - root.set_height(height) + return root From c91f6b635996a31df40298692e306b8b18d4d480 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:36:43 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/avl_tree.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py index 9468b5230..4ecdae720 100644 --- a/data_structures/binary_tree/avl_tree.py +++ b/data_structures/binary_tree/avl_tree.py @@ -195,7 +195,6 @@ def get_left_most(root: MyNode) -> Any: return root.get_data() - def del_node(root: MyNode, data: Any) -> MyNode | None: if root is None: print("Node is empty, nothing to delete") @@ -229,7 +228,9 @@ def del_node(root: MyNode, data: Any) -> MyNode | None: root.set_right(del_node(right_child, data)) # Update the height of the node - root.set_height(1 + my_max(get_height(root.get_left()), get_height(root.get_right()))) + root.set_height( + 1 + my_max(get_height(root.get_left()), get_height(root.get_right())) + ) # Get the balance factor balance_factor = get_height(root.get_right()) - get_height(root.get_left()) From c7c39d0d6afbe9b57550487cb2c32ce568cb4511 Mon Sep 17 00:00:00 2001 From: Shaurya Bisht Date: Mon, 14 Oct 2024 19:00:30 +0530 Subject: [PATCH 3/4] Fixed insert function - added case 1 --- data_structures/trie/radix_tree.py | 15 ++++++++++----- greedy_methods/fractional_knapsack.py | 8 +++++--- matrix/matrix_class.py | 8 +++++--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index caf566a6c..34aa5f01c 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -62,12 +62,17 @@ class RadixNode: -- A (leaf) --- A (leaf) """ - # Case 1: If the word is the prefix of the node + # Case 1: If the word is empty, mark current node as leaf + if not word: + self.is_leaf = True + return + + # Case 2: If the word is the prefix of the node # Solution: We set the current node as leaf if self.prefix == word and not self.is_leaf: self.is_leaf = True - # Case 2: The node has no edges that have a prefix to the word + # Case 3: The node has no edges that have a prefix to the word # Solution: We create an edge from the current node to a new one # containing the word elif word[0] not in self.nodes: @@ -79,12 +84,12 @@ class RadixNode: word ) - # Case 3: The node prefix is equal to the matching + # Case 4: The node prefix is equal to the matching # Solution: We insert remaining word on the next node if remaining_prefix == "": - self.nodes[matching_string[0]].insert(remaining_word) + incoming_node.insert(remaining_word) - # Case 4: The word is greater equal to the matching + # Case 5: The word is greater equal to the matching # Solution: Create a node in between both nodes, change # prefixes and add the new node for the remaining word else: diff --git a/greedy_methods/fractional_knapsack.py b/greedy_methods/fractional_knapsack.py index d52b56f23..f7455a9c9 100644 --- a/greedy_methods/fractional_knapsack.py +++ b/greedy_methods/fractional_knapsack.py @@ -39,9 +39,11 @@ def frac_knapsack(vl, wt, w, n): return ( 0 if k == 0 - else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) - if k != n - else sum(vl[:k]) + else ( + sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k]) + if k != n + else sum(vl[:k]) + ) ) diff --git a/matrix/matrix_class.py b/matrix/matrix_class.py index a5940a38e..230eb9500 100644 --- a/matrix/matrix_class.py +++ b/matrix/matrix_class.py @@ -204,9 +204,11 @@ class Matrix: return Matrix( [ [ - self.minors().rows[row][column] - if (row + column) % 2 == 0 - else self.minors().rows[row][column] * -1 + ( + self.minors().rows[row][column] + if (row + column) % 2 == 0 + else self.minors().rows[row][column] * -1 + ) for column in range(self.minors().num_columns) ] for row in range(self.minors().num_rows) From 00fdf1bedde6672763ec44877b7373a320ab31a8 Mon Sep 17 00:00:00 2001 From: Shaurya Bisht <87357655+ShauryaDusht@users.noreply.github.com> Date: Mon, 14 Oct 2024 13:58:21 +0000 Subject: [PATCH 4/4] Added new case in radix_tree.py --- data_structures/trie/radix_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index 34aa5f01c..23a553d1f 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -231,4 +231,4 @@ def main() -> None: if __name__ == "__main__": - main() + main() \ No newline at end of file