tree-multimap-typed
Version:
251 lines (250 loc) • 9.29 kB
JavaScript
"use strict";
/**
* data-structure-typed
*
* @author Pablo Zeng
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
* @license MIT License
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AVLTreeMultiMap = exports.AVLTreeMultiMapNode = void 0;
const avl_tree_1 = require("./avl-tree");
/**
* Node used by AVLTreeMultiMap; stores the key with a bucket of values (array).
* @remarks Time O(1), Space O(1)
* @template K
* @template V
*/
class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
/**
* Create an AVLTreeMultiMap node with a value bucket.
* @remarks Time O(1), Space O(1)
* @param key - Key of the node.
* @param value - Initial array of values.
* @returns New AVLTreeMultiMapNode instance.
*/
constructor(key, value) {
super(key, value);
this.parent = undefined;
this._left = undefined;
this._right = undefined;
}
/**
* Get the left child pointer.
* @remarks Time O(1), Space O(1)
* @returns Left child node, or null/undefined.
*/
get left() {
return this._left;
}
/**
* Set the left child and update its parent pointer.
* @remarks Time O(1), Space O(1)
* @param v - New left child node, or null/undefined.
* @returns void
*/
set left(v) {
if (v) {
v.parent = this;
}
this._left = v;
}
/**
* Get the right child pointer.
* @remarks Time O(1), Space O(1)
* @returns Right child node, or null/undefined.
*/
get right() {
return this._right;
}
/**
* Set the right child and update its parent pointer.
* @remarks Time O(1), Space O(1)
* @param v - New right child node, or null/undefined.
* @returns void
*/
set right(v) {
if (v) {
v.parent = this;
}
this._right = v;
}
}
exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode;
/**
* AVL-tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
* @remarks Time O(1), Space O(1)
* @template K
* @template V
* @template R
*/
class AVLTreeMultiMap extends avl_tree_1.AVLTree {
/**
* Create an AVLTreeMultiMap and optionally bulk-insert items.
* @remarks Time O(N log N), Space O(N)
* @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
* @param [options] - Options for AVLTreeMultiMap (comparator, reverse, map mode).
* @returns New AVLTreeMultiMap instance.
*/
constructor(keysNodesEntriesOrRaws = [], options) {
super([], Object.assign(Object.assign({}, options), { isMapMode: true }));
if (keysNodesEntriesOrRaws) {
this.addMany(keysNodesEntriesOrRaws);
}
}
_createNode(key, value = []) {
return new AVLTreeMultiMapNode(key, this._isMapMode ? [] : value);
}
/**
* Insert a value or a list of values into the multimap. If the key exists, values are appended.
* @remarks Time O(log N + M), Space O(1)
* @param keyNodeOrEntry - Key, node, or [key, values] entry.
* @param [value] - Single value to add when a bare key is provided.
* @returns True if inserted or appended; false if ignored.
*/
add(keyNodeOrEntry, value) {
if (this.isRealNode(keyNodeOrEntry))
return super.add(keyNodeOrEntry);
const _commonAdd = (key, values) => {
if (key === undefined || key === null)
return false;
const _addToValues = () => {
const existingValues = this.get(key);
if (existingValues !== undefined && values !== undefined) {
for (const value of values)
existingValues.push(value);
return true;
}
return false;
};
const _addByNode = () => {
const existingNode = this.getNode(key);
if (this.isRealNode(existingNode)) {
const existingValues = this.get(existingNode);
if (existingValues === undefined) {
super.add(key, values);
return true;
}
if (values !== undefined) {
for (const value of values)
existingValues.push(value);
return true;
}
else {
return false;
}
}
else {
return super.add(key, values);
}
};
if (this._isMapMode) {
return _addByNode() || _addToValues();
}
return _addToValues() || _addByNode();
};
if (this.isEntry(keyNodeOrEntry)) {
const [key, values] = keyNodeOrEntry;
return _commonAdd(key, value !== undefined ? [value] : values);
}
return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
}
/**
* Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
* @remarks Time O(log N), Space O(1)
* @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
* @param value - Value to remove from the bucket.
* @returns True if the value was removed; false if not found.
*/
deleteValue(keyNodeOrEntry, value) {
const values = this.get(keyNodeOrEntry);
if (Array.isArray(values)) {
const index = values.indexOf(value);
if (index === -1)
return false;
values.splice(index, 1);
if (values.length === 0)
this.delete(keyNodeOrEntry);
return true;
}
return false;
}
/**
* Rebuild the tree into a perfectly balanced form using in-order nodes.
* @remarks Time O(N), Space O(N)
* @param [iterationType] - Traversal style to use when constructing the balanced tree.
* @returns True if rebalancing succeeded (tree not empty).
*/
perfectlyBalance(iterationType = this.iterationType) {
const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
const n = nodes.length;
if (n === 0)
return false;
this._clearNodes();
const build = (l, r, parent) => {
if (l > r)
return undefined;
const m = l + ((r - l) >> 1);
const root = nodes[m];
root.left = build(l, m - 1, root);
root.right = build(m + 1, r, root);
root.parent = parent;
const lh = root.left ? root.left.height : -1;
const rh = root.right ? root.right.height : -1;
root.height = Math.max(lh, rh) + 1;
return root;
};
const newRoot = build(0, n - 1, undefined);
this._setRoot(newRoot);
this._size = n;
return true;
}
/**
* Create a new tree by mapping each [key, values] bucket.
* @remarks Time O(N log N), Space O(N)
* @template MK
* @template MV
* @template MR
* @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
* @param [options] - Options for the output tree.
* @param [thisArg] - Value for `this` inside the callback.
* @returns The mapped AVLTree or AVLTreeMultiMap depending on MV; see overloads.
*/
map(callback, options, thisArg) {
const out = this._createLike([], options);
let i = 0;
for (const [k, v] of this)
out.add(callback.call(thisArg, k, v, i++, this));
return out;
}
/**
* (Protected) Create an empty instance of the same concrete class.
* @remarks Time O(1), Space O(1)
* @template TK
* @template TV
* @template TR
* @param [options] - Optional constructor options for the like-kind instance.
* @returns An empty like-kind instance.
*/
_createInstance(options) {
var _a, _b;
const Ctor = this.constructor;
return new Ctor([], Object.assign(Object.assign({}, ((_b = (_a = this._snapshotOptions) === null || _a === void 0 ? void 0 : _a.call(this)) !== null && _b !== void 0 ? _b : {})), (options !== null && options !== void 0 ? options : {})));
}
/**
* (Protected) Create a like-kind instance and seed it from an iterable.
* @remarks Time O(N log N), Space O(N)
* @template TK
* @template TV
* @template TR
* @param iter - Iterable used to seed the new tree.
* @param [options] - Options merged with the current snapshot.
* @returns A like-kind AVLTree built from the iterable.
*/
_createLike(iter = [], options) {
var _a, _b;
const Ctor = this.constructor;
return new Ctor(iter, Object.assign(Object.assign({}, ((_b = (_a = this._snapshotOptions) === null || _a === void 0 ? void 0 : _a.call(this)) !== null && _b !== void 0 ? _b : {})), (options !== null && options !== void 0 ? options : {})));
}
}
exports.AVLTreeMultiMap = AVLTreeMultiMap;