min-heap-typed 1.42.3 → 1.42.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/.prettierignore +1 -1
  2. package/README.md +1 -1
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +2 -2
  4. package/dist/data-structures/binary-tree/avl-tree.js +5 -3
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +56 -52
  6. package/dist/data-structures/binary-tree/binary-tree.js +115 -53
  7. package/dist/data-structures/binary-tree/bst.d.ts +42 -15
  8. package/dist/data-structures/binary-tree/bst.js +77 -21
  9. package/dist/data-structures/binary-tree/index.d.ts +1 -1
  10. package/dist/data-structures/binary-tree/index.js +1 -1
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +28 -51
  12. package/dist/data-structures/binary-tree/rb-tree.js +148 -180
  13. package/dist/data-structures/binary-tree/{tree-multiset.d.ts → tree-multimap.d.ts} +20 -20
  14. package/dist/data-structures/binary-tree/{tree-multiset.js → tree-multimap.js} +34 -31
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  16. package/dist/types/data-structures/binary-tree/index.d.ts +1 -1
  17. package/dist/types/data-structures/binary-tree/index.js +1 -1
  18. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
  19. package/dist/types/data-structures/binary-tree/rb-tree.js +0 -5
  20. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +4 -0
  21. package/package.json +2 -2
  22. package/src/data-structures/binary-tree/avl-tree.ts +5 -4
  23. package/src/data-structures/binary-tree/binary-tree.ts +201 -131
  24. package/src/data-structures/binary-tree/bst.ts +100 -34
  25. package/src/data-structures/binary-tree/index.ts +1 -1
  26. package/src/data-structures/binary-tree/rb-tree.ts +227 -236
  27. package/src/data-structures/binary-tree/{tree-multiset.ts → tree-multimap.ts} +38 -37
  28. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  29. package/src/types/data-structures/binary-tree/index.ts +1 -1
  30. package/src/types/data-structures/binary-tree/rb-tree.ts +5 -5
  31. package/src/types/data-structures/binary-tree/tree-multimap.ts +6 -0
  32. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -4
  33. package/src/types/data-structures/binary-tree/tree-multiset.ts +0 -6
  34. /package/dist/types/data-structures/binary-tree/{tree-multiset.js → tree-multimap.js} +0 -0
@@ -7,6 +7,41 @@ const queue_1 = require("../queue");
7
7
  class BSTNode extends binary_tree_1.BinaryTreeNode {
8
8
  constructor(key, value) {
9
9
  super(key, value);
10
+ this.parent = undefined;
11
+ this._left = undefined;
12
+ this._right = undefined;
13
+ }
14
+ /**
15
+ * Get the left child node.
16
+ */
17
+ get left() {
18
+ return this._left;
19
+ }
20
+ /**
21
+ * Set the left child node.
22
+ * @param {N | undefined} v - The left child node.
23
+ */
24
+ set left(v) {
25
+ if (v) {
26
+ v.parent = this;
27
+ }
28
+ this._left = v;
29
+ }
30
+ /**
31
+ * Get the right child node.
32
+ */
33
+ get right() {
34
+ return this._right;
35
+ }
36
+ /**
37
+ * Set the right child node.
38
+ * @param {N | undefined} v - The right child node.
39
+ */
40
+ set right(v) {
41
+ if (v) {
42
+ v.parent = this;
43
+ }
44
+ this._right = v;
10
45
  }
11
46
  }
12
47
  exports.BSTNode = BSTNode;
