Skip to content

Commit a70d9c6

Browse files
author
AndreyGeonya
committed
doc interval-tree
1 parent f6a0411 commit a70d9c6

1 file changed

Lines changed: 119 additions & 15 deletions

File tree

‎src/data-structures/interval-tree.js‎

Lines changed: 119 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,81 @@
1+
/**
2+
* Interval tree is an ordered tree data structure to hold intervals.
3+
*
4+
* @example
5+
*
6+
* var IT = require('../src/data-structures/interval-tree');
7+
* var intervalTree = new IT.IntervalTree();
8+
*
9+
* intervalTree.add([0, 100]);
10+
* intervalTree.add([101, 200]);
11+
* intervalTree.add([10, 50]);
12+
* intervalTree.add([120, 220]);
13+
*
14+
* console.log(intervalTree.contains(150)); // true
15+
* console.log(intervalTree.contains(250)); // false
16+
* console.log(intervalTree.intersects([210, 310])); // true
17+
* console.log(intervalTree.intersects([310, 320])); // false
18+
*
19+
* @module data-structures/interval-tree
20+
*/
121
(function (exports) {
22+
223
'use strict';
324

4-
function Node(start, end, left, right) {
25+
/**
26+
* Node which describes an interval.
27+
*
28+
* @public
29+
* @constructor
30+
* @param {Number} start Start of the interval.
31+
* @param {Number} end End of the interval.
32+
* @param {Node} left Left child node.
33+
* @param {Node} right Right child node.
34+
*/
35+
exports.Node = function(start, end, left, right) {
36+
/**
37+
* Node interval.
38+
* @member {Array}
39+
*/
540
this.interval = [start, end];
41+
/**
42+
* Max endpoint in subtree which starts from this node.
43+
* @member {Number}
44+
*/
645
this.max = -Infinity;
46+
/**
47+
* Parent node.
48+
* @member {Node}
49+
*/
750
this.parentNode = null;
51+
/**
52+
* Left child node.
53+
* @member {Node}
54+
*/
855
this.left = left;
56+
/**
57+
* Right child node.
58+
* @member {Node}
59+
*/
960
this.right = right;
1061
}
1162

12-
function IntervalTree() {
63+
/**
64+
* Interval tree.
65+
*
66+
* @public
67+
* @constructor
68+
*/
69+
exports.IntervalTree = function() {
70+
/**
71+
* Root node of the tree.
72+
* @member {Node}
73+
*/
1374
this.root = null;
1475
}
1576

1677
function addNode(node, side, interval) {
17-
var child = new Node(interval[0], interval[1]);
78+
var child = new exports.Node(interval[0], interval[1]);
1879
child.parentNode = node;
1980
node[side] = child;
2081
if (node.max < interval[1]) {
@@ -43,9 +104,15 @@
43104
}
44105
}
45106

46-
IntervalTree.prototype.add = function (interval) {
107+
/**
108+
* Add new interval to the tree.
109+
*
110+
* @public
111+
* @param {Array} intreval Array with start and end points of the interval.
112+
*/
113+
exports.IntervalTree.prototype.add = function (interval) {
47114
if (!this.root) {
48-
this.root = new Node(interval[0], interval[1]);
115+
this.root = new exports.Node(interval[0], interval[1]);
49116
return;
50117
}
51118
addHelper(this.root, interval);
@@ -70,7 +137,16 @@
70137
return result;
71138
}
72139

73-
IntervalTree.prototype.contains = function (point) {
140+
/**
141+
* Checks or point belongs to at least one intarval from the tree.<br><br>
142+
* Complexity: O(log N).
143+
*
144+
* @public
145+
* @method
146+
* @param {Number} point Point which should be checked.
147+
* @return {Boolean} True if point belongs to one of the intervals.
148+
*/
149+
exports.IntervalTree.prototype.contains = function (point) {
74150
return contains(point, this.root);
75151
};
76152

@@ -96,7 +172,16 @@
96172
(b[0] <= a[0] && b[1] >= a[0]) || (b[0] <= a[1] && b[1] >= a[1]);
97173
}
98174

99-
IntervalTree.prototype.intersects = function (interval) {
175+
/**
176+
* Checks or interval belongs to at least one intarval from the tree.<br><br>
177+
* Complexity: O(log N).
178+
*
179+
* @public
180+
* @method
181+
* @param {Array} interval Interval which should be checked.
182+
* @return {Boolean} True if interval intersects with one of the intervals.
183+
*/
184+
exports.IntervalTree.prototype.intersects = function (interval) {
100185
return intersectsHelper(interval, this.root);
101186
};
102187

@@ -107,11 +192,26 @@
107192
return 1 + Math.max(heightHelper(node.left), heightHelper(node.right));
108193
}
109194

110-
IntervalTree.prototype.height = function () {
195+
/**
196+
* Returns height of the tree.
197+
*
198+
* @public
199+
* @method
200+
* @return {Number} Height of the tree.
201+
*/
202+
exports.IntervalTree.prototype.height = function () {
111203
return heightHelper(this.root);
112204
};
113205

114-
IntervalTree.prototype.findMax = function (node) {
206+
/**
207+
* Returns node with the max endpoint in subtree.
208+
*
209+
* @public
210+
* @method
211+
* @param {Node} node Root node of subtree.
212+
* @return {Node} Node with the largest endpoint.
213+
*/
214+
exports.IntervalTree.prototype.findMax = function (node) {
115215
var stack = [node],
116216
current, max = -Infinity, maxNode;
117217
while (stack.length) {
@@ -131,7 +231,7 @@
131231
};
132232

133233
// adjust the max value
134-
IntervalTree.prototype._removeHelper =
234+
exports.IntervalTree.prototype._removeHelper =
135235
function (interval, node) {
136236
if (!node) {
137237
return;
@@ -193,11 +293,15 @@
193293
}
194294
};
195295

196-
IntervalTree.prototype.remove = function (interval) {
296+
/**
297+
* Remove interval from the tree.
298+
*
299+
* @public
300+
* @method
301+
* @param {Array} intreval Array with start and end of the interval.
302+
*/
303+
exports.IntervalTree.prototype.remove = function (interval) {
197304
return this._removeHelper(interval, this.root);
198305
};
199306

200-
exports.Node = Node;
201-
exports.IntervalTree = IntervalTree;
202-
203-
}(typeof exports === 'undefined' ? window : exports));
307+
})(typeof window === 'undefined' ? module.exports : window);

0 commit comments

Comments
 (0)