data-structure-typed 1.42.2 → 1.42.4

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 (57) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +12 -12
  3. package/benchmark/report.html +12 -12
  4. package/benchmark/report.json +104 -104
  5. package/dist/cjs/src/data-structures/binary-tree/avl-tree.d.ts +2 -2
  6. package/dist/cjs/src/data-structures/binary-tree/avl-tree.js +5 -3
  7. package/dist/cjs/src/data-structures/binary-tree/avl-tree.js.map +1 -1
  8. package/dist/cjs/src/data-structures/binary-tree/binary-tree.d.ts +57 -53
  9. package/dist/cjs/src/data-structures/binary-tree/binary-tree.js +116 -54
  10. package/dist/cjs/src/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/src/data-structures/binary-tree/bst.d.ts +42 -15
  12. package/dist/cjs/src/data-structures/binary-tree/bst.js +77 -21
  13. package/dist/cjs/src/data-structures/binary-tree/bst.js.map +1 -1
  14. package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +28 -51
  15. package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +148 -180
  16. package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
  17. package/dist/cjs/src/data-structures/binary-tree/tree-multiset.d.ts +10 -10
  18. package/dist/cjs/src/data-structures/binary-tree/tree-multiset.js +20 -17
  19. package/dist/cjs/src/data-structures/binary-tree/tree-multiset.js.map +1 -1
  20. package/dist/cjs/src/data-structures/graph/abstract-graph.js.map +1 -1
  21. package/dist/cjs/src/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  22. package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
  23. package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.js +0 -5
  24. package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.js.map +1 -1
  25. package/dist/mjs/src/data-structures/binary-tree/avl-tree.d.ts +2 -2
  26. package/dist/mjs/src/data-structures/binary-tree/avl-tree.js +5 -3
  27. package/dist/mjs/src/data-structures/binary-tree/binary-tree.d.ts +57 -53
  28. package/dist/mjs/src/data-structures/binary-tree/binary-tree.js +117 -55
  29. package/dist/mjs/src/data-structures/binary-tree/bst.d.ts +42 -15
  30. package/dist/mjs/src/data-structures/binary-tree/bst.js +79 -21
  31. package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +28 -51
  32. package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +148 -184
  33. package/dist/mjs/src/data-structures/binary-tree/tree-multiset.d.ts +10 -10
  34. package/dist/mjs/src/data-structures/binary-tree/tree-multiset.js +19 -17
  35. package/dist/mjs/src/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  36. package/dist/mjs/src/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
  37. package/dist/mjs/src/types/data-structures/binary-tree/rb-tree.js +0 -5
  38. package/dist/umd/data-structure-typed.min.js +1 -1
  39. package/dist/umd/data-structure-typed.min.js.map +1 -1
  40. package/package.json +2 -2
  41. package/src/data-structures/binary-tree/avl-tree.ts +5 -4
  42. package/src/data-structures/binary-tree/binary-tree.ts +227 -158
  43. package/src/data-structures/binary-tree/bst.ts +100 -34
  44. package/src/data-structures/binary-tree/rb-tree.ts +227 -236
  45. package/src/data-structures/binary-tree/tree-multiset.ts +24 -23
  46. package/src/data-structures/graph/abstract-graph.ts +18 -14
  47. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  48. package/src/types/data-structures/binary-tree/rb-tree.ts +5 -5
  49. package/test/performance/reportor.ts +1 -0
  50. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -1
  51. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +38 -38
  52. package/test/unit/data-structures/binary-tree/bst.test.ts +13 -0
  53. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +205 -159
  54. package/test/unit/data-structures/graph/directed-graph.test.ts +7 -8
  55. package/test/unit/data-structures/graph/salty-edges.json +875 -1
  56. package/test/unit/data-structures/graph/salty-vertexes.json +200 -1
  57. package/test/unit/data-structures/graph/undirected-graph.test.ts +15 -16
@@ -5,8 +5,46 @@ const types_1 = require("../../types");
5
5
  const binary_tree_1 = require("./binary-tree");
6
6
  const queue_1 = require("../queue");
