alexbol99 / flatten-interval-tree

Interval binary search tree
MIT License
41 stars 21 forks source link

Error with finding interval #9

Closed bjnsn closed 5 years ago

bjnsn commented 5 years ago

I ran into an issue when using "@flatten-js/interval-tree": "^1.0.6" from NPM.


function setupTreeAndSearch(intervals, searchInterval) {
    let tree = new IntervalTree();

    for (let i=0; i < intervals.length; i++) {
        tree.insert(intervals[i],"val"+i);
    }

    return tree.search(searchInterval);
}

setupTreeAndSearch(
    [[1,1], [1,4], [5,6], [5.5,7], [7,8]],
    [5.5, 5.7] 
); 
// ["val2", "val3"]
// Correct!

setupTreeAndSearch(
    [[1,1], [1,4], [5,6], [6,7], [7,8]],
    [5.5, 5.7]
);
// []
// Incorrect - expected ["val2"]

The right branch of this.tree_search_interval(...) called from tree.search(...) is excluding the matching (right) branch .

As it is currently running the right conditional is false because, node.not_intersect_right_subtree(...) does not check the left child branch in this line:

let low = this.right.max.low ? this.right.max.low : this.right.item.key.low;

Note: this.right.max is a number, not an object when the line above is run. Perhaps it should be?

Thoughts?

alexbol99 commented 5 years ago

Hi Brett @bjnsn Bug fixed in version 1.0.7 Thank you for cooperation. Alex

bjnsn commented 5 years ago

Fabulous - thanks Alex!