×

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

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 minutes read.

Primitive Data Structure in C

The data structure is a logical or mathematical model for organizing and structuring the main memory or elements. We can classify the data structures in two ways one is primitive, and...

10 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.

Difference between complete and full binary tree

As we all know that the  binary tree is a tree it contains one or two children at each other node. It contains two children's nodes in the Binary tree. The...

6 minutes read.

Delete the Middle element of the Linked List in C

Delete the Middle element of the Linked List in C This article has given a singly linked list and will delete the middle element of the given linked list. Example:  The given...

3 minutes read.

Implementation of stack

Implementation of stack: The stack can be implemented in two ways: using array and using a linked list. The pop and push operations in the array are simpler than the...

3 minutes read.

Data Structure Infix to Postfix Conversion

Infix to Postfix Conversion The infix expression is easy to read and write by humans. In present time, we use the infix expression in our daily life but the computers are...

4 minutes read.

Bookshop management system using file handling in C++

We see different software in every hospitals or library to manage their database. It is very important to store organization’s data. So we use this software. Now we are going...

5 minutes read.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

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.

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.

Cycle sort

Cycle sort is an examination arranging calculation which powers exhibit to be figured into the quantity of cycles where every one of them can be pivoted to create an arranged...

5 minutes read.

Priority Queue in Data Structure

Priority Queue A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served...

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

Identical Linked Lists

Identical Linked Lists In this problem, we have given two linked lists, and we need to check whether the given linked lists are identical or not. Identical means they have the...

4 minutes read.

Write Main Difference Between Tree and Graph in Data Structures

Graph: The graph has two sets, which are considered V and E. These vertices are also called nodes, and edges are referred to as arcs connecting any two nodes in a...

4 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Convert Binary Tree into a Threaded Binary Tree

Implementation /*Writing a C++ program that will help us change the binary tree into a threaded binary tree and help us transform. */ #include <bits/stdc++.h> using namespace std; /*Creating the structure of a node...

11 minutes read.

Left View of Binary Tree

Implementation // creating a C++ program to print the Left view of the binary tree. #include <bits/stdc++.h> using namespace std; struct Nod { int record; struct Nod *Lft, *Rt; }; // creating a utility function that will eventually help...

4 minutes read.

Delete N nodes after M nodes of a linked list

Delete N nodes after M nodes of a linked list In this problem, we have given a linked list and two integers M and N. We need to traverse the linked...

3 minutes read.