×

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

Singly Linked list

Singly Linked list A singly linked list is a kind of linked list which is unidirectional. If we talk about singly linked list, then we can say it can be traversed...

3 minutes read.

What is Skewed Binary Tree

To understand the skewed binary tree, we must first understand the concept of a binary tree. A binary is generally the one in which every single node has two further...

3 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.

Check if a Singly Linked List is Palindrome

Check if a Singly Linked List is Palindrome In this section, we have given a singly linked list, and we need to check whether the given list is a palindrome. Example:           1...

3 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.

Object-Oriented Analysis and Design

While designing a system, one should know all the requirements or needs of the plan beforehand, and to do so, we should use a systematic approach to analyze the goal...

3 minutes read.

Delete nodes from the linked list which have a greater value on the right side

Delete nodes from the linked list which have a greater value on the right side In this problem, we have given a singly linked list, and we need to remove all...

3 minutes read.

Huffman tree in Data Structures

The Huffman trees in the field of data structures are pretty impressive in their work. They are generally treated as the binary tree, which is linked with the least external...

6 minutes read.

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.

Binary Search Tree

Binary Search Tree: A binary search tree is a type of tree in which every node is organized in the sorted order. It is also called an ordered binary tree. Properties...

4 minutes read.

Convert a Binary Tree into a Binary Search Tree

Implementation #include <stdio.h>   #include <stdlib.h>       //creating a node of the binary tree.  struct __nod{       int record;       struct __nod *Lft;       struct __nod *Rt;   };       // presenting the root of the binary tree.   struct...

5 minutes read.

Applications of Different Linked Lists in Data Structure

What is a Linked list? A linked list is a data structure that consists of a sequence of elements, where each containing a reference or ("link") to the next element in...

5 minutes read.

Number of visible boxes putting one inside another

You have given one array, which consists of values which represent the sizes of different boxes. We can put one box inside another if the size of the outside box...

3 minutes read.

Convert Sorted List to Binary Search Tree

Implementation // creating the C++ implementation of the following approach: - #include <bits/stdc++.h> using namespace std; /* Create the link list node and see its implementation. */ class L__Nod { public: int record; L__Nod* next; }; /* constructing a new binary...

15 minutes read.

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known...

3 minutes read.

Data Structure Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

Introduction to Arrays

What exactly is an array? A group of related data pieces stored in contiguous memory regions is referred to as an array. It is the most basic data structure in which...

5 minutes read.

Vertical Order Traversal of Binary Tree

Implementation #include <iostream> #include <vector> #include <map> using namespace std; // representing the primary model of a binary tree node. struct _nod { int ky; _nod *Lft, *Rt; }; // establishing a new function representing the new binary tree node. struct _nod*...

5 minutes read.

Heap Data Structure

In this article, we will learn in detail about Heap (Min heap and Max heap). Before going to the main topics, let’s have a look at what is complete binary...

19 minutes read.

Insertion sort

Insertion sort is a simple sorting technique. It is best suited for small data sets, but it does not suitable for large data sets. In this technique, we pick an...

4 minutes read.