binary-search-tree/red-black-tree

Allow to store references and eventually delete them

make-github-pseudonymous-again opened this issue · 3 comments

const node = tree.add(x);
...
tree.unlink(node);

This is currently not possible because, among other potential problems, rotations swap keys of references to save operations.

According to the command rg '\.key.*=.*\.key', the only other place where this happens is in RedBlackTree#_delete.

Note that this requires keeping track of the RedBlackTree#root in all these operations.

PS: We could have used a WeakMap to point to the current node even though keys have been swapped. But this could lead to a two-fold increase of space usage because it prevents garbage collection of unlinked nodes whose keys has been swapped at some point.