Consider a situation where we have a set of intervals and we need following operations to be implemented efficiently:
- Add an interval
- Remove an interval
- Given an interval x, find if x overlaps with any of the existing intervals.
An Interval Tree can be implemented as an augmented binary-search tree (preferably self-balancing), and thus enable us to perform the required operations in O(logN) time complexity.
Each node of the tree will store the following information:
- An interval i: represented as a pair [low, high].
- Metadata max of right-endpoints: The maximum of the right-endpoints of all the intervals stored in the subtree rooted at this node. Storing this metadata is how we are augmenting the tree.
Example Interval Tree used in Interval Tree | Set 1:
In Interval Tree | Set 1, we saw how to implement an interval tree using a simple BST (which is not self-balancing). In this article, we will use the built-in GNU tree-based container to implement an Interval Tree. The benefits of doing so are:
- We do not have to code our own tree data structure.
- We get the default operations like insert and delete out-of-the-box.
- We get to use the in-built Red-Black tree implementation, which means our tree will be self-balancing.
We will be using GNU policy-based implementation of tree data structure.
The article Policy-based data structures in g++ introduces GNU policy-based data structure along with the required header files.
We will define our own Node_update policy such that we can maintain the maximum of right-endpoints of intervals in the subtree as metadata in the nodes of our tree.
The syntax for defining a custom Node_policy is:
- type_of_our_metadata: int in our case since we want to store the metadata “max of right-endpoints of intervals in the subtree”.
- void operator()(node_iterator it, const_node_iterator end_it): method which is called internally to restore node-invariance, i.e. maintaining correct metadata, after invariance has been invalidated.
- it: node_iterator to the node whose invariance we need to restore.
- end_it: const_node_iterator to a just-after-leaf node.
See GNU tree-based containers for more details.
We will also define a method overlapSearch which searches for any interval in the tree overlapping with a given interval i.
Below is the implementation of the Interval Tree:
Searching for interval [25, 29] Overlaps with [10, 30] Deleting interval [10, 30] Searching for interval [25, 29] No Overlapping Interval
All operations are logarithmic in size, i.e. O(logN), where N is the number of intervals stored in the tree.
We are able to achieve logarithmic worst-case complexity because a red-black tree is used internally which is self-balancing.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Counting Inversions using Ordered Set and GNU C++ PBDS
- Ordered Set and GNU C++ PBDS
- Interval Tree
- How to add reference of an object in Container Classes
- Sum of Interval and Update with Number of Divisors
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Order statistic tree using fenwick tree (BIT)
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Create Balanced Binary Tree using its Leaf Nodes without using extra space
- Convert a Binary Tree into its Mirror Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Tournament Tree (Winner Tree) and Binary Heap
- Check if a binary tree is subtree of another binary tree | Set 1
- Convert a given tree to its Sum Tree
- Binary Tree to Binary Search Tree Conversion
- Check if a given Binary Tree is height balanced like a Red-Black Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.