@@ -19,7 +54,9 @@ class BST extends binary_tree_1.BinaryTree {
19
54
  */
20
55
  constructor(options) {
21
56
  super(options);
57
+ this._root = undefined;
22
58
  this._comparator = (a, b) => a - b;
59
+ this._root = undefined;
23
60
  if (options !== undefined) {
24
61
  const { comparator } = options;
25
62
  if (comparator !== undefined) {
@@ -27,6 +64,12 @@ class BST extends binary_tree_1.BinaryTree {
27
64
  }
28
65
  }
29
66
  }
67
+ /**
68
+ * Get the root node of the binary tree.
69
+ */
70
+ get root() {
71
+ return this._root;
72
+ }
30
73
  /**
31
74
  * The function creates a new binary search tree node with the given key and value.
32
75
  * @param {BTNKey} key - The key parameter is the key value that will be associated with
@@ -41,27 +84,32 @@ class BST extends binary_tree_1.BinaryTree {
41
84
  /**
42
85
  * The `add` function in a binary search tree class inserts a new node with a given key and value
43
86
  * into the tree.
44
- * @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
45
- * `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
87
+ * @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can be either a
88
+ * `BTNKey` (which can be a number or a string), a `BSTNode` object, or `undefined`.
46
89
  * @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
47
90
  * binary search tree.
48
91
  * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
49
- * was not added or if the parameters were invalid, it returns null or undefined.
92
+ * was not added or if the parameters were invalid, it returns undefined or undefined.
50
93
  */
51
94
  add(keyOrNode, value) {
95
+ if (keyOrNode === 8) {
96
+ debugger;
97
+ }
98
+ if (keyOrNode === null)
99
+ return undefined;
52
100
  // TODO support node as a parameter
53
- let inserted = null;
54
- let newNode = null;
101
+ let inserted;
102
+ let newNode;
55
103
  if (keyOrNode instanceof BSTNode) {
56
104
  newNode = keyOrNode;
57
105
  }
58
106
  else if (typeof keyOrNode === 'number') {
59
107
  newNode = this.createNode(keyOrNode, value);
60
108
  }
61
- else if (keyOrNode === null) {
62
- newNode = null;
109
+ else {
110
+ newNode = undefined;
63
111
  }
64
- if (this.root === null) {
112
+ if (this.root === undefined) {
65
113
  this._setRoot(newNode);
66
114
  this._size = this.size + 1;
67
115
  inserted = this.root;
@@ -70,7 +118,7 @@ class BST extends binary_tree_1.BinaryTree {
70
118
  let cur = this.root;
71
119
  let traversing = true;
72
120
  while (traversing) {
73
- if (cur !== null && newNode !== null) {
121
+ if (cur !== undefined && newNode !== undefined) {
74
122
  if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
75
123
  if (newNode) {
76
124
  cur.value = newNode.value;
@@ -129,20 +177,20 @@ class BST extends binary_tree_1.BinaryTree {
129
177
  * @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
130
178
  * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
131
179
  * array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
132
- * `null
180
+ * `undefined
133
181
  * @param {V[]} data - The values of tree nodes
134
182
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
135
183
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
136
184
  * It can have two possible values:
137
- * @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
185
+ * @returns The `addMany` function returns an array of `N`, `undefined`, or `undefined` values.
138
186
  */
139
187
  addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
140
188
  // TODO this addMany function is inefficient, it should be optimized
141
189
  function hasNoNull(arr) {
142
- return arr.indexOf(null) === -1;
190
+ return arr.indexOf(undefined) === -1;
143
191
  }
144
192
  if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
145
- return super.addMany(keysOrNodes, data);
193
+ return super.addMany(keysOrNodes, data).map(n => n !== null && n !== void 0 ? n : undefined);
146
194
  }
147
195
  const inserted = [];
148
196
  const combinedArr = keysOrNodes.map((value, index) => [value, data === null || data === void 0 ? void 0 : data[index]]);
@@ -209,7 +257,7 @@ class BST extends binary_tree_1.BinaryTree {
209
257
  * The function `lastKey` returns the key of the rightmost node if the comparison result is less
210
258
  * than, the key of the leftmost node if the comparison result is greater than, and the key of the
211
259
  * rightmost node otherwise.
212
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting point for finding the last
260
+ * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting point for finding the last
213
261
  * key in a binary tree. It represents the root node of the subtree from which the search for the
214
262
  * last key should begin. If no specific `beginRoot` is provided, the search will start from the root
215
263
  * of the entire binary
@@ -243,9 +291,9 @@ class BST extends binary_tree_1.BinaryTree {
243
291
  * the first node that matches the nodeProperty. If set to true, the function will return an array
244
292
  * containing only that node. If set to false (default), the function will continue the traversal and
245
293
  * return an array containing all nodes that match the node
246
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
294
+ * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
247
295
  * specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
248
- * is `null`, an empty array will be returned.
296
+ * is `undefined`, an empty array will be returned.
249
297
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
250
298
  * traverse the binary tree. It can have one of the following values:
251
299
  * @returns an array of nodes (N[]).
@@ -315,17 +363,18 @@ class BST extends binary_tree_1.BinaryTree {
315
363
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
316
364
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
317
365
  * of the following values:
318
- * @param {BTNKey | N | null} targetNode - The `targetNode` parameter in the
366
+ * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter in the
319
367
  * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
320
368
  * start. It can be either a reference to a specific node (`N`), the key of a node
321
- * (`BTNKey`), or `null` to
369
+ * (`BTNKey`), or `undefined` to
322
370
  * @param iterationType - The `iterationType` parameter determines whether the traversal should be
323
371
  * done recursively or iteratively. It can have two possible values:
324
372
  * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
325
373
  */
326
374
  lesserOrGreaterTraverse(callback = this.defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
375
+ var _a;
327
376
  if (typeof targetNode === 'number')
328
- targetNode = this.getNode(targetNode);
377
+ targetNode = (_a = this.getNode(targetNode)) !== null && _a !== void 0 ? _a : undefined;
329
378
  const ans = [];
330
379
  if (!targetNode)
331
380
  return ans;
@@ -408,6 +457,7 @@ class BST extends binary_tree_1.BinaryTree {
408
457
  if (l <= r) {
409
458
  const m = l + Math.floor((r - l) / 2);
410
459
  const midNode = sorted[m];
460
+ debugger;
411
461
  this.add(midNode.key, midNode.value);
412
462
  stack.push([m + 1, r]);
413
463
  stack.push([l, m - 1]);
@@ -441,7 +491,7 @@ class BST extends binary_tree_1.BinaryTree {
441
491
  }
442
492
  else {
443
493
  const stack = [];
444
- let node = this.root, last = null;
494
+ let node = this.root, last = undefined;
445
495
  const depths = new Map();
446
496
  while (stack.length > 0 || node) {
447
497
  if (node) {
@@ -459,7 +509,7 @@ class BST extends binary_tree_1.BinaryTree {
459
509
  return false;
460
510
  depths.set(node, 1 + Math.max(left, right));
461
511
  last = node;
462
- node = null;
512
+ node = undefined;
463
513
  }
464
514
  }
465
515
  else
@@ -469,6 +519,12 @@ class BST extends binary_tree_1.BinaryTree {
469
519
  }
470
520
  return balanced;
471
521
  }
522
+ _setRoot(v) {
523
+ if (v) {
524
+ v.parent = undefined;
525
+ }
526
+ this._root = v;
527
+ }
472
528
  /**
473
529
  * The function compares two values using a comparator function and returns whether the first value
474
530
  * is greater than, less than, or equal to the second value.
@@ -4,4 +4,4 @@ export * from './binary-indexed-tree';
4
4
  export * from './segment-tree';
5
5
  export * from './avl-tree';
6
6
  export * from './rb-tree';
7
- export * from './tree-multiset';
7
+ export * from './tree-multimap';
@@ -20,4 +20,4 @@ __exportStar(require("./binary-indexed-tree"), exports);
20
20
  __exportStar(require("./segment-tree"), exports);
21
21
  __exportStar(require("./avl-tree"), exports);
22
22
  __exportStar(require("./rb-tree"), exports);
23
- __exportStar(require("./tree-multiset"), exports);
23
+ __exportStar(require("./tree-multimap"), exports);
@@ -5,16 +5,13 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { RBTNColor } from '../../types';
9
- export declare class RBTreeNode {
10
- key: number;
11
- parent: RBTreeNode;
12
- left: RBTreeNode;
13
- right: RBTreeNode;
14
- color: number;
15
- constructor(key: number, color?: RBTNColor);
8
+ import { BinaryTreeDeletedResult, BTNCallback, BTNKey, IterationType, RBTNColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
9
+ import { BST, BSTNode } from "./bst";
10
+ import { IBinaryTree } from "../../interfaces";
11
+ export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
12
+ color: RBTNColor;
13
+ constructor(key: BTNKey, value?: V, color?: RBTNColor);
16
14
  }
17
- export declare const NIL: RBTreeNode;
18
15
  /**
19
16
  * 1. Each node is either red or black.
20
17
  * 2. The root node is always black.
@@ -22,95 +19,75 @@ export declare const NIL: RBTreeNode;
22
19
  * 4. Red nodes must have black children.
23
20
  * 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
24
21
  */
25
- export declare class RedBlackTree {
26
- constructor();
27
- protected _root: RBTreeNode;
28
- get root(): RBTreeNode;
22
+ export declare class RedBlackTree<V = any, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
23
+ constructor(options?: RBTreeOptions);
24
+ protected _root: N;
25
+ get root(): N;
29
26
  protected _size: number;
30
27
  get size(): number;
31
- /**
32
- * The `insert` function inserts a new node with a given key into a red-black tree and fixes any
33
- * violations of the red-black tree properties.
34
- * @param {number} key - The key parameter is a number that represents the value to be inserted into
35
- * the RBTree.
36
- * @returns The function does not explicitly return anything.
37
- */
38
- add(key: number): void;
39
- /**
40
- * The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
41
- * tree.
42
- * @param {number} key - The `node` parameter is of type `RBTreeNode` and represents the current
43
- * node being processed in the delete operation.
44
- * @returns The `delete` function does not return anything. It has a return type of `void`.
45
- */
46
- delete(key: number): void;
47
- isRealNode(node: RBTreeNode | null | undefined): node is RBTreeNode;
48
- /**
49
- * The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
50
- * given key in a red-black tree.
51
- * @param {number} key - The key parameter is a number that represents the value we are searching for
52
- * in the RBTree.
53
- * @param beginRoot - The `beginRoot` parameter is an optional parameter that represents the starting
54
- * point for the search in the binary search tree. If no value is provided for `beginRoot`, it
55
- * defaults to the root of the binary search tree (`this.root`).
56
- * @returns a RBTreeNode.
57
- */
58
- getNode(key: number, beginRoot?: RBTreeNode): RBTreeNode;
28
+ NIL: N;
29
+ add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
30
+ createNode(key: BTNKey, value?: V, color?: RBTNColor): N;
31
+ delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeletedResult<N>[];
32
+ isNode(node: N | undefined): node is N;
33
+ getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
34
+ getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
35
+ getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
59
36
  /**
60
37
  * The function returns the leftmost node in a red-black tree.
61
38
  * @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
62
39
  * a Red-Black Tree.
63
40
  * @returns The leftmost node in the given RBTreeNode.
64
41
  */
65
- getLeftMost(node?: RBTreeNode): RBTreeNode;
42
+ getLeftMost(node?: N): N;
66
43
  /**
67
44
  * The function returns the rightmost node in a red-black tree.
68
45
  * @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
69
46
  * @returns the rightmost node in a red-black tree.
70
47
  */
71
- getRightMost(node: RBTreeNode): RBTreeNode;
48
+ getRightMost(node: N): N;
72
49
  /**
73
50
  * The function returns the successor of a given node in a red-black tree.
74
51
  * @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
75
52
  * @returns the successor of the given RBTreeNode.
76
53
  */
77
- getSuccessor(x: RBTreeNode): RBTreeNode;
54
+ getSuccessor(x: N): N | undefined;
78
55
  /**
79
56
  * The function returns the predecessor of a given node in a red-black tree.
80
57
  * @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
81
58
  * Red-Black Tree.
82
59
  * @returns the predecessor of the given RBTreeNode 'x'.
83
60
  */
84
- getPredecessor(x: RBTreeNode): RBTreeNode;
61
+ getPredecessor(x: N): N;
85
62
  clear(): void;
86
- print(beginRoot?: RBTreeNode): void;
63
+ protected _setRoot(v: N): void;
87
64
  /**
88
65
  * The function performs a left rotation on a red-black tree node.
89
66
  * @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
90
67
  */
91
- protected _leftRotate(x: RBTreeNode): void;
68
+ protected _leftRotate(x: N): void;
92
69
  /**
93
70
  * The function performs a right rotation on a red-black tree node.
94
71
  * @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
95
72
  * rotated.
96
73
  */
97
- protected _rightRotate(x: RBTreeNode): void;
74
+ protected _rightRotate(x: N): void;
98
75
  /**
99
76
  * The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
100
77
  * @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
101
78
  * red-black tree.
102
79
  */
103
- protected _fixDelete(x: RBTreeNode): void;
80
+ protected _fixDelete(x: N): void;
104
81
  /**
105
82
  * The function `_rbTransplant` replaces one node in a red-black tree with another node.
106
83
  * @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
107
84
  * @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
108
85
  */
109
- protected _rbTransplant(u: RBTreeNode, v: RBTreeNode): void;
86
+ protected _rbTransplant(u: N, v: N): void;
110
87
  /**
111
88
  * The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
112
89
  * @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
113
90
  * red-black tree.
114
91
  */
115
- protected _fixInsert(k: RBTreeNode): void;
92
+ protected _fixInsert(k: N): void;
116
93
  }