Skip to content

Segment tree

How a segment tree answers range minimum queries and updates its stored summaries when an individual bar low changes.

A segment tree is a data structure that stores summaries of contiguous pieces of an indexed array in a binary tree. It can answer a range query and replace an individual value in O(log⁡n)O(\log n) time per operation when summaries combine in constant time. A market-data service can use one to answer “what was the lowest bar low between these two times?” even after a vendor corrects an earlier bar.

Query the blocks that fit

Start with one leaf per observation, ordered by time. The root covers the whole array, and each internal node splits its range at the midpoint and stores the minimum of its two children. Write [l,r)[l,r) for a zero-based, half-open range. It includes index ll, excludes rr, and has r−lr-l entries. Adjacent ranges can therefore share a boundary without sharing an observation.

Suppose six consecutive bar lows in a fictional series are (8,3,7,5,2,9)(8,3,7,5,2,9) USD. To query [1,5)[1,5), ignore disjoint nodes, take the stored minimum of each node fully inside the query, and split any partially overlapping node. In the tree below, the selected ranges are [1,3)[1,3), [3,4)[3,4) and [4,5)[4,5). They cover indices 1 through 4 exactly once. Their stored minima give min⁡(3,5,2)=2\min(3,5,2)=2 USD. University of Illinois, Segment Trees, range-minimum queries.
Outlined blocks supply the query result. The dashed path shows which nodes the point assignment recomputes.

Replace one low

Suppose the feed corrects index 4 from 2 to 6 USD. Replace that leaf and walk back to the root. The parent [4,6)[4,6) now stores min⁡(6,9)=6\min(6,9)=6, its parent [3,6)[3,6) stores min⁡(5,6)=5\min(5,6)=5, and the root [0,6)[0,6) stores min⁡(3,5)=3\min(3,5)=3. The original query reads the same three blocks and now gives min⁡(3,5,6)=3\min(3,5,6)=3 USD. Recomputing from both children matters when the old minimum rises. The sibling of an updated node can supply the new minimum for their parent.

A point assignment recomputes one path. In a range query, at most two nodes at each depth partially overlap the requested interval and need to be split. This gives the worst-case O(log⁡n)O(\log n) bounds. Construction takes O(n)O(n) time and storage because each leaf and internal node needs one summary. The example assumes a fixed, sorted timestamp-to-index mapping and valid bar lows. In a historical replay, apply the correction when it became available to avoid look-ahead bias. The SelfMadeQuant lesson Tabular and Time-Indexed Data explains how to validate a time index and choose the aggregation for each price or volume field. The preview is public and the full lesson requires membership.

What the summaries must support

The merge must be associative so changing the grouping of consecutive blocks preserves the result. It can be order-sensitive as long as blocks are merged from left to right. An identity gives a neutral summary for empty ranges or unused leaves in a padded tree. For finite lows, +∞+\infty is the identity because min⁡(x,+∞)=x\min(x,+\infty)=x. It represents an empty contribution, so a missing market observation still needs its own data policy. For range totals with point changes, a Fenwick tree can obtain an interval total by subtracting prefix sums. Minima have no corresponding subtraction rule. AtCoder Library, Segtree.
A lazy segment tree can also handle compatible updates to whole ranges. When an update fully covers a node, it updates the stored summary and leaves a pending operation to pass to its children before descending. Adding a constant to every low in a covered block shifts the block minimum by the same constant. Adding a constant to every element of a sum block needs the block length as well as the old sum. The update must act directly on the stored summary, agree with merging separately updated children, and let successive pending operations compose in time order. The logarithmic bound assumes that applying and composing those operations take constant time. AtCoder Library, Lazy Segtree.

Sources

Last updated
  • data structures
  • algorithms
  • range queries
  • market data
← Back to the glossaryBack to top ↑