C++ Program to Implement Interval Tree

This is a C++ Program to implement interval tree. In computer science, an interval tree is an ordered tree data structure to hold intervals. Specifically, it allows one to efficiently find all intervals that overlap with any given interval or point. It is 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. A similar data structure is the segment tree.

Here is source code of the C++ Program to Implement Interval Tree. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Interval
  6. {
  7.         int low, high;
  8. };
  9.  
  10. struct ITNode
  11. {
  12.         Interval *i; // 'i' could also be a normal variable
  13.         int max;
  14.         ITNode *left, *right;
  15. };
  16.  
  17. // A utility function to create a new Interval Search Tree Node
  18. ITNode * newNode(Interval i)
  19. {
  20.     ITNode *temp = new ITNode;
  21.     temp->i = new Interval(i);
  22.     temp->max = i.high;
  23.     temp->left = temp->right = NULL;
  24. };
  25.  
  26. // A utility function to insert a new Interval Search Tree Node
  27. // This is similar to BST Insert.  Here the low value of interval
  28. // is used tomaintain BST property
  29. ITNode *insert(ITNode *root, Interval i)
  30. {
  31.     // Base case: Tree is empty, new node becomes root
  32.     if (root == NULL)
  33.         return newNode(i);
  34.  
  35.     // Get low value of interval at root
  36.     int l = root->i->low;
  37.  
  38.     // If root's low value is smaller, then new interval goes to
  39.     // left subtree
  40.     if (i.low < l)
  41.         root->left = insert(root->left, i);
  42.  
  43.     // Else, new node goes to right subtree.
  44.     else
  45.         root->right = insert(root->right, i);
  46.  
  47.     // Update the max value of this ancestor if needed
  48.     if (root->max < i.high)
  49.         root->max = i.high;
  50.  
  51.     return root;
  52. }
  53.  
  54. // A utility function to check if given two intervals overlap
  55. bool doOVerlap(Interval i1, Interval i2)
  56. {
  57.     if (i1.low <= i2.high && i2.low <= i1.high)
  58.         return true;
  59.     return false;
  60. }
  61.  
  62. // The main function that searches a given interval i in a given
  63. // Interval Tree.
  64. Interval *intervalSearch(ITNode *root, Interval i)
  65. {
  66.     // Base Case, tree is empty
  67.     if (root == NULL)
  68.         return NULL;
  69.  
  70.     // If given interval overlaps with root
  71.     if (doOVerlap(*(root->i), i))
  72.         return root->i;
  73.  
  74.     // If left child of root is present and max of left child is
  75.     // greater than or equal to given interval, then i may
  76.     // overlap with an interval is left subtree
  77.     if (root->left != NULL && root->left->max >= i.low)
  78.         return intervalSearch(root->left, i);
  79.  
  80.     // Else interval can only overlap with right subtree
  81.     return intervalSearch(root->right, i);
  82. }
  83.  
  84. void inorder(ITNode *root)
  85. {
  86.     if (root == NULL)
  87.         return;
  88.  
  89.     inorder(root->left);
  90.  
  91.     cout << "[" << root->i->low << ", " << root->i->high << "]" << " max = "
  92.             << root->max << endl;
  93.  
  94.     inorder(root->right);
  95. }
  96.  
  97. int main(int argc, char **argv)
  98. {
  99.  
  100.     Interval ints[] = { { 15, 20 }, { 10, 30 }, { 17, 19 }, { 5, 20 },
  101.             { 12, 15 }, { 30, 40 } };
  102.     int n = sizeof(ints) / sizeof(ints[0]);
  103.     ITNode *root = NULL;
  104.     for (int i = 0; i < n; i++)
  105.         root = insert(root, ints[i]);
  106.  
  107.     cout << "In-order traversal of constructed Interval Tree is\n";
  108.     inorder(root);
  109.  
  110.     Interval x = { 6, 7 };
  111.  
  112.     cout << "\nSearching for interval [" << x.low << "," << x.high << "]";
  113.     Interval *res = intervalSearch(root, x);
  114.     if (res == NULL)
  115.         cout << "\nNo Overlapping Interval";
  116.     else
  117.         cout << "\nOverlaps with [" << res->low << ", " << res->high << "]";
  118. }

Output:

$ g++ IntervalTree.cpp
$ a.out
 
In-order 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]
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.