This C++ code implements an Interval Tree data structure for efficient interval searching. The Interval Tree is a binary search tree that stores intervals and allows for quick retrieval of intervals that overlap with a given query interval.
The code defines a class IntervalTree
, which has methods for inserting intervals, searching for overlapping intervals, and printing the tree structure. The intervals are represented by a nested Node
class, which is the building block of the Interval Tree.
IntervalTree()
: Constructor for creating an empty Interval Tree.void insertInterval(int low, int high)
: Inserts a new interval into the tree.void searchInterval(int low, int high)
: Searches for intervals that overlap with the given query interval.void print()
: Prints the entire tree structure using Depth-First Search (DFS).
Node* insertInterval(Node* node, int low, int high)
: Private method for recursively inserting intervals into the tree.bool intersection(Node* x, int low, int high)
: Private method to check if there is an intersection between two intervals.Node* searchInterval(Node* node, int low, int high)
: Private method for recursively searching for overlapping intervals.void dfs(Node* root)
: Private method for Depth-First Search traversal.
- Run the following command to build the project:
cmake --build build -j 12
- Run the following command to run the tests:
GTEST_COLOR=1 ctest --test-dir build --output-on-failure -j12
IntervalTree intervalTree;
// Insert intervals
intervalTree.insertInterval(10, 20);
intervalTree.insertInterval(30, 40);
// Search for overlapping intervals
intervalTree.searchInterval(15, 25);
// Print the tree structure
intervalTree.print();
// git submodule update --init --recursive
updates the submodule, already fixed with script so you dont have to write it