#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
🌲 FREE · RANGE QUERIES

Segment Tree Visualizer: Fast Range Queries, Step by Step

Enter up to 16 numbers, run a range-sum query or a point update, and see which nodes are visited.

  • 🧮 Range sums
  • ✏️ Point updates
  • 🎯 Overlap cases

How to use the Segment Tree Visualizer

  1. 1. Set it upEnter 2–16 whole numbers and choose a query or an update.
  2. 2. RunPress Run.
  3. 3. ReplayStep through: green nodes are used whole, blue are split further, red are skipped.

Worked example

For the array 5, 8, 6, 3, 2, 7, 2, 6 the root holds the total, 39. Querying indexes 2 to 5 gives 6 + 3 + 2 + 7 = 18. The query uses two whole ranges, [2,3] = 9 and [4,5] = 9, without looking at their children, and skips [0,1] and [6,7] entirely. A plain loop would add 4 numbers here, but on a large array the tree touches only about 2 log n nodes however wide the range is.

Point updates

Switch to Point update, set position 3 to 10, and the leaf changes from 3 to 10. Only the sums on the path from that leaf to the root are recomputed, one node per level, and the total becomes 46. That's O(log n), compared with O(n) to rebuild a prefix-sum array after every change. That trade-off is why segment trees are used when an array changes between queries.

When to use one

  • Many range queries (sum, minimum, maximum, GCD) mixed with updates.
  • Competitive programming problems with up to 10⁵ or 10⁶ operations.
  • If there are no updates, a prefix-sum array is simpler; for sums with updates, a Fenwick tree is shorter to code.
  • With lazy propagation, a segment tree can also add a value to a whole range in O(log n).

Frequently asked questions

Can it do minimum instead of sum?

This visualizer shows sums. The structure is the same for min or max; only the combine step changes.

Why up to 16 elements?

So the whole tree fits on screen.

How much memory does a segment tree need?

About 2n nodes, often allocated as an array of 4n for simplicity.

Is the segment tree visualizer free?

Yes. You can use the segment tree visualizer without signing up. The first time you run it, we ask once for your name and WhatsApp number; after that the segment tree visualizer and every other Uncodemy tool open straight away.

More free tools

See all free tools