Node.js module implementation of Segment Tree data structure for storing intervals, or segments. It allows querying which of the stored segments contain a given point.
var stree = require('s-tree');
stree(function(tree) {
tree
.push(5, 10, 'foo')
.push(8, 14, 'bar')
.push(12, 16)
.build();
tree.query({ start: 10, end: 12 }, function(intervals) {
...
});
});npm install s-tree
Store a interval (start, end) with a optional data parameter.
startStart point of interval (number)endEnd point of interval (number)dataOptinal key value (e.g. id, label or even an object) for future reference (object)
Returns object which is a reference to the tree itself` (Chain Pattern)
Example:
var stree = require('s-tree');
stree(function(tree) {
...
tree.push(5, 10);
tree.push(5, 10, 'foo');
...
});Build the segment tree data structure.
Returns object which is a reference to the tree itself` (Chain Pattern)
var stree = require('s-tree');
stree(function(tree) {
...
tree.build();
...
});Query for intervals which overlap a given endpoint (start, end).
options:startStart point of interval (number)endEnd point of interval (number)endpointsoptional parameter to indicate whether or not endpoints should be included on interval comparison (boolean, default: true)concurrencyoptional parameter to indicate whether or not the query should look only for concurrent intervals (boolean, default: false)
callbackThe callback function which provides the queried intervals (e.g. function(intervals) {...})
Returns number of intervals
Example:
var stree = require('s-tree');
stree(function(tree) {
...
tree.query({ start: 5, end: 10 }, function(intervals) {
...
});
tree.query({ start: 5, end: 10, endpoints: false }, function(intervals) {
...
});
tree.query({ start: 5, end: 10, concurrency: true }, function(intervals) {
...
});
var numberOfIntrevals = tree.query({ start: 5, end: 10 });
...
});Query for array of intervals which overlap the given endpoints ([start], [end]).
options:startStart points of intervals (array)endEnd points of intervals (array)endpointsoptional parameter to indicate whether or not endpoints should be included on interval comparison (boolean, default: true)concurrencyoptional parameter to indicate whether or not the query should look only for concurrent intervals (boolean, default: false)
callbackThe callback function which provides the queried intervals (e.g. function(intervals) {...})
Returns number of intervals
Example:
var stree = require('s-tree');
stree(function(tree) {
...
tree.query({ start: [5, 10], end: [8, 14] }, function(intervals) {
...
});
tree.query({ start: [5, 10], end: [8, 14], endpoints: false }, function(intervals) {
...
});
tree.query({ start: [5, 10], end: [8, 14], concurrency: true }, function(intervals) {
...
});
var numberOfIntrevals = tree.query({ start: [5, 10], end: [8, 14] });
...
});Query for intervals which overlaps a given point (point).
options:pointPoint where intervals overlap (number)endpointsoptional parameter to indicate whether or not endpoints should be included on interval comparison (boolean, default: true)concurrencyoptional parameter to indicate whether or not the query should look only for concurrent intervals (boolean, default: false)
callbackThe callback function which provides the queried intervals (e.g. function(intervals) {...})
Returns number of intervals
Example:
var stree = require('s-tree');
stree(function(tree) {
...
tree.query({ point: 5 }, function(intervals) {
...
});
tree.query({ point: 5, endpoints: false }, function(intervals) {
...
});
tree.query({ point: 5, concurrency: true }, function(intervals) {
...
});
var numberOfIntrevals = tree.query({ point: 5 });
...
});Query for intervals which overlaps the given points (points).
options:pointsPoints where intervals overlap (array)endpointsoptional parameter to indicate whether or not endpoints should be included on interval comparison (boolean, default: true)concurrencyoptional parameter to indicate whether or not the query should look only for concurrent intervals (boolean, default: false)
callbackThe callback function which provides the queried intervals (e.g. function(intervals) {...})
Returns number of intervals
Example:
var stree = require('s-tree');
stree(function(tree) {
...
tree.query({ points: [5, 10, 25] }, function(intervals) {
...
});
tree.query({ points: [5, 10, 25], endpoints: false }, function(intervals) {
...
});
tree.query({ points: [5, 10, 25], concurrency: true }, function(intervals) {
...
});
var numberOfIntrevals = tree.query({ points: [5, 10, 25] });
...
});Remove intervals from the data structure.
Returns object which is a reference to the tree itself` (Chain Pattern)
Example:
var stree = require('s-tree');
stree(function(tree) {
...
tree.clear();
...
});Indicates whether or not there are intervals in the data structure.
Returns Boolean
Example:
var stree = require('s-tree');
stree(function(tree) {
...
if (tree.hasIntervals()) {...};
...
});