From f9ed074fd4fe875ac0f79930cc2bc089e9d11fc1 Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Fri, 14 Mar 2025 23:15:45 +0530 Subject: [PATCH 01/12] forked and added name email to authors --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 943bf804..8b5220bb 100644 --- a/AUTHORS +++ b/AUTHORS @@ -11,3 +11,4 @@ Pratik Goyal Jay Thorat Rajveer Singh Bharadwaj Kishan Ved +Arvinder Singh Dhoul \ No newline at end of file From 50804c432985ffa4783f6c5e4401d6fe6edf1487 Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Mon, 17 Mar 2025 06:26:53 +0530 Subject: [PATCH 02/12] added feature: fenwich tree --- pydatastructs/trees/fenwich_tree.py | 85 +++++++++++++ .../trees/tests/test_fenwich_tree.py | 112 ++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 pydatastructs/trees/fenwich_tree.py create mode 100644 pydatastructs/trees/tests/test_fenwich_tree.py diff --git a/pydatastructs/trees/fenwich_tree.py b/pydatastructs/trees/fenwich_tree.py new file mode 100644 index 00000000..35ba8740 --- /dev/null +++ b/pydatastructs/trees/fenwich_tree.py @@ -0,0 +1,85 @@ +class fenwich_tree: + """ + Implementation of Fenwich tree/Binary Indexed Tree + """ + + def __init__(self, size_or_array): + """ + Initializes the Fenwich Tree. + + Args: + size_or_array: size of array the tree will represent or array of values + """ + + if isinstance(size_or_array, int): + self.size = size_or_array + self.tree = [0] * (self.size + 1) + self.original_array = [0] * self.size + elif isinstance(size_or_array, list): + self.original_array = list(size_or_array) + self.size = len(self.original_array) + self.tree = [0] * (self.size + 1) + for i, val in enumerate(self.original_array): + self._update_tree(i, val) + else: + raise ValueError("size_or_array must be an integer or a list.") + + def _update_tree(self, index, delta): + """ + Internal helper to update the Fenwick Tree after a change in the original array. + """ + index += 1 # Fenwick Tree is 1-indexed + while index <= self.size: + self.tree[index] += delta + index += index & (-index) + + def update(self, index, value): + """ + Updates the value at the given index in the original array and the Fenwick Tree. + + Args: + index: The index to update (0-based). + value: The new value. + """ + if not (0 <= index < self.size): + raise IndexError("Index out of bounds") + delta = value - self.original_array[index] + self.original_array[index] = value + self._update_tree(index, delta) + + def prefix_sum(self, index): + """ + Calculates the prefix sum up to the given index (inclusive). + + Args: + index: The index up to which to calculate the sum (0-based). + + Returns: + The prefix sum. + """ + if not (0 <= index < self.size): + raise IndexError("Index out of bounds") + index += 1 # + sum_val = 0 + while index > 0: + sum_val += self.tree[index] + index -= index & (-index) + return sum_val + + def range_sum(self, start_index, end_index): + """ + Calculates the sum of elements within the given range (inclusive). + + Args: + start_index: The starting index of the range (0-based). + end_index: The ending index of the range (0-based). + + Returns: + The sum of elements in the range. + """ + if not (0 <= start_index <= end_index < self.size): + raise IndexError("Indices out of bounds") + if start_index == 0: + return self.prefix_sum(end_index) + else: + return self.prefix_sum(end_index) - self.prefix_sum(start_index - 1) \ No newline at end of file diff --git a/pydatastructs/trees/tests/test_fenwich_tree.py b/pydatastructs/trees/tests/test_fenwich_tree.py new file mode 100644 index 00000000..5bc7c26f --- /dev/null +++ b/pydatastructs/trees/tests/test_fenwich_tree.py @@ -0,0 +1,112 @@ +import unittest +from pydatastructs import fenwich_tree + +class TestFenwickTree(unittest.TestCase): + + def test_initialization_with_size(self): + ft = fenwich_tree(5) + self.assertEqual(ft.size, 5) + self.assertEqual(ft.tree, [0, 0, 0, 0, 0, 0]) + self.assertEqual(ft.original_array, [0, 0, 0, 0, 0]) + + def test_initialization_with_array(self): + arr = [1, 2, 3, 4, 5] + ft = fenwich_tree(arr) + self.assertEqual(ft.size, 5) + self.assertEqual(ft.original_array, arr) + # Manually calculate prefix sums and check the tree structure + expected_tree = [0, 1, 3, 3, 10, 5] + self.assertEqual(ft.tree, expected_tree) + + def test_initialization_with_empty_array(self): + arr = [] + ft = fenwich_tree(arr) + self.assertEqual(ft.size, 0) + self.assertEqual(ft.tree, [0]) + self.assertEqual(ft.original_array, []) + + def test_initialization_with_invalid_input(self): + with self.assertRaises(ValueError): + fenwich_tree("invalid") + + def test_update_single_element(self): + ft = fenwich_tree([1, 2, 3, 4, 5]) + ft.update(1, 10) + self.assertEqual(ft.original_array, [1, 10, 3, 4, 5]) + expected_tree = [0, 1, 11, 3, 18, 5] + self.assertEqual(ft.tree, expected_tree) + + def test_update_out_of_bounds(self): + ft = fenwich_tree(5) + with self.assertRaises(IndexError): + ft.update(5, 10) + with self.assertRaises(IndexError): + ft.update(-1, 10) + + def test_prefix_sum_positive_indices(self): + arr = [1, 2, 3, 4, 5] + ft = fenwich_tree(arr) + self.assertEqual(ft.prefix_sum(0), 1) + self.assertEqual(ft.prefix_sum(1), 3) + self.assertEqual(ft.prefix_sum(2), 6) + self.assertEqual(ft.prefix_sum(3), 10) + self.assertEqual(ft.prefix_sum(4), 15) + + def test_prefix_sum_out_of_bounds(self): + ft = fenwich_tree(5) + with self.assertRaises(IndexError): + ft.prefix_sum(5) + with self.assertRaises(IndexError): + ft.prefix_sum(-1) + + def test_prefix_sum_empty_array(self): + ft = fenwich_tree([]) + with self.assertRaises(IndexError): + ft.prefix_sum(0) # Should raise IndexError as size is 0 + + def test_range_sum_valid_range(self): + arr = [1, 2, 3, 4, 5] + ft = fenwich_tree(arr) + self.assertEqual(ft.range_sum(0, 0), 1) + self.assertEqual(ft.range_sum(0, 1), 3) + self.assertEqual(ft.range_sum(1, 3), 2 + 3 + 4) + self.assertEqual(ft.range_sum(2, 4), 3 + 4 + 5) + self.assertEqual(ft.range_sum(0, 4), 1 + 2 + 3 + 4 + 5) + + def test_range_sum_out_of_bounds(self): + ft = fenwich_tree(5) + with self.assertRaises(IndexError): + ft.range_sum(0, 5) + with self.assertRaises(IndexError): + ft.range_sum(-1, 2) + with self.assertRaises(IndexError): + ft.range_sum(1, 5) + with self.assertRaises(IndexError): + ft.range_sum(-1, -1) + + def test_range_sum_invalid_range(self): + ft = fenwich_tree(5) + with self.assertRaises(IndexError): + ft.range_sum(3, 1) + + def test_range_sum_single_element(self): + arr = [10, 20, 30] + ft = fenwich_tree(arr) + self.assertEqual(ft.range_sum(0, 0), 10) + self.assertEqual(ft.range_sum(1, 1), 20) + self.assertEqual(ft.range_sum(2, 2), 30) + + def test_range_sum_entire_array(self): + arr = [1, 2, 3, 4, 5] + ft = fenwich_tree(arr) + self.assertEqual(ft.range_sum(0, ft.size - 1), 15) + + def test_update_and_query_sequence(self): + ft = fenwich_tree([2, 5, 1, 8, 3]) + self.assertEqual(ft.prefix_sum(3), 2 + 5 + 1 + 8) # 16 + ft.update(1, 10) + self.assertEqual(ft.prefix_sum(3), 2 + 10 + 1 + 8) # 21 + self.assertEqual(ft.range_sum(0, 2), 2 + 10 + 1) # 13 + ft.update(4, 0) + self.assertEqual(ft.prefix_sum(4), 2 + 10 + 1 + 8 + 0) # 21 + self.assertEqual(ft.range_sum(3, 4), 8 + 0) # 8 From c3d3b74e9a2afb155b45000a405d978e339c63a3 Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Mon, 17 Mar 2025 07:03:47 +0530 Subject: [PATCH 03/12] modified test file --- pydatastructs/trees/fenwich_tree.py | 4 ++++ pydatastructs/trees/tests/test_fenwich_tree.py | 2 +- pydatastructs/utils/misc_util.py | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pydatastructs/trees/fenwich_tree.py b/pydatastructs/trees/fenwich_tree.py index 35ba8740..3a1e0844 100644 --- a/pydatastructs/trees/fenwich_tree.py +++ b/pydatastructs/trees/fenwich_tree.py @@ -1,3 +1,7 @@ +__all__ = [ + 'fenwich_tree' +] + class fenwich_tree: """ Implementation of Fenwich tree/Binary Indexed Tree diff --git a/pydatastructs/trees/tests/test_fenwich_tree.py b/pydatastructs/trees/tests/test_fenwich_tree.py index 5bc7c26f..9c73a5c2 100644 --- a/pydatastructs/trees/tests/test_fenwich_tree.py +++ b/pydatastructs/trees/tests/test_fenwich_tree.py @@ -1,5 +1,5 @@ import unittest -from pydatastructs import fenwich_tree +from pydatastructs.trees.fenwich_tree import fenwich_tree class TestFenwickTree(unittest.TestCase): diff --git a/pydatastructs/utils/misc_util.py b/pydatastructs/utils/misc_util.py index 4c7ca7fa..866fce94 100644 --- a/pydatastructs/utils/misc_util.py +++ b/pydatastructs/utils/misc_util.py @@ -623,3 +623,5 @@ def summation(x_y): return x if y is None else y return x + y + + From 4fa620015c953c27f2f206a5ac5a7b7ce638af5a Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Wed, 19 Mar 2025 07:18:21 +0530 Subject: [PATCH 04/12] added trie with additional features --- pydatastructs/trees/tests/test_trie.py | 111 ++++++++++++++++++++++ pydatastructs/trees/trie.py | 122 +++++++++++++++++++++++++ 2 files changed, 233 insertions(+) create mode 100644 pydatastructs/trees/tests/test_trie.py create mode 100644 pydatastructs/trees/trie.py diff --git a/pydatastructs/trees/tests/test_trie.py b/pydatastructs/trees/tests/test_trie.py new file mode 100644 index 00000000..edd73026 --- /dev/null +++ b/pydatastructs/trees/tests/test_trie.py @@ -0,0 +1,111 @@ +import pytest +from trie import Trie + +def test_trie_insert_search(): + trie = Trie() + trie.insert("apple") + assert trie.search("apple") + assert not trie.search("app") + trie.insert("app") + assert trie.search("app") + +def test_trie_starts_with(): + trie = Trie() + trie.insert("apple") + assert trie.starts_with("app") + assert trie.starts_with("a") + assert not trie.starts_with("b") + assert not trie.starts_with("applxyz") + +def test_trie_empty(): + trie = Trie() + assert not trie.search("apple") + assert not trie.starts_with("app") + +def test_trie_multiple_words(): + trie = Trie() + trie.insert("apple") + trie.insert("application") + trie.insert("banana") + assert trie.search("apple") + assert trie.search("application") + assert trie.search("banana") + assert not trie.search("app") + assert trie.starts_with("app") + assert trie.starts_with("ban") + assert not trie.starts_with("aplx") + +def test_trie_case_sensitive(): + trie = Trie() + trie.insert("Apple") + assert trie.search("Apple") + assert not trie.search("apple") + +def test_count_words(): + trie = Trie() + assert trie.count_words() == 0 + trie.insert("apple") + assert trie.count_words() == 1 + trie.insert("app") + assert trie.count_words() == 2 + trie.insert("apple") + assert trie.count_words() == 2 + +def test_longest_common_prefix(): + trie = Trie() + assert trie.longest_common_prefix() == "" + trie.insert("apple") + assert trie.longest_common_prefix() == "apple" + trie.insert("application") + assert trie.longest_common_prefix() == "appl" + trie.insert("banana") + assert trie.longest_common_prefix() == "" + +def test_autocomplete(): + trie = Trie() + trie.insert("apple") + trie.insert("application") + trie.insert("app") + assert trie.autocomplete("app") == ["app", "apple", "application"] + assert trie.autocomplete("appl") == ["apple", "application"] + assert trie.autocomplete("b") == [] + +def test_bulk_insert(): + trie = Trie() + trie.bulk_insert(["apple", "banana", "orange"]) + assert trie.search("apple") + assert trie.search("banana") + assert trie.search("orange") + assert trie.count_words() == 3 + +def test_clear(): + trie = Trie() + trie.insert("apple") + trie.clear() + assert trie.is_empty() + assert trie.count_words() == 0 + assert not trie.search("apple") + +def test_is_empty(): + trie = Trie() + assert trie.is_empty() + trie.insert("apple") + assert not trie.is_empty() + trie.clear() + assert trie.is_empty() + +def test_find_all_words(): + trie = Trie() + trie.bulk_insert(["apple", "banana", "orange"]) + assert sorted(trie.find_all_words()) == sorted(["apple", "banana", "orange"]) + trie.clear() + assert trie.find_all_words() == [] + + +def test_longest_word(): + trie = Trie() + assert trie.longest_word() is None + trie.bulk_insert(["apple", "banana", "application"]) + assert trie.longest_word() == "application" + trie.insert("a") + assert trie.longest_word() == "application" \ No newline at end of file diff --git a/pydatastructs/trees/trie.py b/pydatastructs/trees/trie.py new file mode 100644 index 00000000..5087aaf1 --- /dev/null +++ b/pydatastructs/trees/trie.py @@ -0,0 +1,122 @@ +# trie.py + +class TrieNode: + """Represents a node in the Trie data structure.""" + def __init__(self): + """Initializes a TrieNode with empty children and is_end_of_word set to False.""" + self.children = {} + self.is_end_of_word = False + self.word = None + +class Trie: + """Represents the Trie (prefix tree) data structure.""" + def __init__(self): + """Initializes an empty Trie with a root TrieNode.""" + self.root = TrieNode() + self.word_count = 0 + + def insert(self, word): + """Inserts a word into the Trie.""" + node = self.root + for char in word: + if char not in node.children: + node.children[char] = TrieNode() + node = node.children[char] + if not node.is_end_of_word: + node.is_end_of_word = True + node.word = word + self.word_count += 1 + + def search(self, word): + """Searches for a word in the Trie.""" + node = self.root + for char in word: + if char not in node.children: + return False + node = node.children[char] + return node.is_end_of_word + + def starts_with(self, prefix): + """Checks if any word in the Trie starts with the given prefix.""" + node = self.root + for char in prefix: + if char not in node.children: + return False + node = node.children[char] + return True + + def count_words(self): + """Returns the total number of words stored in the Trie.""" + return self.word_count + + def longest_common_prefix(self): + """Finds the longest common prefix among all words in the Trie.""" + node = self.root + prefix = "" + while len(node.children) == 1 and not node.is_end_of_word: + char = next(iter(node.children)) + prefix += char + node = node.children[char] + return prefix + + def autocomplete(self, prefix): + """Provides a list of words that match a given prefix.""" + node = self.root + for char in prefix: + if char not in node.children: + return [] + node = node.children[char] + + def collect_words(current_node, current_prefix): + words = [] + if current_node.is_end_of_word: + words.append(current_prefix) + for char, child_node in current_node.children.items(): + words.extend(collect_words(child_node, current_prefix + char)) + return words + + return collect_words(node, prefix) + + def bulk_insert(self, words): + """Inserts multiple words into the Trie in a single operation.""" + for word in words: + self.insert(word) + + def clear(self): + """Removes all words from the Trie, resetting it.""" + self.root = TrieNode() + self.word_count = 0 + + def is_empty(self): + """Returns True if the Trie is empty, otherwise False.""" + return self.word_count == 0 + + def find_all_words(self): + """Retrieves all words currently stored in the Trie.""" + def collect_words(current_node): + words = [] + if current_node.is_end_of_word: + words.append(current_node.word) + for child_node in current_node.children.values(): + words.extend(collect_words(child_node)) + return words + + return collect_words(self.root) + + def shortest_unique_prefix(self, word): + """Determines the shortest unique prefix for a given word.""" + node = self.root + prefix = "" + for char in word: + prefix += char + if len(node.children[char].children) <= 1 and node.children[char].is_end_of_word == False : + return prefix + node = node.children[char] + return word + + def longest_word(self): + """Finds and returns the longest word in the Trie.""" + all_words = self.find_all_words() + if not all_words: + return None + return max(all_words, key=len) \ No newline at end of file From d8c6711d385fc70bd1475496cc81cc1e8346aaf8 Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Wed, 19 Mar 2025 07:26:01 +0530 Subject: [PATCH 05/12] resolved ModuleNotFound error --- pydatastructs/trees/tests/test_trie.py | 2 +- pydatastructs/trees/trie.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pydatastructs/trees/tests/test_trie.py b/pydatastructs/trees/tests/test_trie.py index edd73026..d79df3c7 100644 --- a/pydatastructs/trees/tests/test_trie.py +++ b/pydatastructs/trees/tests/test_trie.py @@ -1,5 +1,5 @@ import pytest -from trie import Trie +from pydatastructs.trees.trie import Trie def test_trie_insert_search(): trie = Trie() diff --git a/pydatastructs/trees/trie.py b/pydatastructs/trees/trie.py index 5087aaf1..75398198 100644 --- a/pydatastructs/trees/trie.py +++ b/pydatastructs/trees/trie.py @@ -1,5 +1,3 @@ -# trie.py - class TrieNode: """Represents a node in the Trie data structure.""" def __init__(self): From 3adae51f9c7e9700a54b972d76b73f7dc356efc2 Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Wed, 19 Mar 2025 07:36:33 +0530 Subject: [PATCH 06/12] modified fenwich tree file --- pydatastructs/trees/fenwich_tree.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pydatastructs/trees/fenwich_tree.py b/pydatastructs/trees/fenwich_tree.py index 3a1e0844..9378aa60 100644 --- a/pydatastructs/trees/fenwich_tree.py +++ b/pydatastructs/trees/fenwich_tree.py @@ -86,4 +86,5 @@ def range_sum(self, start_index, end_index): if start_index == 0: return self.prefix_sum(end_index) else: - return self.prefix_sum(end_index) - self.prefix_sum(start_index - 1) \ No newline at end of file + return self.prefix_sum(end_index) - self.prefix_sum(start_index - 1) + \ No newline at end of file From 18e60cbb93eb0b782b321b63f842bddbf7997206 Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Wed, 19 Mar 2025 07:46:38 +0530 Subject: [PATCH 07/12] new line --- pydatastructs/trees/fenwich_tree.py | 5 +++-- pydatastructs/trees/trie.py | 9 +++++---- pydatastructs/utils/misc_util.py | 2 -- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pydatastructs/trees/fenwich_tree.py b/pydatastructs/trees/fenwich_tree.py index 9378aa60..a0ec6d26 100644 --- a/pydatastructs/trees/fenwich_tree.py +++ b/pydatastructs/trees/fenwich_tree.py @@ -83,8 +83,9 @@ def range_sum(self, start_index, end_index): """ if not (0 <= start_index <= end_index < self.size): raise IndexError("Indices out of bounds") - if start_index == 0: + if start_index is 0: return self.prefix_sum(end_index) else: return self.prefix_sum(end_index) - self.prefix_sum(start_index - 1) - \ No newline at end of file + + diff --git a/pydatastructs/trees/trie.py b/pydatastructs/trees/trie.py index 75398198..ac5a1021 100644 --- a/pydatastructs/trees/trie.py +++ b/pydatastructs/trees/trie.py @@ -51,7 +51,7 @@ def longest_common_prefix(self): """Finds the longest common prefix among all words in the Trie.""" node = self.root prefix = "" - while len(node.children) == 1 and not node.is_end_of_word: + while len(node.children) is 1 and not node.is_end_of_word: char = next(iter(node.children)) prefix += char node = node.children[char] @@ -87,7 +87,7 @@ def clear(self): def is_empty(self): """Returns True if the Trie is empty, otherwise False.""" - return self.word_count == 0 + return self.word_count is 0 def find_all_words(self): """Retrieves all words currently stored in the Trie.""" @@ -107,7 +107,7 @@ def shortest_unique_prefix(self, word): prefix = "" for char in word: prefix += char - if len(node.children[char].children) <= 1 and node.children[char].is_end_of_word == False : + if len(node.children[char].children) <= 1 and node.children[char].is_end_of_word is False : return prefix node = node.children[char] return word @@ -117,4 +117,5 @@ def longest_word(self): all_words = self.find_all_words() if not all_words: return None - return max(all_words, key=len) \ No newline at end of file + return max(all_words, key=len) + \ No newline at end of file diff --git a/pydatastructs/utils/misc_util.py b/pydatastructs/utils/misc_util.py index 866fce94..4c7ca7fa 100644 --- a/pydatastructs/utils/misc_util.py +++ b/pydatastructs/utils/misc_util.py @@ -623,5 +623,3 @@ def summation(x_y): return x if y is None else y return x + y - - From c68362d92eb852fd69487f1f3734408d5976adf5 Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Wed, 19 Mar 2025 07:54:04 +0530 Subject: [PATCH 08/12] new line --- pydatastructs/trees/fenwich_tree.py | 3 +-- pydatastructs/trees/tests/test_trie.py | 3 ++- pydatastructs/trees/trie.py | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pydatastructs/trees/fenwich_tree.py b/pydatastructs/trees/fenwich_tree.py index a0ec6d26..30c766a7 100644 --- a/pydatastructs/trees/fenwich_tree.py +++ b/pydatastructs/trees/fenwich_tree.py @@ -87,5 +87,4 @@ def range_sum(self, start_index, end_index): return self.prefix_sum(end_index) else: return self.prefix_sum(end_index) - self.prefix_sum(start_index - 1) - - + \ No newline at end of file diff --git a/pydatastructs/trees/tests/test_trie.py b/pydatastructs/trees/tests/test_trie.py index d79df3c7..8b53f364 100644 --- a/pydatastructs/trees/tests/test_trie.py +++ b/pydatastructs/trees/tests/test_trie.py @@ -108,4 +108,5 @@ def test_longest_word(): trie.bulk_insert(["apple", "banana", "application"]) assert trie.longest_word() == "application" trie.insert("a") - assert trie.longest_word() == "application" \ No newline at end of file + assert trie.longest_word() == "application" + diff --git a/pydatastructs/trees/trie.py b/pydatastructs/trees/trie.py index ac5a1021..a096dad1 100644 --- a/pydatastructs/trees/trie.py +++ b/pydatastructs/trees/trie.py @@ -118,4 +118,5 @@ def longest_word(self): if not all_words: return None return max(all_words, key=len) + \ No newline at end of file From 418496cd07e764401d8e0166ce5e5d6f79f7ab0f Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Wed, 19 Mar 2025 08:00:59 +0530 Subject: [PATCH 09/12] new line --- pydatastructs/trees/fenwich_tree.py | 2 +- pydatastructs/trees/tests/test_trie.py | 1 - pydatastructs/trees/trie.py | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pydatastructs/trees/fenwich_tree.py b/pydatastructs/trees/fenwich_tree.py index 30c766a7..f473e092 100644 --- a/pydatastructs/trees/fenwich_tree.py +++ b/pydatastructs/trees/fenwich_tree.py @@ -87,4 +87,4 @@ def range_sum(self, start_index, end_index): return self.prefix_sum(end_index) else: return self.prefix_sum(end_index) - self.prefix_sum(start_index - 1) - \ No newline at end of file + diff --git a/pydatastructs/trees/tests/test_trie.py b/pydatastructs/trees/tests/test_trie.py index 8b53f364..9cdd2ce4 100644 --- a/pydatastructs/trees/tests/test_trie.py +++ b/pydatastructs/trees/tests/test_trie.py @@ -109,4 +109,3 @@ def test_longest_word(): assert trie.longest_word() == "application" trie.insert("a") assert trie.longest_word() == "application" - diff --git a/pydatastructs/trees/trie.py b/pydatastructs/trees/trie.py index a096dad1..e33a75c2 100644 --- a/pydatastructs/trees/trie.py +++ b/pydatastructs/trees/trie.py @@ -119,4 +119,4 @@ def longest_word(self): return None return max(all_words, key=len) - \ No newline at end of file + From 9a9cca1ebaa1b833772a866507be854d305fef5f Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Wed, 19 Mar 2025 08:12:53 +0530 Subject: [PATCH 10/12] mitigation --- pydatastructs/trees/fenwich_tree.py | 1 - pydatastructs/trees/trie.py | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/pydatastructs/trees/fenwich_tree.py b/pydatastructs/trees/fenwich_tree.py index f473e092..0445516d 100644 --- a/pydatastructs/trees/fenwich_tree.py +++ b/pydatastructs/trees/fenwich_tree.py @@ -87,4 +87,3 @@ def range_sum(self, start_index, end_index): return self.prefix_sum(end_index) else: return self.prefix_sum(end_index) - self.prefix_sum(start_index - 1) - diff --git a/pydatastructs/trees/trie.py b/pydatastructs/trees/trie.py index e33a75c2..411dc3ad 100644 --- a/pydatastructs/trees/trie.py +++ b/pydatastructs/trees/trie.py @@ -51,7 +51,7 @@ def longest_common_prefix(self): """Finds the longest common prefix among all words in the Trie.""" node = self.root prefix = "" - while len(node.children) is 1 and not node.is_end_of_word: + while len(node.children) == 1 and not node.is_end_of_word: char = next(iter(node.children)) prefix += char node = node.children[char] @@ -87,7 +87,7 @@ def clear(self): def is_empty(self): """Returns True if the Trie is empty, otherwise False.""" - return self.word_count is 0 + return self.word_count == 0 def find_all_words(self): """Retrieves all words currently stored in the Trie.""" @@ -118,5 +118,3 @@ def longest_word(self): if not all_words: return None return max(all_words, key=len) - - From 91657f9238f28e6b1d9878b1c60f52eaa99219bd Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Wed, 19 Mar 2025 08:24:26 +0530 Subject: [PATCH 11/12] white spaces removal --- pydatastructs/trees/fenwich_tree.py | 10 +++++----- pydatastructs/trees/trie.py | 28 ++++++++++++++-------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/pydatastructs/trees/fenwich_tree.py b/pydatastructs/trees/fenwich_tree.py index 0445516d..d21584bf 100644 --- a/pydatastructs/trees/fenwich_tree.py +++ b/pydatastructs/trees/fenwich_tree.py @@ -18,7 +18,7 @@ def __init__(self, size_or_array): if isinstance(size_or_array, int): self.size = size_or_array self.tree = [0] * (self.size + 1) - self.original_array = [0] * self.size + self.original_array = [0] * self.size elif isinstance(size_or_array, list): self.original_array = list(size_or_array) self.size = len(self.original_array) @@ -27,7 +27,7 @@ def __init__(self, size_or_array): self._update_tree(i, val) else: raise ValueError("size_or_array must be an integer or a list.") - + def _update_tree(self, index, delta): """ Internal helper to update the Fenwick Tree after a change in the original array. @@ -67,9 +67,9 @@ def prefix_sum(self, index): sum_val = 0 while index > 0: sum_val += self.tree[index] - index -= index & (-index) + index -= index & (-index) return sum_val - + def range_sum(self, start_index, end_index): """ Calculates the sum of elements within the given range (inclusive). @@ -83,7 +83,7 @@ def range_sum(self, start_index, end_index): """ if not (0 <= start_index <= end_index < self.size): raise IndexError("Indices out of bounds") - if start_index is 0: + if start_index == 0: return self.prefix_sum(end_index) else: return self.prefix_sum(end_index) - self.prefix_sum(start_index - 1) diff --git a/pydatastructs/trees/trie.py b/pydatastructs/trees/trie.py index 411dc3ad..6a26b511 100644 --- a/pydatastructs/trees/trie.py +++ b/pydatastructs/trees/trie.py @@ -2,47 +2,47 @@ class TrieNode: """Represents a node in the Trie data structure.""" def __init__(self): """Initializes a TrieNode with empty children and is_end_of_word set to False.""" - self.children = {} - self.is_end_of_word = False - self.word = None + self.children = {} + self.is_end_of_word = False + self.word = None class Trie: """Represents the Trie (prefix tree) data structure.""" def __init__(self): """Initializes an empty Trie with a root TrieNode.""" - self.root = TrieNode() + self.root = TrieNode() self.word_count = 0 def insert(self, word): """Inserts a word into the Trie.""" - node = self.root + node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() - node = node.children[char] + node = node.children[char] if not node.is_end_of_word: - node.is_end_of_word = True + node.is_end_of_word = True node.word = word self.word_count += 1 def search(self, word): """Searches for a word in the Trie.""" - node = self.root + node = self.root for char in word: if char not in node.children: return False - node = node.children[char] - return node.is_end_of_word + node = node.children[char] + return node.is_end_of_word def starts_with(self, prefix): """Checks if any word in the Trie starts with the given prefix.""" - node = self.root + node = self.root for char in prefix: if char not in node.children: return False - node = node.children[char] - return True - + node = node.children[char] + return True + def count_words(self): """Returns the total number of words stored in the Trie.""" return self.word_count From f37f0da04922794b2e04cf12d2b728877c0ba729 Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Wed, 19 Mar 2025 08:31:44 +0530 Subject: [PATCH 12/12] modified trie --- pydatastructs/trees/trie.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/pydatastructs/trees/trie.py b/pydatastructs/trees/trie.py index 6a26b511..7ccc26e1 100644 --- a/pydatastructs/trees/trie.py +++ b/pydatastructs/trees/trie.py @@ -101,17 +101,6 @@ def collect_words(current_node): return collect_words(self.root) - def shortest_unique_prefix(self, word): - """Determines the shortest unique prefix for a given word.""" - node = self.root - prefix = "" - for char in word: - prefix += char - if len(node.children[char].children) <= 1 and node.children[char].is_end_of_word is False : - return prefix - node = node.children[char] - return word - def longest_word(self): """Finds and returns the longest word in the Trie.""" all_words = self.find_all_words()