Segment Tree Visualizer

Build a range-sum segment tree, inspect full-cover and no-overlap query nodes, and preview the path changed by a point update.

Values
8
Nodes
15
Height
4
Sum
21

Segment tree input

The first 16 whole numbers are used so the tree remains readable.

Range query

Point update

Step controls

Step 1 of 11: Start range sum query [2, 6] at the root.

Tree diagram

Start range sum query [2, 6] at the root.

Active Taken/updated Update path
[0, 7]23[0, 3]13[4, 7]10[0, 1]3[2, 3]10[4, 5]7[6, 7]3[0, 0]5[1, 1]-2[2, 2]7[3, 3]3[4, 4]6[5, 5]1[6, 6]4[7, 7]-1

Array view

0
5
1
-2
2
7
3
3
4
6
5
1
6
4
7
-1

Query summary

range = [2, 6]
taken nodes = 0
sum = 21

Trace

Step 1. Start range sum query [2, 6] at the root.
Step 2. Split [0, 7] because it partially overlaps the query.
Step 3. Split [0, 3] because it partially overlaps the query.
Step 4. Skip [0, 1] because it does not overlap [2, 6].
Step 5. Take [2, 3] as a full cover and add 10.
Step 6. Split [4, 7] because it partially overlaps the query.
Step 7. Take [4, 5] as a full cover and add 7.
Step 8. Split [6, 7] because it partially overlaps the query.
Step 9. Take [6, 6] as a full cover and add 4.
Step 10. Skip [7, 7] because it does not overlap [2, 6].
Step 11. Range sum query [2, 6] returns 21.

What is a Segment Tree Visualizer?

A segment tree visualizer shows how an array is split into nested ranges so range queries and point updates can be answered quickly.

This tool builds a sum segment tree. Every node stores a range [left, right] and the sum for that range. During a query, fully covered nodes are taken, non-overlapping nodes are skipped, and partially overlapping nodes are split.

For the full cluster of related pages, browse the Data Structure Visualizers hub.

How to use this segment tree visualizer

  • Paste an array of whole numbers.
  • Choose the left and right indexes for a range sum query.
  • Run the query to inspect full-cover, partial-overlap, and no-overlap decisions.
  • Choose an index and a new value to preview a point update.
  • Apply the update when you want to rebuild the tree from the changed array.

Segment tree complexity

A segment tree uses O(n) space and builds in O(n) time. A range query visits only the branches needed to cover the requested interval, which is why range queries are O(log n) for many common operations. A point update also touches one root-to-leaf path, so it runs in O(log n).

If your array never changes, compare this with the Prefix Sum Visualizer. If your array changes often, compare this page with the Fenwick Tree Visualizer.

For static range minimum and maximum queries, compare this page with the Sparse Table Visualizer.

Frequently Asked Questions

What does this segment tree visualizer store?
This visualizer stores range sums. Each internal node is the sum of its left and right child ranges.
Why does a segment tree query skip some nodes?
A node can be skipped when its stored range does not overlap the query range at all.
What is a full-cover node?
A full-cover node is a segment whose range sits entirely inside the query range, so its stored sum can be added without visiting its children.
Can this segment tree handle updates?
Yes. The point update preview highlights the leaf that changes and the ancestor nodes whose sums must be recomputed.

Related tools