Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
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.
8 changes: 4 additions & 4 deletions ds/binary_indexed_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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.
"""
Expand Down
1 change: 1 addition & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
To run tests from the command line use: python -m pytest --cov=ds/ --cov-report=html:tests/code_coverage
1 change: 0 additions & 1 deletion tests/README.txt

This file was deleted.