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.

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:
- We went to left subtree because x.low <= max in left subtree
- max in left subtree is a high of one of the intervals let us say [a, max] in left subtree.
- Since x doesn’t overlap with any node in left subtree x.high must be smaller than ‘a‘.
- All nodes in BST are ordered by low value, so all nodes in right subtree must have low value greater than ‘a‘.
- 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
C++ // C++ Program to implement Interval Tree #include <iostream> using namespace std; // Structure to represent an interval struct Interval { int low, high; }; // Structure to represent a node in Interval Search Tree struct Node { Interval *i; int max; Node *left, *right; }; // A utility function to create a new Interval Search Tree Node Node * newNode(Interval i) { Node *temp = new Node; temp->i = new Interval(i); temp->max = i.high; temp->left = temp->right = nullptr; return temp; }; // A utility function to insert a new Interval Search Tree Node // This is similar to BST Insert. Here the low value of interval // is used tomaintain BST property Node *insert(Node *root, Interval i) { // Base case: Tree is empty, new node becomes root if (root == nullptr) return newNode(i); // Get low value of interval at root int l = root->i->low; // If root's low value is smaller, // then new interval goes to left subtree if (i.low < l) root->left = insert(root->left, i); // Else, new node goes to right subtree. else root->right = insert(root->right, i); // Update the max value of this ancestor if needed if (root->max < i.high) root->max = i.high; return root; } // A utility function to check if given two intervals overlap bool isOverlapping(Interval i1, Interval i2) { if (i1.low <= i2.high && i2.low <= i1.high) return true; return false; } // The main function that searches a given // interval i in a given Interval Tree. Interval *overlapSearch(Node *root, Interval i) { // Base Case, tree is empty if (root == nullptr) return nullptr; // If given interval overlaps with root if (isOverlapping(*(root->i), i)) return root->i; // If left child of root is present and max of left child is // greater than or equal to given interval, then i may // overlap with an interval is left subtree if (root->left != nullptr && root->left->max >= i.low) return overlapSearch(root->left, i); // Else interval can only overlap with right subtree return overlapSearch(root->right, i); } void inorder(Node *root) { if (root == nullptr) return; inorder(root->left); cout << "[" << root->i->low << ", " << root->i->high << "]" << " max = " << root->max << endl; inorder(root->right); } int main() { Interval ints[] = {{15, 20}, {10, 30}, {17, 19}, {5, 20}, {12, 15}, {30, 40} }; int n = sizeof(ints)/sizeof(ints[0]); Node *root = nullptr; for (int i = 0; i < n; i++) root = insert(root, ints[i]); cout << "Inorder traversal of constructed Interval Tree is\n"; inorder(root); Interval x = {6, 7}; cout << "\nSearching for interval [" << x.low << "," << x.high << "]"; Interval *res = overlapSearch(root, x); if (res == nullptr) cout << "\nNo Overlapping Interval"; else cout << "\nOverlaps with [" << res->low << ", " << res->high << "]"; return 0; }
Java // C++ Program to implement Interval Tree import java.util.*; class Interval { int low, high; Interval(int low, int high) { this.low = low; this.high = high; } } class Node { Interval i; int max; Node left, right; } class GFG { // A utility function to create a new Interval Search Tree Node static Node newNode(Interval i) { Node temp = new Node(); temp.i = new Interval(i.low, i.high); temp.max = i.high; temp.left = temp.right = null; return temp; } // A utility function to insert a new Interval Search Tree Node // This is similar to BST Insert. Here the low value of interval // is used tomaintain BST property static Node insert(Node root, Interval i) { // Base case: Tree is empty, new node becomes root if (root == null) return newNode(i); // Get low value of interval at root int l = root.i.low; // If root's low value is smaller, // then new interval goes to left subtree if (i.low < l) root.left = insert(root.left, i); // Else, new node goes to right subtree. else root.right = insert(root.right, i); // Update the max value of this ancestor if needed if (root.max < i.high) root.max = i.high; return root; } // A utility function to check if given two intervals overlap static boolean isOverlapping(Interval i1, Interval i2) { if (i1.low <= i2.high && i2.low <= i1.high) return true; return false; } // The main function that searches a given // interval i in a given Interval Tree. static Interval overlapSearch(Node root, Interval i) { // Base Case, tree is empty if (root == null) return null; // If given interval overlaps with root if (isOverlapping(root.i, i)) return root.i; // If left child of root is present and max of left child is // greater than or equal to given interval, then i may // overlap with an interval is left subtree if (root.left != null && root.left.max >= i.low) return overlapSearch(root.left, i); // Else interval can only overlap with right subtree return overlapSearch(root.right, i); } static void inorder(Node root) { if (root == null) return; inorder(root.left); System.out.println("[" + root.i.low + ", " + root.i.high + "]" + " max = " + root.max); inorder(root.right); } public static void main(String[] args) { Interval[] ints = { new Interval(15, 20), new Interval(10, 30), new Interval(17, 19), new Interval(5, 20), new Interval(12, 15), new Interval(30, 40) }; int n = ints.length; Node root = null; for (int i = 0; i < n; i++) root = insert(root, ints[i]); System.out.println("Inorder traversal of constructed Interval Tree is"); inorder(root); Interval x = new Interval(6, 7); System.out.print("\nSearching for interval [" + x.low + "," + x.high + "]"); Interval res = overlapSearch(root, x); if (res == null) System.out.println("\nNo Overlapping Interval"); else System.out.println("\nOverlaps with [" + res.low + ", " + res.high + "]"); } }
Python # C++ Program to implement Interval Tree # Structure to represent an interval class Interval: def __init__(self, low, high): self.low = low self.high = high # Structure to represent a node in Interval Search Tree class Node: def __init__(self, i): self.i = i self.max = i.high self.left = None self.right = None # A utility function to create a new Interval Search Tree Node def newNode(i): temp = Node(Interval(i.low, i.high)) return temp # A utility function to insert a new Interval Search Tree Node # This is similar to BST Insert. Here the low value of interval # is used tomaintain BST property def insert(root, i): # Base case: Tree is empty, new node becomes root if root is None: return newNode(i) # Get low value of interval at root l = root.i.low # If root's low value is smaller, # then new interval goes to left subtree if i.low < l: root.left = insert(root.left, i) # Else, new node goes to right subtree. else: root.right = insert(root.right, i) # Update the max value of this ancestor if needed if root.max < i.high: root.max = i.high return root # A utility function to check if given two intervals overlap def isOverlapping(i1, i2): if i1.low <= i2.high and i2.low <= i1.high: return True return False # The main function that searches a given # interval i in a given Interval Tree. def overlapSearch(root, i): # Base Case, tree is empty if root is None: return None # If given interval overlaps with root if isOverlapping(root.i, i): return root.i # If left child of root is present and max of left child is # greater than or equal to given interval, then i may # overlap with an interval is left subtree if root.left is not None and root.left.max >= i.low: return overlapSearch(root.left, i) # Else interval can only overlap with right subtree return overlapSearch(root.right, i) def inorder(root): if root is None: return inorder(root.left) print("[" + str(root.i.low) + ", " + str(root.i.high) + "]" + " max = " + str(root.max)) inorder(root.right) def main(): ints = [Interval(15, 20), Interval(10, 30), Interval(17, 19), Interval(5, 20), Interval(12, 15), Interval(30, 40)] n = len(ints) root = None for i in range(n): root = insert(root, ints[i]) print("Inorder traversal of constructed Interval Tree is") inorder(root) x = Interval(6, 7) print("\nSearching for interval [" + str(x.low) + "," + str(x.high) + "]", end="") res = overlapSearch(root, x) if res is None: print("\nNo Overlapping Interval") else: print("\nOverlaps with [" + str(res.low) + ", " + str(res.high) + "]") if __name__ == "__main__": main()
C# // C++ Program to implement Interval Tree using System; class Interval { public int low, high; public Interval(int low, int high) { this.low = low; this.high = high; } } class Node { public Interval i; public int max; public Node left, right; } class GFG { // A utility function to create a new Interval Search Tree Node static Node newNode(Interval i) { Node temp = new Node(); temp.i = new Interval(i.low, i.high); temp.max = i.high; temp.left = temp.right = null; return temp; } // A utility function to insert a new Interval Search Tree Node // This is similar to BST Insert. Here the low value of interval // is used tomaintain BST property static Node insert(Node root, Interval i) { // Base case: Tree is empty, new node becomes root if (root == null) return newNode(i); // Get low value of interval at root int l = root.i.low; // If root's low value is smaller, // then new interval goes to left subtree if (i.low < l) root.left = insert(root.left, i); // Else, new node goes to right subtree. else root.right = insert(root.right, i); // Update the max value of this ancestor if needed if (root.max < i.high) root.max = i.high; return root; } // A utility function to check if given two intervals overlap static bool isOverlapping(Interval i1, Interval i2) { if (i1.low <= i2.high && i2.low <= i1.high) return true; return false; } // The main function that searches a given // interval i in a given Interval Tree. static Interval overlapSearch(Node root, Interval i) { // Base Case, tree is empty if (root == null) return null; // If given interval overlaps with root if (isOverlapping(root.i, i)) return root.i; // If left child of root is present and max of left child is // greater than or equal to given interval, then i may // overlap with an interval is left subtree if (root.left != null && root.left.max >= i.low) return overlapSearch(root.left, i); // Else interval can only overlap with right subtree return overlapSearch(root.right, i); } static void inorder(Node root) { if (root == null) return; inorder(root.left); Console.WriteLine("[" + root.i.low + ", " + root.i.high + "]" + " max = " + root.max); inorder(root.right); } static void Main() { Interval[] ints = new Interval[] { new Interval(15, 20), new Interval(10, 30), new Interval(17, 19), new Interval(5, 20), new Interval(12, 15), new Interval(30, 40) }; int n = ints.Length; Node root = null; for (int i = 0; i < n; i++) root = insert(root, ints[i]); Console.WriteLine("Inorder traversal of constructed Interval Tree is"); inorder(root); Interval x = new Interval(6, 7); Console.Write("\nSearching for interval [" + x.low + "," + x.high + "]"); Interval res = overlapSearch(root, x); if (res == null) Console.WriteLine("\nNo Overlapping Interval"); else Console.WriteLine("\nOverlaps with [" + res.low + ", " + res.high + "]"); } }
JavaScript // C++ Program to implement Interval Tree // Structure to represent an interval class Interval { constructor(low, high) { this.low = low; this.high = high; } } // Structure to represent a node in Interval Search Tree class Node { constructor(i) { this.i = i; this.max = i.high; this.left = null; this.right = null; } } // A utility function to create a new Interval Search Tree Node function newNode(i) { let temp = new Node(new Interval(i.low, i.high)); return temp; } // A utility function to insert a new Interval Search Tree Node // This is similar to BST Insert. Here the low value of interval // is used tomaintain BST property function insert(root, i) { // Base case: Tree is empty, new node becomes root if (root === null) return newNode(i); // Get low value of interval at root let l = root.i.low; // If root's low value is smaller, // then new interval goes to left subtree if (i.low < l) root.left = insert(root.left, i); // Else, new node goes to right subtree. else root.right = insert(root.right, i); // Update the max value of this ancestor if needed if (root.max < i.high) root.max = i.high; return root; } // A utility function to check if given two intervals overlap function isOverlapping(i1, i2) { if (i1.low <= i2.high && i2.low <= i1.high) return true; return false; } // The main function that searches a given // interval i in a given Interval Tree. function overlapSearch(root, i) { // Base Case, tree is empty if (root === null) return null; // If given interval overlaps with root if (isOverlapping(root.i, i)) return root.i; // If left child of root is present and max of left child is // greater than or equal to given interval, then i may // overlap with an interval is left subtree if (root.left !== null && root.left.max >= i.low) return overlapSearch(root.left, i); // Else interval can only overlap with right subtree return overlapSearch(root.right, i); } function inorder(root) { if (root === null) return; inorder(root.left); console.log("[" + root.i.low + ", " + root.i.high + "]" + " max = " + root.max); inorder(root.right); } function main() { let ints = [new Interval(15, 20), new Interval(10, 30), new Interval(17, 19), new Interval(5, 20), new Interval(12, 15), new Interval(30, 40) ]; let n = ints.length; let root = null; for (let i = 0; i < n; i++) root = insert(root, ints[i]); console.log("Inorder traversal of constructed Interval Tree is"); inorder(root); let x = new Interval(6, 7); console.log("\nSearching for interval [" + x.low + "," + x.high + "]"); let res = overlapSearch(root, x); if (res === null) console.log("\nNo Overlapping Interval"); else console.log("\nOverlaps with [" + res.low + ", " + res.high + "]"); } main();
OutputInorder 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.
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.
Similar Reads
Introduction to Tree Data Structure
Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree. Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) o
15+ min read
Tree Traversal Techniques
Tree Traversal techniques include various ways to visit all the nodes of the tree. Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. In this article, we will discuss all the tree travers
7 min read
Applications of tree data structure
A tree is a type of data structure that represents a hierarchical relationship between data elements, called nodes. The top node in the tree is called the root, and the elements below the root are called child nodes. Each child node may have one or more child nodes of its own, forming a branching st
4 min read
Advantages and Disadvantages of Tree
Tree is a non-linear data structure. It consists of nodes and edges. A tree represents data in a hierarchical organization. It is a special type of connected graph without any cycle or circuit. Advantages of Tree:Efficient searching: Trees are particularly efficient for searching and retrieving data
2 min read
Difference between an array and a tree
Array:An array is a collection of homogeneous(same type) data items stored in contiguous memory locations. For example, if an array is of type âintâ, it can only store integer elements and cannot allow the elements of other types such as double, float, char, etc. The array is a linear data structure
3 min read
Inorder Tree Traversal without Recursion
Given a binary tree, the task is to perform in-order traversal of the tree without using recursion. Example: Input: Output: 4 2 5 1 3Explanation: Inorder traversal (Left->Root->Right) of the tree is 4 2 5 1 3 Input: Output: 1 7 10 8 6 10 5 6Explanation: Inorder traversal (Left->Root->Rig
8 min read
Types of Trees in Data Structures
A tree in data structures is a hierarchical data structure that consists of nodes connected by edges. It is used to represent relationships between elements, where each node holds data and is connected to other nodes in a parent-child relationship. Types of Trees The main types of trees in data stru
4 min read
Generic Trees (N-ary Tree)
Introduction to Generic Trees (N-ary Trees)
Generic trees are a collection of nodes where each node is a data structure that consists of records and a list of references to its children(duplicate references are not allowed). Unlike the linked list, each node stores the address of multiple nodes. Every node stores address of its children and t
5 min read
Inorder traversal of an N-ary Tree
Given an N-ary tree containing, the task is to print the inorder traversal of the tree. Examples:Â Input: N = 3Â Â Output: 5 6 2 7 3 1 4Input: N = 3Â Â Output: 2 3 5 1 4 6Â Approach: The inorder traversal of an N-ary tree is defined as visiting all the children except the last then the root and finall
6 min read
Preorder Traversal of an N-ary Tree
Given an N-ary Tree. The task is to write a program to perform the preorder traversal of the given n-ary tree. Examples: Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 O
14 min read
Iterative Postorder Traversal of N-ary Tree
Given an N-ary tree, the task is to find the post-order traversal of the given tree iteratively.Examples: Input: 1 / | \ 3 2 4 / \ 5 6 Output: [5, 6, 3, 2, 4, 1] Input: 1 / \ 2 3 Output: [2, 3, 1] Approach:We have already discussed iterative post-order traversal of binary tree using one stack. We wi
10 min read
Level Order Traversal of N-ary Tree
Given an N-ary Tree. The task is to print the level order traversal of the tree where each level will be in a new line. Examples: Input: Output: 13 2 45 6Explanation: At level 1: only 1 is present.At level 2: 3, 2, 4 is presentAt level 3: 5, 6 is present Input: Output: 12 3 4 56 7 8 9 1011 12 1314Ex
11 min read
ZigZag Level Order Traversal of an N-ary Tree
Given a Generic Tree consisting of n nodes, the task is to find the ZigZag Level Order Traversal of the given tree.Note: A generic tree is a tree where each node can have zero or more children nodes. Unlike a binary tree, which has at most two children per node (left and right), a generic tree allow
8 min read
Binary Tree
Introduction to Binary Tree
Binary Tree is a non-linear and hierarchical data structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves. Representation of Binary TreeEach node in a Binar
15+ min read
Properties of Binary Tree
This post explores the fundamental properties of a binary tree, covering its structure, characteristics, and key relationships between nodes, edges, height, and levels Note: Height of root node is considered as 0. Properties of Binary Trees1. Maximum Nodes at Level 'l'A binary tree can have at most
4 min read
Applications, Advantages and Disadvantages of Binary Tree
A binary tree is a tree that has at most two children for any of its nodes. There are several types of binary trees. To learn more about them please refer to the article on "Types of binary tree" Applications:General ApplicationsDOM in HTML: Binary trees help manage the hierarchical structure of web
2 min read
Binary Tree (Array implementation)
Given an array that represents a tree in such a way that array indexes are values in tree nodes and array values give the parent node of that particular index (or node). The value of the root node index would always be -1 as there is no parent for root. Construct the standard linked representation o
6 min read
Complete Binary Tree
We know a tree is a non-linear data structure. It has no limitation on the number of children. A binary tree has a limitation as any node of the tree has at most two children: a left and a right child. What is a Complete Binary Tree?A complete binary tree is a special type of binary tree where all t
7 min read
Perfect Binary Tree
What is a Perfect Binary Tree? A perfect binary tree is a special type of binary tree in which all the leaf nodes are at the same depth, and all non-leaf nodes have two children. In simple terms, this means that all leaf nodes are at the maximum depth of the tree, and the tree is completely filled w
4 min read