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
4 changes: 2 additions & 2 deletions .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Continuous Integration Tests

on:
push:
branches: [ "main" ]
branches: [ "main", "dev" ]
pull_request:
branches: [ "main" ]
branches: [ "main", "dev" ]

permissions:
contents: read
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +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.
2 changes: 1 addition & 1 deletion all_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ds.heaps import MinHeap, MaxHeap
from ds.deque import Deque
from ds.binary_search_tree import BinarySearchTree
from ds.binary_index_tree import BinaryIndexTree
from ds.binary_indexed_tree import BinaryIndexedTree
from ds.segment_tree import SegmentTree
from ds.disjoint_sets import DisjointSets
from ds.trie import Trie
Expand Down
3 changes: 3 additions & 0 deletions ds/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ds/__pycache__/*
*__pycache__*
*.pyc
Binary file removed ds/__pycache__/binary_index_tree.cpython-311.pyc
Binary file not shown.
Binary file removed ds/__pycache__/binary_search_tree.cpython-311.pyc
Binary file not shown.
Binary file removed ds/__pycache__/disjoint_sets.cpython-311.pyc
Binary file not shown.
Binary file removed ds/__pycache__/segment_tree.cpython-311.pyc
Binary file not shown.
Binary file removed ds/__pycache__/trie.cpython-311.pyc
Binary file not shown.
48 changes: 25 additions & 23 deletions ds/binary_index_tree.py → ds/binary_indexed_tree.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# -*- coding: utf-8 -*-
"""
Binary index tree data structure.
Binary indexed tree data structure module, see help(BinaryIndexedTree) for details.
"""

from typing import Union, List


class BinaryIndexTree:
class BinaryIndexedTree:
"""
Binary Index Tree data-structure.
Also known as a Fenwick Tree, Binary Index Trees allow for evaluation of the sum function over a given
Binary Indexed Tree data-structure.

Also known as a Fenwick Tree, Binary Indexed Trees allow for evaluation of the sum function over a given
range in an array in O(log2(n)) time. They also support updates to the values of the array in O(log2(n))
time which is substantial improvement over O(n) update time required for a prefix-sum.
time which is substantial improvement over O(n) update time required for updates in a prefix-sum.

The binary representation of the array element's index is used for various purposes in constructing the
tree, making query evaluations, and updating values, hence the name Binary Index Tree.
tree, making query evaluations, and updating values, hence the name Binary Indexed Tree.

See: https://www.youtube.com/watch?v=uSFzHCZ4E-8&t=12s for details.
"""

Expand All @@ -27,10 +27,10 @@ def __init__(self, arr: List[Union[int, float]]):
Parameters
----------
arr : List[Union[int, float]]
An input array of values for which the binary index tree is built.
An input array of values for which the binary indexed tree is built.

"""
# Construct the binary index tree
# Construct the binary indexed tree
self.arr = arr.copy() # Store a copy of the original array internally
self.binary_idx_tree = arr.copy() # Start off with a copy of the original input array
for idx in range(1, len(self.binary_idx_tree) + 1): # Use 1-indexing throughout
Expand All @@ -41,7 +41,8 @@ def __init__(self, arr: List[Union[int, float]]):
def update(self, idx: int, val: Union[int, float]) -> None:
"""
Updates the value of an element in the original array located at a particular index (idx) and also the
binary index tree accordingly. An update can also be accomplished with: binary_idx_tree[idx] = val
binary indexed tree accordingly. An update can also be accomplished with: binary_idx_tree[idx] = val.
Performs updates in O(log2(n)) time.

Parameters
----------
Expand All @@ -63,39 +64,40 @@ def update(self, idx: int, val: Union[int, float]) -> None:
def _range_query(self, end: int) -> Union[float, int]:
"""
Internal helper function for performing range queries. Computes the sum of all elements up through
index end. Is used by range_query to compute the sum of elements start:end by taking the sum through
end and subtracting off the sum through (start-1).
index end. This method is used by range_query to compute the sum of elements start:end by taking the
sum through index=end and subtracting off the sum through index=(start-1). Runs in O(log2(n)) time.

Parameters
----------
end : int
The ending index of the range query.

Returns
-------
Union[float, int]
The evaluation of the range sum query from the first element through the end index element.

"""
end += 1 # Convert to 1-indexing
sum_total = 0 # Aggregate the sum total across all entries from the start up through idnex end
sum_total = 0 # Aggregate the sum total across all entries from the start, up through index end
while end > 0:
sum_total += self.binary_idx_tree[end - 1]
end -= (end & -end) # Flip the last set bit
return sum_total

def range_query(self, start: int, end: int) -> Union[float, int]:
"""
Performs a sum range query using the binary index tree and returns the aggregate answer. Computes the
sum [start, end] of the original array.
Performs a sum range query using the binary indexed tree and returns the aggregate answer. Computes
the sum of the array elements falling within the inclusive index interval [start, end] of the original
array. Runs in O(log2(n)) time.

Parameters
----------
start : int
The starting index of the range query.
end : int
The ending index of the range query.

Returns
-------
Union[float, int]
Expand Down Expand Up @@ -132,6 +134,6 @@ def __str__(self) -> str:

def __len__(self):
"""
Returns the length of binary index tree.
Returns the length of binary indexed tree.
"""
return len(self.binary_idx_tree)
Loading