DSAIntermediatearticle

Segment Tree Data Structure: Efficient Range Queries

Master Segment Tree for efficient range queries and updates. Learn implementation in Java.

Sachin SarawgiApril 20, 20262 min read2 minute lesson

Segment Tree Data Structure

A Segment Tree is a versatile data structure used to perform range queries and updates efficiently. It allows us to process operations like Range Sum Query or Range Minimum/Maximum Query in $O(\log N)$ time, significantly faster than the $O(N)$ brute-force approach.

The Intuition

A Segment Tree is a binary tree where:

  1. Each leaf node represents a single element from the original array.
  2. Each internal node represents the combined result (sum, min, max, etc.) of its children's segments.

Java Implementation (Range Sum Query)

public class SegmentTree {
    private int[] tree;
    private int n;

    public SegmentTree(int[] arr) {
        n = arr.length;
        tree = new int[4 * n];
        build(arr, 1, 0, n - 1);
    }

    private void build(int[] arr, int node, int start, int end) {
        if (start == end) {
            tree[node] = arr[start];
            return;
        }
        int mid = (start + end) / 2;
        build(arr, 2 * node, start, mid);
        build(arr, 2 * node + 1, mid + 1, end);
        tree[node] = tree[2 * node] + tree[2 * node + 1];
    }

    public int query(int node, int start, int end, int L, int R) {
        if (R < start || end < L) return 0;
        if (L <= start && end <= R) return tree[node];
        
        int mid = (start + end) / 2;
        return query(2 * node, start, mid, L, R) + query(2 * node + 1, mid + 1, end, L, R);
    }
}

Why it matters

  • Range Queries: Efficiently querying sum, minimum, or maximum over a range $[L, R]$.
  • Dynamic Updates: Updating array elements in $O(\log N)$ time.

Complexity

  • Time: O(log N) for both query and update.
  • Space: O(N) to store the tree (typically 4 * N).
📚

Recommended Resources

Designing Data-Intensive ApplicationsBest Seller

The definitive guide to building scalable, reliable distributed systems by Martin Kleppmann.

View on Amazon
Kafka: The Definitive GuideEditor's Pick

Real-time data and stream processing by Confluent engineers.

View on Amazon
Apache Kafka Series on Udemy

Hands-on Kafka course covering producers, consumers, Kafka Streams, and Connect.

View Course

Practical engineering notes

Get the next backend guide in your inbox

One useful note when a new deep dive is published: system design tradeoffs, Java production lessons, Kafka debugging, database patterns, and AI infrastructure.

No spam. Just practical notes you can use at work.

Sachin Sarawgi

Written by

Sachin Sarawgi

Engineering Manager and backend engineer with 10+ years building distributed systems across fintech, enterprise SaaS, and startups. CodeSprintPro is where I write practical guides on system design, Java, Kafka, databases, AI infrastructure, and production reliability.

Keep Learning

Move through the archive without losing the thread.

Related Articles

More deep dives chosen from shared tags, category overlap, and reading difficulty.

More in DSA

Category-based suggestions if you want to stay in the same domain.