interval tree in JavaScript (source is written in CoffeeScript)
improves previous interval-tree
more maintenanceability with CoffeeScript OOP.
more robustness with mocha, CircleCI
latest API documentation Page (YUIDoc)
$ npm install interval-tree2
var IntervalTree = require('interval-tree2');
when using in web, use dist/interval-tree.js
in this module.
<script src="dist/interval-tree.js"></scirpt>
var itree = new IntervalTree(300); // 300 : the center of the tree
itree.add(22, 56, 'foo'); // 'foo' is the id of the interval data
itree.add(44, 199, 'bar'); // 'bar' is the id of the interval data
itree.add(1, 38); // id is automatically set when not given
var intervals = itree.search(103);
or
var intervals = itree.pointSearch(103);
intervals.forEach(function(interval) {
console.log(interval.start); // overlapped interval start position
console.log(interval.end); // overlapped interval end position
console.log(interval.id); // id of the overlapped interval
});
var intervals2 = itree.search(103, 400);
or
var intervals2 = itree.rangeSearch(103, 400);
intervals2.forEach(function(interval) {
console.log(interval.start); // overlapped interval start position
console.log(interval.end); // overlapped interval end position
console.log(interval.id); // id of the overlapped interval
});
itree.remove('foo');