ExamplesBy LevelBy TopicLearning Paths
838 Fundamental

Interval Tree — Stabbing Queries

Functional Programming

Tutorial Video

Text description (accessibility)

This video demonstrates the "Interval Tree — Stabbing Queries" functional Rust example. Difficulty level: Fundamental. Key concepts covered: Functional Programming. Given a set of intervals [l, r] and a query point q, find all intervals that contain q (stabbing query). Key difference from OCaml: | Aspect | Rust | OCaml |

Tutorial

The Problem

Given a set of intervals [l, r] and a query point q, find all intervals that contain q (stabbing query). Naive linear scan is O(n) per query. An interval tree answers stabbing queries in O(log n + k) where k is the number of reported intervals, after O(n log n) preprocessing. This enables efficient scheduling conflict detection (does event at time q overlap any existing booking?), genomic range queries (which genes cover position q?), and network packet classification (which firewall rules match this port range?). Interval trees are the data structure behind many calendar and scheduling applications.

🎯 Learning Outcomes

  • • Build an interval tree using a centered decomposition: for each node, store intervals crossing the center
  • • Organize crossing intervals in two sorted lists: by left endpoint and by right endpoint
  • • Implement stabbing query: follow left or right child based on q vs. center, query crossing intervals
  • • Understand the O(log n + k) query complexity and O(n log n) build complexity
  • • Compare with segment tree and augmented BST approaches to range queries
  • Code Example

    #![allow(clippy::all)]
    //! Placeholder

    Key Differences

    AspectRustOCaml
    Tree structureOption<Box<IntervalTree>>Algebraic type with Leaf
    Sorted intervalsVec<(i64, i64)>(int * int) list
    Null handlingOptionPattern match on Leaf
    Build complexityO(n log n)Same
    Query complexityO(log n + k)Same
    MemoryCompact Vec tuplesGC-managed list nodes

    OCaml Approach

    OCaml represents the interval tree with an algebraic type: type t = Leaf | Node of { center: int; left_sorted: interval list; right_sorted: interval list; left_child: t; right_child: t }. The stab q tree function pattern-matches on the tree structure. List.filter or binary search on sorted lists reports crossing intervals. OCaml's immutable default makes building the tree via let rec build natural. The List.sort and List.rev operations create the two sorted interval lists.

    Full Source

    #![allow(clippy::all)]
    //! Placeholder

    Deep Comparison

    OCaml vs Rust: Interval Tree Stabbing

    Overview

    See the example.rs and example.ml files for detailed implementations.

    Key Differences

    AspectOCamlRust
    Type systemHindley-MilnerOwnership + traits
    MemoryGCZero-cost abstractions
    MutabilityExplicit refmut keyword
    Error handlingOption/ResultResult<T, E>

    See README.md for detailed comparison.

    Exercises

  • Implement all_overlaps(l, r): find all intervals that overlap the query interval [l, r].
  • Add deletion support: mark intervals as deleted with a tombstone and rebuild when tombstone ratio exceeds 50%.
  • Compare interval tree vs. sorted array + binary search for stabbing queries at varying n.
  • Implement a segment tree-based approach for stabbing queries and compare with interval tree.
  • Handle open/closed interval endpoints: (l, r), [l, r), (l, r], [l, r] all handled correctly.
  • Open Source Repos