×

Interval Tree

Interval Tree

Interval Tree: The concept is to increase a Binary Search Tree self-balancing such as Red Black Tree, and AVL Tree, so that every feature can be completed in time O(Logn).

Each Interval Tree node stores information following.

  • I: An interval representing a pair [low, high]
  • max: Maximum high value in a node-rooted subtree.

The low interval value is used as the key to preserve order within BST.

Insert and delete actions that are used in BST self-balancing are just like insert and delete operations.  

Interval Tree

The key operation is to search for an interval that overlaps. Following is the new algorithm for an overlapping interval x in a root-rooted Interval tree.

1) If x overlaps with an interval of the root, return interval of the root.

2) If left root child is not empty and the limit in the left child is empty is higher than the low value of x, recur for the child left

3) Similar recurrence for the right child.

Why does the Algorithm above work?

Let the query interval be x. We need to prove this for two cases to follow.

Case 1: One of the following must be valid when we go to the correct subtree.

A) The right subtree overlaps: This is fine, as we need to return one overlapping interval.

B) In either subtree, there is no overlap: we go to the right subtree only if either the left subtree is NULL or the left maximum value is lower than x.low. So, the interval in the left subtree cannot be present.

Case 2: One of the following must be true when we go to the left subtree.

A) The left subtree overlaps: This is fine, as we need to return one overlapping interval.

B) In either subtree, there is no overlap: this is the most important part. We need to consider the facts that follow.

  • We went to the left subtree, because in the left subtree x.low <= max
  • Max in the left subtree is one of the intervals in the left subtree, let's say [a, max].
  • Since x does not overlap with any node in the left x.low subtree, it must be lower than 'a.'
  • All nodes in BST are ordered by low value, so the low value of all nodes in the right subtree must be higher than 'a.'
  • We can say from the above two facts that all intervals in the right subtree are of a low value greater than x.low. So x in the right subtree cannot overlap with any interval.

C++ establishment of Interval Tree follows. Basic BST insert operation is used to keep it simple in implementation. Ideally, this should be AVL Tree insertion or Red-Black Tree insertion.

#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 ITNode
{
    Interval *k;  // 'k' could also be a normal variable
    int max;
    ITNode *left, *right;
};
// A utility function to create a new Interval Search Tree Node
ITNode * newNode(Interval k)
{
    ITNode *temp = new ITNode;
    temp->k = new Interval(k);
    temp->max = k.high;
    temp->left = temp->right = NULL;
    return temp;
};
ITNode *insert(ITNode *root, Interval k)
{
    // Base case: Tree is empty, new node becomes root
    if (root == NULL)
        return newNode(k);
    // Get low value of interval at root
    int l = root->k->low;
    if (k.low < l)
        root->left = insert(root->left, k);
    // Else, new node goes to right subtree.
    else
        root->right = insert(root->right, k);
    // Update the max value of this ancestor if needed
    if (root->max < k.high)
        root->max = k.high;
    return root;
}
bool doOVerlap(Interval j1, Interval j2)
{
    if (j1.low <= j2.high && j2.low <=j1.high)
        return true;
    return false;
}
Interval *overlapSearch(ITNode *root, Interval k)
{
    // Base Case, tree is empty
    if (root == NULL) return NULL;
    // If given interval overlaps with root
    if (doOVerlap(*(root->k), k))
        return root->k;
    // 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 >= k.low)
        return overlapSearch(root->left, k);
    // Else interval can only overlap with right subtree
    return overlapSearch(root->right, k);
}
void inorder(ITNode *root)
{
    if (root == NULL) return;
    inorder(root->left);
    cout << "[" << root->k->low << ", " << root->k->high << "]"
         << " max = " << root->max << endl;
    inorder(root->right);
}
// Driver program to test above functions
int main()
{
    // Let us create interval tree shown in above figure
    Interval ints[] = {{20, 25}, {15, 60}, {17, 19},
        {4, 20}, {13, 15}, {35, 40}
    };
    int n = sizeof(ints)/sizeof(ints[0]);
    ITNode *root = NULL;
    for (int t = 0; t < n; t++)
        root = insert(root, ints[t]);
    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 == NULL)
        cout << "\nNo Overlapping Interval";
    else
        cout << "\nOverlaps with [" << res->low << ", " << res->high << "]";
    return 0;
}

Output:

Interval Tree

Related Topics

Bubble Sort vs Heap Sort

In this article, we are going to compare the two most common sorting techniques, Bubble Sort and Heap sort. Before discussing their differences, let us first discuss the idea of...

7 minutes read.

Heap Sort in Data Structure

Heap Sort: Heap Sort is very useful and efficient sorting algorithm in data structure. We can say it is a comparison base sorting algorithm, similar sort where we will find...

2 minutes read.

Queue Data Structure

Queue in DS: The queue is a non-primitive and linear data structure. It works on the principle of FIFO (First In First Out). That is, the element that is added...

4 minutes read.

Binary Tree in Data Structures

What is a Binary Tree in Data Structures? The term binary itself means bi, which implies two of anything. So very clearly, we know we present the trees in the form...

6 minutes read.

Deletion in Binary Search Tree

Implementation #include <iostream> using namespace std; struct _nod {   int ky;   struct _nod *Lft, *Rt; }; // Creating a node in the binary tree. struct _nod *nw_nod(int Itm) {   struct _nod *temp = (struct _nod *)malloc(sizeof(struct...

4 minutes read.

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

Queue operations in Data Structure

Queue - Queue is a linear data structure or first in first out data structure means the first element added in the queue will be removed first and the last...

7 minutes read.

Lowest Common Ancestor in a Binary Tree

The lowest node in the tree that contains both n1 and n2 as descendants is the lowest common ancestor (LCA), and n1 and n2 are the nodes for which we...

11 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Remove duplicates from an unsorted Linked List

Remove duplicates from an unsorted Linked List This article will explain how we can remove duplicates from unsorted linked lists. Here we have given an unsorted singly linked list and will...

3 minutes read.

Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from...

3 minutes read.

Reverse the Singly Linked List in C

Reverse the Singly Linked List in C This article has given a singly linked list and will reverse the linked list by changing the links between nodes. Example:                         Input:  2 -> 4...

3 minutes read.

Recaman’s Sequence

Recamán's succession repeat connection in arithmetic and software engineering. Since its components are obviously connected with the past components, they are as often as possible characterized utilizing recursion. It takes its...

4 minutes read.

Breadth First Search

Breadth First Search Breadth first search is a graph traversing algorithm. In this, we start traversing from the source node or any selected node and traverse the graph layer by layer....

6 minutes read.

Circular Linked List

Circular Linked List A circular linked list where all nodes are connected to their next node and last node is connected to the starting node or we can say all nodes...

5 minutes read.

Height of a binary tree

The height of a binary tree is generally defined as the height or length of the root _nod in the entire binary tree. In simple words, the height of a...

4 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.

Equal Sum

Find an element in array such that the sum of left array is equal to the sum of right array You have been given an array of numbers. You have to...

4 minutes read.

Horizontal and Vertical Scaling

Being a software engineer, you would have designed a website or application and deployed it on any server. Imagine that the developed application starts getting popular, and many users engage...

6 minutes read.

What is a Height-Balanced Tree in Data Structure

A height-balanced tree is a type of binary tree. If the absolute difference between the heights of the left and right subtree is less than or equal to 1, then...

6 minutes read.