Fenwick Tree (Binary Indexed Tree - BIT)
A data structure that can efficiently update elements and calculate prefix sums in O(log n) time. Also known as Binary Indexed Tree (BIT), invented by Peter Fenwick in 1994. Fenwick trees are more space-efficient and simpler than segment trees for range sum queries, using only O(n) space compared to segment tree's O(4n).
Visualization
Interactive visualization for Fenwick Tree (Binary Indexed Tree - BIT)
Fenwick Tree (Binary Indexed Tree)
Original Array:
Fenwick Tree (1-indexed):
Update Value
Range Sum Query
• Update: O(log n) - Updates value at index and propagates changes
• Query: O(log n) - Computes prefix sum or range sum
• Space: O(n) - Efficient tree representation
Interactive visualization with step-by-step execution
Implementation
1class FenwickTree {
2 private tree: number[];
3 private n: number;
4
5 constructor(n: number) {
6 this.n = n;
7 this.tree = new Array(n + 1).fill(0); // 1-indexed
8 }
9
10 // Get lowest set bit
11 private LSB(x: number): number {
12 return x & (-x);
13 }
14
15 // Add delta to index (1-indexed)
16 update(index: number, delta: number): void {
17 while (index <= this.n) {
18 this.tree[index] += delta;
19 index += this.LSB(index);
20 }
21 }
22
23 // Get prefix sum [1, index]
24 prefixSum(index: number): number {
25 let sum = 0;
26 while (index > 0) {
27 sum += this.tree[index];
28 index -= this.LSB(index);
29 }
30 return sum;
31 }
32
33 // Get range sum [left, right] (1-indexed)
34 rangeSum(left: number, right: number): number {
35 return this.prefixSum(right) - this.prefixSum(left - 1);
36 }
37
38 // Build from array
39 static fromArray(arr: number[]): FenwickTree {
40 const tree = new FenwickTree(arr.length);
41 for (let i = 0; i < arr.length; i++) {
42 tree.update(i + 1, arr[i]); // Convert to 1-indexed
43 }
44 return tree;
45 }
46}Deep Dive
Theoretical Foundation
Fenwick tree stores partial sums in an array using a clever indexing scheme based on binary representation. Each index i is responsible for a range of elements determined by the position of the lowest set bit in i. This allows O(log n) prefix sum queries and updates by traversing ancestors/descendants in the tree structure implicitly stored in the array.
Complexity
Time
O(log n)
O(log n)
O(log n)
Space
O(n)
Applications
Industry Use
Dynamic ranking systems
Inversion count in arrays
Range sum queries with updates
Competitive programming
2D range sum queries (2D BIT)
Order statistics
Fenwick tree on coordinate compression
Use Cases
Related Algorithms
AVL Tree (Self-Balancing BST)
A self-balancing binary search tree where the heights of the two child subtrees of any node differ by at most one. Named after inventors Adelson-Velsky and Landis (1962), AVL trees guarantee O(log n) time for search, insert, and delete operations by maintaining balance through rotations. It was the first self-balancing binary search tree to be invented.
Segment Tree (Range Query Tree)
Segment Tree is a binary tree data structure for storing intervals/segments that enables efficient range queries (sum, min, max, GCD) and updates in O(log n) time. Each node represents an interval, with leaves representing single elements. Essential for competitive programming, it solves problems like range sum queries with updates, which would be O(n) with arrays but becomes O(log n) with segment trees.
Lowest Common Ancestor (LCA)
Lowest Common Ancestor (LCA) finds the deepest node that is an ancestor of two given nodes in a tree. This fundamental tree query operation has applications in computational biology (phylogenetic trees), version control systems (finding merge base), network routing, and range queries. Multiple algorithms exist: simple recursive O(n), binary lifting O(log n) per query after O(n log n) preprocessing, and Tarjan's offline algorithm for batch queries.
Morris Traversal (Threaded Binary Tree)
Morris Traversal is a brilliant tree traversal technique that achieves O(1) space complexity without using recursion or an explicit stack. Invented by Joseph H. Morris in 1979, it temporarily modifies the tree structure by creating 'threads' (temporary links) that allow the algorithm to traverse back to ancestors. After traversal, the original tree structure is restored. This makes it ideal for memory-constrained environments and demonstrates creative problem-solving in algorithm design.