VOOZH about

URL: https://www.geeksforgeeks.org/dsa/interval-tree/

⇱ Interval Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Interval Tree

Last Updated : 23 Jul, 2025

You are given a set of intervals, where each interval contains two variables, low and high, that defines the start and end time of the interval. The task is perform the following operations efficiently:

  • Add an interval
  • Remove an interval
  • Given an interval x, find if x overlaps with any of the existing intervals.

What is an Interval Tree?

The idea is to augment a self-balancing Binary Search Tree (BST) called Interval Tree similar to Red Black Tree, AVL Tree, etc to maintain set of intervals so that all operations can be done in O(log n) time. 
Every node of Interval Tree stores following information. 

  • i: An interval which is represented as a pair [start, end]
  • max: Maximum high value in subtree rooted with this node.

The low value of an interval is used as key to maintain order in BST. The insert and delete operations are same as insert and delete in self-balancing BST used. 

👁 IntervalSearcTree

How to find if the given interval overlaps with existing interval ?

Following is algorithm for searching an overlapping interval x in an Interval tree rooted with root:

  • If x overlaps with root's interval, return the root's interval.
  • If left child of root is not empty and the max in left child is greater than x's low value, recur for left child
  • Else recur for right child.

How does the above algorithm work?

Let the interval to be searched be x. We need to prove this in for following two cases:

Case 1: When we go to right subtree, one of the following must be true. 

  • There is an overlap in right subtree: This is fine as we need to return one overlapping interval. 
  • There is no overlap in either subtree: We go to right subtree only when either left is NULL or maximum value in left is smaller than x.low. So the interval cannot be present in left subtree.

Case 2: When we go to left subtree, one of the following must be true. 

  • There is an overlap in left subtree: This is fine as we need to return one overlapping interval. 
  • There is no overlap in either subtree: This is the most important part. We need to consider following facts:
  1. We went to left subtree because x.low <= max in left subtree 
  2. max in left subtree is a high of one of the intervals let us say [a, max] in left subtree. 
  3. Since x doesn't overlap with any node in left subtree x.high must be smaller than 'a'. 
  4. All nodes in BST are ordered by low value, so all nodes in right subtree must have low value greater than 'a'.
  5. From above two facts, we can say all intervals in right subtree have low value greater than x.high. So x cannot overlap with any interval in right subtree.

How do Insert and Delete work?

The operations work same as Binary Search Tree (BST) insert and delete operations as a Segment tree is mainly a BST


Output
Inorder traversal of constructed Interval Tree is
[5, 20] max = 20
[10, 30] max = 30
[12, 15] max = 15
[15, 20] max = 40
[17, 19] max = 40
[30, 40] max = 40

Searching for interval [6,7]
Overlaps with [5, 20]

Time Complexity: O(n*h), where n is the number of intervals, and h is the height of Interval Tree. In average cases, h = log (n) and the time complexity will be O(n * log(n)). In the worst case, the tree will be skewed and the height will be n, thus the time complexity will be O(n ^ 2). If Interval Tree is made self-balancing like AVL Tree, then time complexity reduces to O(n * log n).
Space Complexity: O(n + h), to store the n intervals, and considering the recursive call stack which can be O(h).

Applications of Interval Tree:

Interval tree is mainly a geometric data structure and often used for windowing queries, for instance, to find all roads on a computerized map inside a rectangular viewport, or to find all visible elements inside a three-dimensional scene

Interval Tree vs Segment Tree

Both segment and interval trees store intervals. Segment tree is mainly optimized for queries for a given point, and interval trees are mainly optimized for overlapping queries for a given interval.

Operations can be perform on the interval tree are:

Interval trees are a type of data structure used for organizing and searching intervals (i.e., ranges of values). The following are some of the operations that can be performed on an interval tree:

  • Insertion: Add a new interval to the tree.
  • Deletion: Remove an interval from the tree.
  • Search: Find all intervals that overlap with a given interval.
  • Query: Find the interval in the tree that contains a given point.
  • Range query: Find all intervals that overlap with a given range.
  • Merge: Combine two or more interval trees into a single tree.
  • Split: Divide a tree into two or more smaller trees based on a given interval.
  • Balancing: Maintain the balance of the tree to ensure its performance is optimized.
  • Traversal: Visit all intervals in the tree in a specific order, such as in-order, pre-order, or post-order.

In addition to these basic operations, interval trees can be extended to support more advanced operations, such as searching for intervals with a specific length, finding the closest intervals to a given point, and more. The choice of operations depends on the specific use case and requirements of the application.

Comment
Article Tags: