Data Structures · Visualization

Segment Tree: Introduction

Segment Tree

A segment tree is a binary tree built over an array where each node represents a range (segment) of the array, and stores a summary value for that range — most commonly a sum, but it could be a min, max, or other combinable value. Time: O(log n) for range query and point update, O(n) to build. Space: O(n).

Big-O: Build O(n); range query and point update O(log n).

Segment Tree: Introduction

Segment Tree

A segment tree is a binary tree built over an array where each node represents a range (segment) of the array, and stores a summary value for that range — most commonly a sum, but it could be a min, max, or other combinable value. The root covers the whole array, and each node's two children split its range in half. This structure lets you answer 'summary of any range' queries and update individual elements much faster than recalculating from scratch each time.

How Segment Tree: Introduction Works

  1. The root node represents the entire array's range and stores its combined value (e.g. sum).
  2. Split the range in half; the left child covers the first half, the right child covers the second half.
  3. Keep splitting recursively until each leaf node represents a single array element.
  4. Every internal node's value is the combination (e.g. sum) of its two children's values.

Segment Tree: Introduction Worked Example

Input: Build a sum segment tree over [1, 3, 5, 7]

  1. Leaves: [1], [3], [5], [7]
  2. Combine pairs: [1,3] -> 4, [5,7] -> 12
  3. Combine the two halves: [1,3,5,7] -> 16 (the root)

Output: 16 / \ 4 12 / \ / \ 1 3 5 7

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
Segment Tree: Introduction
A binary tree where each node covers a range of the array and stores a combined value (like a sum) for that range — letting range queries skip most of the array.
39
[0…7]
17
[0…3]
22
[4…7]
8
[0…1]
9
[2…3]
13
[4…5]
9
[6…7]
5
[0]
3
[1]
8
[2]
1
[3]
9
[4]
4
[5]
7
[6]
2
[7]
Base array:53819472
Active Leaf
Segment Tree: Introduction
Press ▶ Run to watch it step by step.
Python · Segment Trees in Code
# Segment tree: 2n array.
# leaves hold the array; parents hold the
# SUM of their children.
n = 8
tree = [1, 3, 5, 7]

# node i's children: 2*i and 2*i+1
# example: intro on [1, 3, 5, 7]

Segment Tree: Introduction Time & Space Complexity

TimeO(log n) for range query and point update, O(n) to build
SpaceO(n)

When to Use Segment Tree: Introduction

  • Fast range-sum, range-min, or range-max queries over a changing array
  • Competitive programming problems involving repeated range queries and updates
  • Interval scheduling and range-based statistics in real-time systems

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.