Skip to content

Commit

Permalink
Merge pull request #47 from theSherwood/master
Browse files Browse the repository at this point in the history
fix: finds the lowest intersecting interval
  • Loading branch information
alexbol99 authored Sep 17, 2023
2 parents 6a99b44 + c7a577c commit 45bcbbf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/classes/intervalTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,12 @@ class IntervalTree {
let curr = node;
while (curr && curr != this.nil_node) {
if (curr.less_than(search_node)) {
if (curr.intersect(search_node) && (!best || curr.less_than(best))) best = curr;
curr = curr.right;
if (curr.intersect(search_node)) {
best = curr;
curr = curr.left;
} else {
curr = curr.right;
}
} else {
if (!best || curr.less_than(best)) best = curr;
curr = curr.left;
Expand Down
7 changes: 7 additions & 0 deletions test/intervalTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ describe('#IntervalTree', function() {
let iterator = tree.iterate([3,8]);
expect(iterator.next().value).to.deep.equal([1,3]);
});
it('Finds the lowest intersecting interval regardless of tree structure', function () {
let tree = new IntervalTree();
let ints = [[6,8],[5,12],[4,7],[1,5]];
for (let int of ints) tree.insert(int);
let iterator = tree.iterate([5,5]);
expect(iterator.next().value).to.deep.equal([1,5]);
});
it('May find first forward interval when there is no intersection', function () {
let tree = new IntervalTree();
let ints = [[6,8],[1,3],[5,12],[1,1],[5,7]];
Expand Down

0 comments on commit 45bcbbf

Please sign in to comment.