From a8c79d192ae15ebe95e8a9d9e4d48b24a3f30977 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 8 Jun 2016 21:20:36 +0300 Subject: [PATCH] Add the ability to remove interval from intervaltree. --- vendor/intervaltree/core.js | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/vendor/intervaltree/core.js b/vendor/intervaltree/core.js index fc1a9a22b..438d48b22 100644 --- a/vendor/intervaltree/core.js +++ b/vendor/intervaltree/core.js @@ -54,6 +54,12 @@ goog.scope(function() { return this; } + remove(item) { + const interval = makeInterval(item) + this.root = remove(this.root, interval); + return this; + } + contains(point) { assert(goog.isNumber(point)); assert(this.root !== null); @@ -109,6 +115,50 @@ goog.scope(function() { return root; } + function remove(root, interval) { + if (root === null) { + return root; + } else if (root.interval.start === interval.start && + root.interval.end === interval.end) { + if (root.left === null) { + return root.right; + } else if (root.right === null) { + return root.left; + } else { + const result = removeLeftMost(root.right); + const newroot = result[0]; + const newright = result[1]; + + newroot.left = root.left; + newroot.right = newright; + return newroot; + } + } else { + root.left = remove(root.left, interval); + root.right = remove(root.right, interval); + + root.height = calculateHeight(root); + root.maxEnd = calculateMaxEnd(root); + + return root; + } + } + + function removeLeftMost(root) { + if (root === null) { + return [null, null]; + } else if (root.left === null) { + return [root, root.right]; + } else { + const result = removeLeftMost(root.left); + const newroot = result[0]; + const newleft = result[1]; + root.left = newleft; + + return [newroot, root]; + } + } + function calculateMaxEnd(node) { const left = node.left ? node.left.maxEnd : 0; const right = node.right ? node.right.maxEnd: 0;