How a Fenwick tree sums a prefix

Short answer: A Fenwick tree, or binary indexed tree, stores partial sums so that the prefix sum through index i, and a point update at i, both run in O(log n). The index trick is the least significant set bit: i & −i in two’s complement. That bit names…

How a binary heap sifts in an array

Short answer: A binary heap stores a complete binary tree in an array. The parent of index i is floor((i−1)/2), and the children are 2i+1 and 2i+2, for zero-based indexes. The heap property says every parent is ordered before its children. Sift up repairs an insertion at the end.…