Skip to content

Instantly share code, notes, and snippets.

View podrezo's full-sized avatar

Petro Podrezo podrezo

View GitHub Profile
@podrezo
podrezo / interval_tree.js
Created August 24, 2020 23:05
An interval tree written in JS to find the number of intersecting intervals at a given point
// https://en.wikipedia.org/wiki/Interval_tree
export default class IntervalTree {
// intervals is a two dimensional array of "from" and "to" values
// the values can be of any type. If they can directly be compared
// using greater/lesser than operators then a compare function does
// not need to be supplied
constructor(intervals) {
this.allIntervals = intervals.map(interval => new Interval(interval[0], interval[1]));
this.root = new IntervalTreeNode(this.allIntervals);