From 275dcfdfd0292b44dac2c4c4ac8707d4db986149 Mon Sep 17 00:00:00 2001 From: Roushan Singh Date: Thu, 25 Apr 2024 18:43:31 +0530 Subject: [PATCH 01/16] 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/16] [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/16] 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/16] [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/16] 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/16] [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/16] [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/16] 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/16] [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/16] [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/16] 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/16] [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/16] 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() From 4da3e1c5d2045717c200f0b0b9fc348c87d8af78 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 10 Sep 2026 13:11:59 +0200 Subject: [PATCH 14/16] Improve comments in radix_tree.py Refactor comments for clarity and correctness in radix_tree.py. --- data_structures/trie/radix_tree.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index 7bae3c154bcf..cd49dde9c1be 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -1,10 +1,9 @@ -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 +trie (prefix tree) in which each node that is the only child is merged with its parent [https://en.wikipedia.org/wiki/Radix_tree] """ +import unittest class RadixNode: @@ -64,7 +63,7 @@ def insert(self, word: str) -> None: -- A (leaf) --- A (leaf) """ - ## Handle the Case where word is empty by using an if branch + ## Handle the Case where the word is empty by using an if branch if word == "": self.is_leaf = True return @@ -87,11 +86,11 @@ def insert(self, word: str) -> None: ) # Case 3: The node prefix is equal to the matching - # Solution: We insert remaining word on the next node + # Solution: We insert the remaining word on the next node if remaining_prefix == "": self.nodes[matching_string[0]].insert(remaining_word) - # Case 4: The word is greater equal to the matching + # Case 4: The word is greater than or equal to the matching # Solution: Create a node in between both nodes, change # prefixes and add the new node for the remaining word else: @@ -107,7 +106,7 @@ def insert(self, word: str) -> None: self.nodes[matching_string[0]].insert(remaining_word) def find(self, word: str) -> bool: - """Returns if the word is on the tree + """Returns whether the word is on the tree Args: word (str): word to check @@ -198,11 +197,6 @@ 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 -## and call it from main() - - class TestRadixNode(unittest.TestCase): def test_trie(self) -> None: words = "banana bananas bandana band apple all beast".split() @@ -220,7 +214,7 @@ def test_trie(self) -> None: def test_trie_2(self) -> None: """ - now add a new test case which inserts + Now add a new test case that inserts foobbb, fooaaa, foo in the given order and checks for different assertions """ @@ -236,4 +230,7 @@ def test_trie_2(self) -> None: if __name__ == "__main__": + import doctest + + doctest.testmod() unittest.main() From ff5b7fea1b14df39e7e737f88ad7c175a18231d8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 10 Sep 2026 11:12:13 +0000 Subject: [PATCH 15/16] [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 cd49dde9c1be..64c4ecd51131 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -3,6 +3,7 @@ trie (prefix tree) in which each node that is the only child is merged with its parent [https://en.wikipedia.org/wiki/Radix_tree] """ + import unittest From 4b2c6060cf6d3e317e21e55e078bbce5eb924641 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 10 Sep 2026 13:15:35 +0200 Subject: [PATCH 16/16] Add test function for RadixNode operations Added a test function for the RadixNode class to validate insertion, search, and deletion of words. --- data_structures/trie/radix_tree.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/data_structures/trie/radix_tree.py b/data_structures/trie/radix_tree.py index 64c4ecd51131..b227d774858e 100644 --- a/data_structures/trie/radix_tree.py +++ b/data_structures/trie/radix_tree.py @@ -198,6 +198,21 @@ def print_tree(self, height: int = 0) -> None: value.print_tree(height + 1) +def test_trie() -> None: + 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") + + class TestRadixNode(unittest.TestCase): def test_trie(self) -> None: words = "banana bananas bandana band apple all beast".split()