From 275dcfdfd0292b44dac2c4c4ac8707d4db986149 Mon Sep 17 00:00:00 2001 From: Roushan Singh Date: Thu, 25 Apr 2024 18:43:31 +0530 Subject: [PATCH 01/13] Fixes The Bug In Radix Tree. Issue #11316 --- data_structures/trie/radix_tree.py | 76 ++++++++++++++++-------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index caf566a6ce30..7a14674df8b3 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -3,7 +3,7 @@ trie (prefix tree) in whicheach node that is the only child is merged with its parent [https://en.wikipedia.org/wiki/Radix_tree] """ - +import unittest class RadixNode: def __init__(self, prefix: str = "", is_leaf: bool = False) -> None: @@ -62,6 +62,11 @@ def insert(self, word: str) -> None: -- A (leaf) --- A (leaf) """ + ## Handle the Case where word is empty by using an if branch + if word == "": + self.is_leaf = True + return + # Case 1: 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: @@ -190,40 +195,39 @@ def print_tree(self, height: int = 0) -> None: for value in self.nodes.values(): value.print_tree(height + 1) - -def test_trie() -> bool: - words = "banana bananas bandana band apple all beast".split() - root = RadixNode() - root.insert_many(words) - - assert all(root.find(word) for word in words) - assert not root.find("bandanas") - assert not root.find("apps") - root.delete("all") - assert not root.find("all") - root.delete("banana") - assert not root.find("banana") - assert root.find("bananas") - - return True - - -def pytests() -> None: - assert test_trie() - - -def main() -> None: - """ - >>> pytests() - """ - root = RadixNode() - words = "banana bananas bandanas bandana band apple all beast".split() - root.insert_many(words) - - print("Words:", words) - print("Tree:") - root.print_tree() - +## write unit test for the code using unittest library with logic similar to test_trie() function +## and call it from main() + +class TestRadixNode(unittest.TestCase): + def test_trie(self) -> None: + words = "banana bananas bandana band apple all beast".split() + root = RadixNode() + root.insert_many(words) + + self.assertTrue(all(root.find(word) for word in words)) + self.assertFalse(root.find("bandanas")) + self.assertFalse(root.find("apps")) + root.delete("all") + self.assertFalse(root.find("all")) + root.delete("banana") + self.assertFalse(root.find("banana")) + self.assertTrue(root.find("bananas")) + + + def test_trie_2(self) -> None: + ''' + now add a new test case which inserts + foobbb, fooaaa, foo in the given order and checks for different assertions + ''' + words = "foobbb fooaaa foo".split() + root = RadixNode() + root.insert_many(words) + + self.assertTrue(all(root.find(word) for word in words)) + root.delete("foo") + self.assertFalse(root.find("foo")) + self.assertTrue(root.find("foobbb")) + self.assertTrue(root.find("fooaaa")) if __name__ == "__main__": - main() + unittest.main() From 87aec39494623f29ff42168169d47fa49dc8f28c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Apr 2024 13:25:28 +0000 Subject: [PATCH 02/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/trie/radix_tree.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index 7a14674df8b3..c610392ad387 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -3,8 +3,10 @@ trie (prefix tree) in whicheach node that is the only child is merged with its parent [https://en.wikipedia.org/wiki/Radix_tree] """ + import unittest + class RadixNode: def __init__(self, prefix: str = "", is_leaf: bool = False) -> None: # Mapping from the first character of the prefix of the node @@ -195,9 +197,11 @@ def print_tree(self, height: int = 0) -> None: for value in self.nodes.values(): value.print_tree(height + 1) + ## write unit test for the code using unittest library with logic similar to test_trie() function ## and call it from main() + class TestRadixNode(unittest.TestCase): def test_trie(self) -> None: words = "banana bananas bandana band apple all beast".split() @@ -212,13 +216,12 @@ def test_trie(self) -> None: root.delete("banana") self.assertFalse(root.find("banana")) self.assertTrue(root.find("bananas")) - def test_trie_2(self) -> None: - ''' - now add a new test case which inserts + """ + now add a new test case which inserts foobbb, fooaaa, foo in the given order and checks for different assertions - ''' + """ words = "foobbb fooaaa foo".split() root = RadixNode() root.insert_many(words) @@ -229,5 +232,6 @@ def test_trie_2(self) -> None: self.assertTrue(root.find("foobbb")) self.assertTrue(root.find("fooaaa")) + if __name__ == "__main__": unittest.main() From 394802af57f07bfcca32b1a5b012c29e5f031ebd Mon Sep 17 00:00:00 2001 From: Roushan Singh Date: Thu, 25 Apr 2024 19:25:37 +0530 Subject: [PATCH 03/13] Added Ruff Recommendations Based on ruff Recommendations - Removed WhiteSpaces - Moved the Import to beginning - Replaced assertTrue with regular assert --- data_structures/trie/radix_tree.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index 7a14674df8b3..4fd39162a31f 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -1,9 +1,10 @@ +import unittest + """ A Radix Tree is a data structure that represents a space-optimized trie (prefix tree) in whicheach node that is the only child is merged with its parent [https://en.wikipedia.org/wiki/Radix_tree] """ -import unittest class RadixNode: def __init__(self, prefix: str = "", is_leaf: bool = False) -> None: @@ -204,30 +205,29 @@ def test_trie(self) -> None: root = RadixNode() root.insert_many(words) - self.assertTrue(all(root.find(word) for word in words)) - self.assertFalse(root.find("bandanas")) - self.assertFalse(root.find("apps")) + assert all(root.find(word) for word in words) + assert not root.find("bandanas") + assert not root.find("apps") root.delete("all") - self.assertFalse(root.find("all")) + assert not root.find("all") root.delete("banana") - self.assertFalse(root.find("banana")) - self.assertTrue(root.find("bananas")) - + assert not root.find("banana") + assert root.find("bananas") def test_trie_2(self) -> None: ''' now add a new test case which inserts foobbb, fooaaa, foo in the given order and checks for different assertions - ''' + ''' words = "foobbb fooaaa foo".split() root = RadixNode() root.insert_many(words) - self.assertTrue(all(root.find(word) for word in words)) + assert all(root.find(word) for word in words) root.delete("foo") - self.assertFalse(root.find("foo")) - self.assertTrue(root.find("foobbb")) - self.assertTrue(root.find("fooaaa")) + assert not root.find("foo") + assert root.find("foobbb") + assert root.find("fooaaa") if __name__ == "__main__": unittest.main() From e58ca101d52bb2239ff6bd84c4f955c09ff27c7a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Apr 2024 14:07:12 +0000 Subject: [PATCH 04/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/trie/radix_tree.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index 9bb469047c22..e346cc383fc0 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -6,6 +6,7 @@ with its parent [https://en.wikipedia.org/wiki/Radix_tree] """ + class RadixNode: def __init__(self, prefix: str = "", is_leaf: bool = False) -> None: # Mapping from the first character of the prefix of the node @@ -217,10 +218,10 @@ def test_trie(self) -> None: assert root.find("bananas") def test_trie_2(self) -> None: - ''' - now add a new test case which inserts + """ + now add a new test case which inserts foobbb, fooaaa, foo in the given order and checks for different assertions - ''' + """ words = "foobbb fooaaa foo".split() root = RadixNode() root.insert_many(words) From becc449a5af38a3f608240f6880567fc58e90b48 Mon Sep 17 00:00:00 2001 From: Roushan Singh Date: Thu, 25 Apr 2024 19:44:17 +0530 Subject: [PATCH 05/13] Ruff Fixes : E501 Line too long --- data_structures/trie/radix_tree.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index 9bb469047c22..fef6ae230a8d 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -218,8 +218,9 @@ def test_trie(self) -> None: def test_trie_2(self) -> None: ''' - now add a new test case which inserts - foobbb, fooaaa, foo in the given order and checks for different assertions + now add a new test case which inserts + foobbb, fooaaa, foo in the given order and checks + for different assertions ''' words = "foobbb fooaaa foo".split() root = RadixNode() From 7b3b3914f684e7d5b1e87496f01f0d313c2cda03 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Apr 2024 14:16:18 +0000 Subject: [PATCH 06/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/trie/radix_tree.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index d6a02938eca8..fe0ceb85ce60 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -218,11 +218,11 @@ def test_trie(self) -> None: assert root.find("bananas") def test_trie_2(self) -> None: - ''' + """ now add a new test case which inserts foobbb, fooaaa, foo in the given order and checks for different assertions - ''' + """ words = "foobbb fooaaa foo".split() root = RadixNode() root.insert_many(words) From c9207e2c4e1ac7630ce487fe2f81635956a86400 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Apr 2024 13:25:28 +0000 Subject: [PATCH 07/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/trie/radix_tree.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index 4fd39162a31f..d24d254034cb 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -196,9 +196,11 @@ def print_tree(self, height: int = 0) -> None: for value in self.nodes.values(): value.print_tree(height + 1) + ## write unit test for the code using unittest library with logic similar to test_trie() function ## and call it from main() + class TestRadixNode(unittest.TestCase): def test_trie(self) -> None: words = "banana bananas bandana band apple all beast".split() @@ -215,10 +217,10 @@ def test_trie(self) -> None: assert root.find("bananas") def test_trie_2(self) -> None: - ''' - now add a new test case which inserts + """ + now add a new test case which inserts foobbb, fooaaa, foo in the given order and checks for different assertions - ''' + """ words = "foobbb fooaaa foo".split() root = RadixNode() root.insert_many(words) @@ -229,5 +231,6 @@ def test_trie_2(self) -> None: assert root.find("foobbb") assert root.find("fooaaa") + if __name__ == "__main__": unittest.main() From af76a5e179d6cf5b77b9996760cffd6f2b46a5a9 Mon Sep 17 00:00:00 2001 From: Roushan Singh Date: Thu, 25 Apr 2024 19:44:17 +0530 Subject: [PATCH 08/13] Ruff Fixes : E501 Line too long --- data_structures/trie/radix_tree.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index d24d254034cb..fef6ae230a8d 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -217,10 +217,11 @@ def test_trie(self) -> None: assert root.find("bananas") def test_trie_2(self) -> None: - """ + ''' now add a new test case which inserts - foobbb, fooaaa, foo in the given order and checks for different assertions - """ + foobbb, fooaaa, foo in the given order and checks + for different assertions + ''' words = "foobbb fooaaa foo".split() root = RadixNode() root.insert_many(words) From 3e4a386c4623b81dc1c1cfa8f7247b4ad9f36578 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Apr 2024 14:07:12 +0000 Subject: [PATCH 09/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/trie/radix_tree.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index fef6ae230a8d..d6a02938eca8 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -6,6 +6,7 @@ with its parent [https://en.wikipedia.org/wiki/Radix_tree] """ + class RadixNode: def __init__(self, prefix: str = "", is_leaf: bool = False) -> None: # Mapping from the first character of the prefix of the node From 298959e44c6f2ab749ec660ac55666f318863291 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Apr 2024 14:35:14 +0000 Subject: [PATCH 10/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/trie/radix_tree.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index fef6ae230a8d..fe0ceb85ce60 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -6,6 +6,7 @@ with its parent [https://en.wikipedia.org/wiki/Radix_tree] """ + class RadixNode: def __init__(self, prefix: str = "", is_leaf: bool = False) -> None: # Mapping from the first character of the prefix of the node @@ -217,11 +218,11 @@ def test_trie(self) -> None: assert root.find("bananas") def test_trie_2(self) -> None: - ''' + """ now add a new test case which inserts foobbb, fooaaa, foo in the given order and checks for different assertions - ''' + """ words = "foobbb fooaaa foo".split() root = RadixNode() root.insert_many(words) From 0d53bc7735b57b1a40644b07f96ddbbf7e445203 Mon Sep 17 00:00:00 2001 From: Roushan Singh Date: Thu, 25 Apr 2024 20:05:35 +0530 Subject: [PATCH 11/13] Fixes Ruff Error --- data_structures/trie/radix_tree.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index fef6ae230a8d..eebc8677e60a 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -197,7 +197,8 @@ def print_tree(self, height: int = 0) -> None: value.print_tree(height + 1) -## write unit test for the code using unittest library with logic similar to test_trie() function +## write unit test for the code using unittest library with +## logic similar to test_trie() function ## and call it from main() From 00f1dd426dffe2b1fe48e9e9d626850a618817cd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 25 Apr 2024 14:36:04 +0000 Subject: [PATCH 12/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- 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 04557b5151f7..7bae3c154bcf 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -198,7 +198,7 @@ def print_tree(self, height: int = 0) -> None: value.print_tree(height + 1) -## write unit test for the code using unittest library with +## write unit test for the code using unittest library with ## logic similar to test_trie() function ## and call it from main() From 66588e9b6b7f7989dce4a0ba906b524bdc44a9e9 Mon Sep 17 00:00:00 2001 From: Roushan Singh Date: Thu, 25 Apr 2024 20:06:45 +0530 Subject: [PATCH 13/13] Ruff All Fixes --- 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 04557b5151f7..7bae3c154bcf 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -198,7 +198,7 @@ def print_tree(self, height: int = 0) -> None: value.print_tree(height + 1) -## write unit test for the code using unittest library with +## write unit test for the code using unittest library with ## logic similar to test_trie() function ## and call it from main()