alexbol99 / flatten-interval-tree

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

[Feature Request] addition of allowTouching mode #29

Closed henrymcl closed 2 years ago

henrymcl commented 3 years ago

Currenly when [4,5] is checked with a tree [[7,8],[1,4],[11,12],[1,1],[5,7]] for intersection, the program returns true. However in many situations like scheduling, this is seldom considered as intersecting. Activities occur after activities in the same venue and the information that they touch each other could often be perceived as noise.

Upon inspecting the code, I noticed that adding equal signs to the Interval.not_intersect method seems to have worked

/**
     * Predicate returns true if this interval does not intersect other interval
     * @param other_interval
     * @returns {boolean}
     */
    not_intersect(other_interval) {
        return (this.high <= other_interval.low || other_interval.high <= this.low);
    }

It doesn't appear to break other operations like the tree balancing either, but I can't be sure.

alexbol99 commented 2 years ago

Hello @henry132109

Indeed, method not_intersect() used only in search() and does not effect tree balancing. I think the simpliest way to change default behaviour is to override this method in child class:

       class TimeInterval extends Interval {
            not_intersect(other_interval) {
                return (this.high <= other_interval.low || other_interval.high <= this.low);
            }
        }

        const tree = new IntervalTree();
        tree.insert(new TimeInterval(7,8));
        tree.insert(new TimeInterval(1,4));
        tree.insert(new TimeInterval(11,12));
        tree.insert(new TimeInterval(5,7));

        const resp1 = tree.search([4,5]);
        expect(resp1).to.deep.equal([]);

        const resp2 = tree.search([4,11]);
        expect(resp2).to.deep.equal([[5,7],[7,8]]);

I added your case to the test

Thanks, Alex