diff --git a/README.md b/README.md index c44c176..38a5a82 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # Data Structures -This project contains a variety of data structures implemented in python with a set of tests that achieve a near 100% code coverage. \ No newline at end of file +This project contains a variety of data structures implemented in python with a set of tests that achieves nearly 100% code coverage. Data structures implemented in this repo include: Linked list, doubly linked list, min heap, max heap, deque, binary search tree, binary indexed tree, segment tree, disjoint sets (union find), trie, LRU cache, and LFU cache. \ No newline at end of file diff --git a/ds/binary_indexed_tree.py b/ds/binary_indexed_tree.py index a1faefd..590f126 100644 --- a/ds/binary_indexed_tree.py +++ b/ds/binary_indexed_tree.py @@ -61,7 +61,7 @@ def update(self, idx: int, val: Union[int, float]) -> None: self.binary_idx_tree[idx - 1] += net_chg # Apply the net change to this tree node idx = idx + (idx & -idx) # Get the next parent range index - def _range_query(self, end: int) -> Union[float, int]: + def _prefix_sum(self, end: int) -> Union[float, int]: """ Internal helper function for performing range queries. Computes the sum of all elements up through index end. This method is used by range_query to compute the sum of elements start:end by taking the @@ -109,9 +109,9 @@ def range_query(self, start: int, end: int) -> Union[float, int]: if start == end: # Handle special edge case when start == end, return the entry of the original arr return self.arr[end] - ans = self._range_query(end) # Compute the sum up through the end index + ans = self._prefix_sum(end) # Compute the sum up through the end index if start > 0: # If the start is above index 0, then subtract the sum of elements through (start - 1) - ans -= self._range_query(start - 1) # so that the result is the sum of arr[start:(end + 1)] + ans -= self._prefix_sum(start - 1) # so that the result is the sum of arr[start:(end + 1)] return ans def __setitem__(self, idx: int, val: Union[int, float]) -> None: @@ -132,7 +132,7 @@ def __str__(self) -> str: """ return self.__repr__() - def __len__(self): + def __len__(self) -> int: """ Returns the length of binary indexed tree. """ diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..fa4f641 --- /dev/null +++ b/tests/README.md @@ -0,0 +1 @@ +To run tests from the command line use: python -m pytest --cov=ds/ --cov-report=html:tests/code_coverage \ No newline at end of file diff --git a/tests/README.txt b/tests/README.txt deleted file mode 100644 index 6d5e7ef..0000000 --- a/tests/README.txt +++ /dev/null @@ -1 +0,0 @@ -Run tests from the command line using: python -m pytest --cov=ds/ --cov-report=html:tests/code_coverage \ No newline at end of file