Coder Social home page Coder Social logo

interval-tree's Introduction

Interval Tree

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.

Implementation Details

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.

Class: IntervalTree

Public Methods:

  1. IntervalTree(): Constructor for creating an empty Interval Tree.
  2. void insertInterval(int low, int high): Inserts a new interval into the tree.
  3. void searchInterval(int low, int high): Searches for intervals that overlap with the given query interval.
  4. void print(): Prints the entire tree structure using Depth-First Search (DFS).

Private Methods:

  1. Node* insertInterval(Node* node, int low, int high): Private method for recursively inserting intervals into the tree.
  2. bool intersection(Node* x, int low, int high): Private method to check if there is an intersection between two intervals.
  3. Node* searchInterval(Node* node, int low, int high): Private method for recursively searching for overlapping intervals.
  4. void dfs(Node* root): Private method for Depth-First Search traversal.

To start the project:

  1. Run the following command to build the project: cmake --build build -j 12

TEST

  1. Run the following command to run the tests: GTEST_COLOR=1 ctest --test-dir build --output-on-failure -j12

Example Usage:

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

interval-tree's People

Contributors

abdallhfattah avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.