NEW
A tree data structure supporting efficient range queries and updates over arrays.
DESCRIPTION
A segment tree organizes array intervals in a binary tree to answer range queries (sum, min, max) and perform updates in O(log n) time. It precomputes information for segments, enabling fast aggregation over arbitrary ranges. Variants include lazy propagation to defer and batch updates for efficiency. Segment trees trade additional memory for query/update speed and are common in competitive programming and real-time analytics. They complement Fenwick trees by handling a broader set of range operations. Persistent and dynamic forms extend use to versioned data and changing array sizes.
ALTERNATIVE DEFINITIONS
Wikipedia
In computer science, a segment tree is a tree data structure for storing intervals, or segments.
Emphasizes storage of intervals/segments as the core concept.
CP-Algorithms
Segment Tree is a data structure that allows answering range queries over an array, while modifying the array.
Highlights simultaneous support for queries and updates.
RELATED TERMS
Interval Tree Range Minimum Query Lazy Propagation Prefix Sum Binary Search Tree Treap
COMPETING TERMS
KNOWLEDGE AREAS
Mathematics Software Engineering Data Science
TARGET AUDIENCE
Software Developer Data Scientist AI Engineer Data Engineer Software Architect
EXTERNAL SOURCES
- Wikipedia - Segment tree
- CP-Algorithms - Practical guide and implementations
- Topcoder - Tutorial including lazy propagation
TOP QUESTIONS
What is a Segment Tree used for?
A Segment Tree supports fast range queries and updates, such as sums, minima, or maxima over subarrays. It’s widely used in competitive programming and real-time analytics pipelines.
Category: Overview
How does lazy propagation work in a Segment Tree?
Lazy propagation defers updates to child nodes until needed, storing pending operations in markers. This keeps range updates efficient without immediately touching all descendants.
Category: Concepts
When should I choose Segment Tree over Fenwick Tree?
Choose Segment Tree when you need operations beyond prefix sums, such as range minimum/maximum or range updates with lazy propagation. Fenwick Trees excel for simple sum or frequency aggregation.
Category: Decision
What are the time and space complexities of a Segment Tree?
Queries and updates are O(log n), and building is O(n). Space is typically about 4n for a static array-based implementation.
Category: Performance
Can a Segment Tree handle dynamic arrays?
Yes, using pointer-based trees or implicit trees you can support insertions/deletions. However, complexity and implementation difficulty increase compared to fixed-size arrays.
Category: Design
What are common mistakes when implementing a Segment Tree?
Typical mistakes include off-by-one indexing errors, forgetting to propagate lazy updates, and incorrect base cases. Clear index conventions and tests help avoid these pitfalls.
Category: Mistakes
Is a Segment Tree suitable for multidimensional data?
You can build higher-dimensional segment trees, but complexity and memory usage grow rapidly. Alternatives like Fenwick Trees or specialized spatial structures might be preferable.
Category: Scope
How does a Segment Tree compare to a Sparse Table?
Sparse Tables offer O(1) queries for idempotent operations like min but are static; Segment Trees support updates in O(log n), trading speed for flexibility.
Category: Comparison
What is a real-world analogy for a Segment Tree?
Think of it as a hierarchical ledger: each folder summarizes a subset of accounts, letting you quickly total any range by consulting only relevant folders.
Category: Example