7
7
  class BSTNode extends binary_tree_1.BinaryTreeNode {
8
+ parent;
8
9
  constructor(key, value) {
9
10
  super(key, value);
11
+ this.parent = undefined;
12
+ this._left = undefined;
13
+ this._right = undefined;
14
+ }
15
+ _left;
16
+ /**
17
+ * Get the left child node.
18
+ */
19
+ get left() {
20
+ return this._left;
21
+ }
22
+ /**
23
+ * Set the left child node.
24
+ * @param {N | undefined} v - The left child node.
25
+ */
26
+ set left(v) {
27
+ if (v) {
28
+ v.parent = this;
29
+ }
30
+ this._left = v;
31
+ }
32
+ _right;
33
+ /**
34
+ * Get the right child node.
35
+ */
36
+ get right() {
37
+ return this._right;
38
+ }
39
+ /**
40
+ * Set the right child node.
41
+ * @param {N | undefined} v - The right child node.
42
+ */
43
+ set right(v) {
44
+ if (v) {
45
+ v.parent = this;
46
+ }
47
+ this._right = v;
10
48
  }
11
49
  }
12
50
  exports.BSTNode = BSTNode;
@@ -19,6 +57,7 @@ class BST extends binary_tree_1.BinaryTree {
19
57
  */
20
58
  constructor(options) {
21
59
  super(options);
60
+ this._root = undefined;
22
61
  if (options !== undefined) {
23
62
  const { comparator } = options;
24
63
  if (comparator !== undefined) {
@@ -26,6 +65,13 @@ class BST extends binary_tree_1.BinaryTree {
26
65
  }
27
66
  }
28
67
  }
68
+ _root = undefined;
69
+ /**
70
+ * Get the root node of the binary tree.
71
+ */
72
+ get root() {
73
+ return this._root;
74
+ }
29
75
  /**
30
76
  * The function creates a new binary search tree node with the given key and value.
31
77
  * @param {BTNKey} key - The key parameter is the key value that will be associated with
@@ -40,27 +86,32 @@ class BST extends binary_tree_1.BinaryTree {
40
86
  /**
41
87
  * The `add` function in a binary search tree class inserts a new node with a given key and value
42
88
  * into the tree.
43
- * @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
44
- * `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
89
+ * @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can be either a
90
+ * `BTNKey` (which can be a number or a string), a `BSTNode` object, or `undefined`.
45
91
  * @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
46
92
  * binary search tree.
47
93
  * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
48
- * was not added or if the parameters were invalid, it returns null or undefined.
94
+ * was not added or if the parameters were invalid, it returns undefined or undefined.
49
95
  */
50
96
  add(keyOrNode, value) {
97
+ if (keyOrNode === 8) {
98
+ debugger;
99
+ }
100
+ if (keyOrNode === null)
101
+ return undefined;
51
102
  // TODO support node as a parameter
52
- let inserted = null;
53
- let newNode = null;
103
+ let inserted;
104
+ let newNode;
54
105
  if (keyOrNode instanceof BSTNode) {
55
106
  newNode = keyOrNode;
56
107
  }
57
108
  else if (typeof keyOrNode === 'number') {
58
109
  newNode = this.createNode(keyOrNode, value);
59
110
  }
60
- else if (keyOrNode === null) {
61
- newNode = null;
111
+ else {
112
+ newNode = undefined;
62
113
  }
63
- if (this.root === null) {
114
+ if (this.root === undefined) {
64
115
  this._setRoot(newNode);
65
116
  this._size = this.size + 1;
66
117
  inserted = this.root;
@@ -69,7 +120,7 @@ class BST extends binary_tree_1.BinaryTree {
69
120
  let cur = this.root;
70
121
  let traversing = true;
71
122
  while (traversing) {
72
- if (cur !== null && newNode !== null) {
123
+ if (cur !== undefined && newNode !== undefined) {
73
124
  if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
74
125
  if (newNode) {
75
126
  cur.value = newNode.value;
@@ -128,20 +179,20 @@ class BST extends binary_tree_1.BinaryTree {
128
179
  * @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
129
180
  * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
130
181
  * array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
131
- * `null
182
+ * `undefined
132
183
  * @param {V[]} data - The values of tree nodes
133
184
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
134
185
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
135
186
  * It can have two possible values:
136
- * @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
187
+ * @returns The `addMany` function returns an array of `N`, `undefined`, or `undefined` values.
137
188
  */
138
189
  addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
139
190
  // TODO this addMany function is inefficient, it should be optimized
140
191
  function hasNoNull(arr) {
141
- return arr.indexOf(null) === -1;
192
+ return arr.indexOf(undefined) === -1;
142
193
  }
143
194
  if (!isBalanceAdd || !hasNoNull(keysOrNodes)) {
144
- return super.addMany(keysOrNodes, data);
195
+ return super.addMany(keysOrNodes, data).map(n => n ?? undefined);
145
196
  }
146
197
  const inserted = [];
147
198
  const combinedArr = keysOrNodes.map((value, index) => [value, data?.[index]]);
@@ -208,7 +259,7 @@ class BST extends binary_tree_1.BinaryTree {
208
259
  * The function `lastKey` returns the key of the rightmost node if the comparison result is less
209
260
  * than, the key of the leftmost node if the comparison result is greater than, and the key of the
210
261
  * rightmost node otherwise.
211
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting point for finding the last
262
+ * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting point for finding the last
212
263
  * key in a binary tree. It represents the root node of the subtree from which the search for the
213
264
  * last key should begin. If no specific `beginRoot` is provided, the search will start from the root
214
265
  * of the entire binary
@@ -241,9 +292,9 @@ class BST extends binary_tree_1.BinaryTree {
241
292
  * the first node that matches the nodeProperty. If set to true, the function will return an array
242
293
  * containing only that node. If set to false (default), the function will continue the traversal and
243
294
  * return an array containing all nodes that match the node
244
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
295
+ * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
245
296
  * specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
246
- * is `null`, an empty array will be returned.
297
+ * is `undefined`, an empty array will be returned.
247
298
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
248
299
  * traverse the binary tree. It can have one of the following values:
249
300
  * @returns an array of nodes (N[]).
@@ -313,17 +364,17 @@ class BST extends binary_tree_1.BinaryTree {
313
364
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
314
365
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
315
366
  * of the following values:
316
- * @param {BTNKey | N | null} targetNode - The `targetNode` parameter in the
367
+ * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter in the
317
368
  * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
318
369
  * start. It can be either a reference to a specific node (`N`), the key of a node
319
- * (`BTNKey`), or `null` to
370
+ * (`BTNKey`), or `undefined` to
320
371
  * @param iterationType - The `iterationType` parameter determines whether the traversal should be
321
372
  * done recursively or iteratively. It can have two possible values:
322
373
  * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
323
374
  */
324
375
  lesserOrGreaterTraverse(callback = this.defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
325
376
  if (typeof targetNode === 'number')
326
- targetNode = this.getNode(targetNode);
377
+ targetNode = this.getNode(targetNode) ?? undefined;
327
378
  const ans = [];
328
379
  if (!targetNode)
329
380
  return ans;
@@ -406,6 +457,7 @@ class BST extends binary_tree_1.BinaryTree {
406
457
  if (l <= r) {
407
458
  const m = l + Math.floor((r - l) / 2);
408
459
  const midNode = sorted[m];
460
+ debugger;
409
461
  this.add(midNode.key, midNode.value);
410
462
  stack.push([m + 1, r]);
411
463
  stack.push([l, m - 1]);
@@ -438,7 +490,7 @@ class BST extends binary_tree_1.BinaryTree {
438
490
  }
439
491
  else {
440
492
  const stack = [];
441
- let node = this.root, last = null;
493
+ let node = this.root, last = undefined;
442
494
  const depths = new Map();
443
495
  while (stack.length > 0 || node) {
444
496
  if (node) {
@@ -456,7 +508,7 @@ class BST extends binary_tree_1.BinaryTree {
456
508
  return false;
457
509
  depths.set(node, 1 + Math.max(left, right));
458
510
  last = node;
459
- node = null;
511
+ node = undefined;
460
512
  }
461
513
  }
462
514
  else
@@ -467,6 +519,12 @@ class BST extends binary_tree_1.BinaryTree {
467
519
  return balanced;
468
520
  }
469
521
  _comparator = (a, b) => a - b;
522
+ _setRoot(v) {
523
+ if (v) {
524
+ v.parent = undefined;
525
+ }
526
+ this._root = v;
527
+ }
470
528
  /**
471
529
  * The function compares two values using a comparator function and returns whether the first value
472
530
  * is greater than, less than, or equal to the second value.
@@ -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
  }