From 7dc55f514a6724d85b108d6d29515188189af5af Mon Sep 17 00:00:00 2001 From: samaingw Date: Sun, 3 Dec 2017 14:53:45 +0100 Subject: [PATCH 1/4] iterative BST building --- static-interval-tree.js | 144 ++++++++++++++++++++++++++++++---------- 1 file changed, 110 insertions(+), 34 deletions(-) diff --git a/static-interval-tree.js b/static-interval-tree.js index 329cec40..e6b2688c 100644 --- a/static-interval-tree.js +++ b/static-interval-tree.js @@ -56,49 +56,125 @@ function buildBST( low, high ) { - var mid = (low + (high - low) / 2) | 0, - midMinusOne = ~-mid, - midPlusOne = -~mid; + var dataStack = new Array(); // augmentation stack, no more than h+1 augmentations + var controlStack = new Array(); // tree walking control stack, no more than h+1 booleans + var tempStack = new Array(); // miscellaenous variables stack, no more than h (i, low, high) - var current = sortedIndices[mid]; - tree[i] = current + 1; + var mid = Infinity, + midMinusOne = Infinity, + midPlusOne = Infinity; - var end = endGetter ? endGetter(intervals[current]) : intervals[current][1]; + var current = Infinity; - var left = i * 2 + 1, - right = i * 2 + 2; + var end = -Infinity; + + var left = Infinity, + right = Infinity; var leftEnd = -Infinity, rightEnd = -Infinity; - if (low <= midMinusOne) { - leftEnd = buildBST( - intervals, - endGetter, - sortedIndices, - tree, - augmentations, - left, - low, - midMinusOne - ); - } + var augmentation = -Infinity; - if (midPlusOne <= high) { - rightEnd = buildBST( - intervals, - endGetter, - sortedIndices, - tree, - augmentations, - right, - midPlusOne, - high - ); - } + var goingDeeper = true, + leftDone = false; + + while (!(controlStack.length === 0 && leftDone)) { + + mid = (low + (high - low) / 2) | 0; + midMinusOne = ~-mid; + midPlusOne = -~mid; + + current = sortedIndices[mid]; + tree[i] = current + 1; + + end = endGetter ? endGetter(intervals[current]) : intervals[current][1]; + + left = i * 2 + 1; + right = i * 2 + 2; + + if (goingDeeper) { + dataStack.push(end); +// console.log('datastack' + dataStack); + if (low <= midMinusOne) {// left needs to be done, then right + goingDeeper = true; + + leftDone = false; + controlStack.push(leftDone); - var augmentation = Math.max(end, leftEnd, rightEnd); + tempStack.push(low); + tempStack.push(high); + tempStack.push(i); + i = left; + high = midMinusOne; +// controlStack.push(i) + + // tree[left] = result[0]; + //leftEnd = result[1]; + } + else if (midPlusOne <= high) { // no left part just right part + goingDeeper = true; + + leftDone = true; + controlStack.push(leftDone); + + tempStack.push(low); + tempStack.push(high); + tempStack.push(i); + + i = right; + low = midPlusOne; + } + else { // search leaf, time to go up + goingDeeper = false; + + i = tempStack.pop(); + high = tempStack.pop(); + low = tempStack.pop(); + } + } + else { + leftDone = controlStack.pop(); + if (leftDone) {// Follow the movement: making the post-recursion actions, and going up ! + goingDeeper = false; + + rightEnd = -Infinity; + leftEnd = -Infinity; + + if (midPlusOne <= high) { + rightEnd = dataStack.pop(); + } + if (low <= midMinusOne) { + leftEnd = dataStack.pop(); + } + end = dataStack.pop(); + + augmentation = Math.max(end, leftEnd, rightEnd); + dataStack.push(augmentation); + + i = tempStack.pop(); + high = tempStack.pop(); + low = tempStack.pop(); + + } + else { //need to do right ! + goingDeeper = true; + + leftDone = true; + controlStack.push(leftDone); + + tempStack.push(low); + tempStack.push(high); + tempStack.push(i); + + i = right; + low = midPlusOne; + } + } + } + +// console.log(dataStack, tree); var augmentationPointer = current; if (augmentation === leftEnd) @@ -107,7 +183,7 @@ function buildBST( augmentationPointer = augmentations[tree[right] - 1]; augmentations[current] = augmentationPointer; - +// console.log(augmentation, dataStack); return augmentation; } From 5a598dad0d40592f2e219d69da09dd6dbe889695 Mon Sep 17 00:00:00 2001 From: samaingw Date: Sun, 3 Dec 2017 14:55:41 +0100 Subject: [PATCH 2/4] comments --- static-interval-tree.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/static-interval-tree.js b/static-interval-tree.js index e6b2688c..0061bde6 100644 --- a/static-interval-tree.js +++ b/static-interval-tree.js @@ -95,7 +95,6 @@ function buildBST( if (goingDeeper) { dataStack.push(end); -// console.log('datastack' + dataStack); if (low <= midMinusOne) {// left needs to be done, then right goingDeeper = true; @@ -108,10 +107,6 @@ function buildBST( i = left; high = midMinusOne; -// controlStack.push(i) - - // tree[left] = result[0]; - //leftEnd = result[1]; } else if (midPlusOne <= high) { // no left part just right part goingDeeper = true; @@ -174,7 +169,6 @@ function buildBST( } } -// console.log(dataStack, tree); var augmentationPointer = current; if (augmentation === leftEnd) @@ -183,7 +177,6 @@ function buildBST( augmentationPointer = augmentations[tree[right] - 1]; augmentations[current] = augmentationPointer; -// console.log(augmentation, dataStack); return augmentation; } From 6776be1ca926a91bcd6140f3c10a5b4c29490fcd Mon Sep 17 00:00:00 2001 From: samaingw Date: Mon, 4 Dec 2017 14:45:05 +0100 Subject: [PATCH 3/4] Bugfix: augmentations not properly updated during BST building --- static-interval-tree.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/static-interval-tree.js b/static-interval-tree.js index 0061bde6..e409fa17 100644 --- a/static-interval-tree.js +++ b/static-interval-tree.js @@ -147,6 +147,14 @@ function buildBST( augmentation = Math.max(end, leftEnd, rightEnd); dataStack.push(augmentation); + var augmentationPointer = current; + + if (augmentation === leftEnd) + augmentationPointer = augmentations[tree[left] - 1]; + else if (augmentation === rightEnd) + augmentationPointer = augmentations[tree[right] - 1]; + + augmentations[current] = augmentationPointer; i = tempStack.pop(); high = tempStack.pop(); From 1875639912d41e80330a3713f4e29b4beb766a2a Mon Sep 17 00:00:00 2001 From: samaingw Date: Mon, 4 Dec 2017 15:17:08 +0100 Subject: [PATCH 4/4] syntax correctness --- static-interval-tree.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/static-interval-tree.js b/static-interval-tree.js index e409fa17..8ca34556 100644 --- a/static-interval-tree.js +++ b/static-interval-tree.js @@ -74,7 +74,8 @@ function buildBST( var leftEnd = -Infinity, rightEnd = -Infinity; - var augmentation = -Infinity; + var augmentation = -Infinity, + augmentationPointer = Infinity; var goingDeeper = true, leftDone = false; @@ -147,7 +148,8 @@ function buildBST( augmentation = Math.max(end, leftEnd, rightEnd); dataStack.push(augmentation); - var augmentationPointer = current; + + augmentationPointer = current; if (augmentation === leftEnd) augmentationPointer = augmentations[tree[left] - 1]; @@ -177,7 +179,7 @@ function buildBST( } } - var augmentationPointer = current; + augmentationPointer = current; if (augmentation === leftEnd) augmentationPointer = augmentations[tree[left] - 1];