Skip to content

Commit

Permalink
Resolves #1268
Browse files Browse the repository at this point in the history
  • Loading branch information
dfahlander committed Apr 28, 2021
1 parent 26b5c66 commit 42417a0
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/helpers/rangeset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,39 @@ function rebalance(target: IntervalTreeNode) {
const diff = (target.r?.d || 0) - (target.l?.d || 0);
const r = diff > 1 ? "r" : diff < -1 ? "l" : "";
if (r) {
// Rotate

// Rotate (https://en.wikipedia.org/wiki/Tree_rotation)
//
//
// [OLDROOT]
// [OLDROOT.L] [NEWROOT]
// [NEWROOT.L] [NEWROOT.R]
//
// Is going to become:
//
//
// [NEWROOT]
// [OLDROOT] [NEWROOT.R]
// [OLDROOT.L] [NEWROOT.L]

// * clone now has the props of OLDROOT
// Plan:
// * target must be given the props of NEWROOT
// * target[l] must point to a new OLDROOT
// * target[r] must point to NEWROOT.R
// * OLDROOT[r] must point to NEWROOT.L
const l = r === "r" ? "l" : "r";
extend(target, clone[r]);
extend(clone[r], clone);
clone[r]![r] = target[l];
target[l] = clone[r];
clone[r]!.d = computeDepth(clone[r]!);
// * Make a new oldRoot (clone the clone)
const oldRoot = {...clone};
// * Point it's right side to NEWROOT.L
oldRoot[r] = target[l];
// * Point the NEWROOT.l to OLDROOT
target[l] = oldRoot;
// Recompute depth
oldRoot.d = computeDepth(oldRoot);
}
// Recompute depth
target.d = computeDepth(target);
}

Expand Down

0 comments on commit 42417a0

Please sign in to comment.