min-heap-typed 1.40.0 → 1.41.1

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 (38) hide show
  1. package/dist/data-structures/binary-tree/binary-tree.d.ts +14 -3
  2. package/dist/data-structures/binary-tree/binary-tree.js +52 -10
  3. package/dist/data-structures/binary-tree/bst.d.ts +2 -2
  4. package/dist/data-structures/binary-tree/bst.js +3 -3
  5. package/dist/data-structures/binary-tree/rb-tree.d.ts +96 -9
  6. package/dist/data-structures/binary-tree/rb-tree.js +377 -12
  7. package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
  8. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -7
  9. package/dist/types/data-structures/binary-tree/rb-tree.js +11 -6
  10. package/package.json +2 -2
  11. package/src/data-structures/binary-tree/avl-tree.ts +3 -2
  12. package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
  13. package/src/data-structures/binary-tree/binary-tree.ts +87 -17
  14. package/src/data-structures/binary-tree/bst.ts +10 -7
  15. package/src/data-structures/binary-tree/rb-tree.ts +396 -349
  16. package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
  17. package/src/data-structures/graph/abstract-graph.ts +11 -12
  18. package/src/data-structures/graph/directed-graph.ts +7 -6
  19. package/src/data-structures/graph/undirected-graph.ts +7 -6
  20. package/src/data-structures/hash/hash-map.ts +1 -1
  21. package/src/data-structures/hash/tree-map.ts +1 -2
  22. package/src/data-structures/hash/tree-set.ts +1 -2
  23. package/src/data-structures/heap/heap.ts +2 -2
  24. package/src/data-structures/heap/max-heap.ts +1 -1
  25. package/src/data-structures/heap/min-heap.ts +1 -1
  26. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  27. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  28. package/src/data-structures/matrix/matrix.ts +1 -1
  29. package/src/data-structures/matrix/vector2d.ts +1 -2
  30. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  31. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  32. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  33. package/src/data-structures/queue/deque.ts +3 -4
  34. package/src/data-structures/queue/queue.ts +1 -1
  35. package/src/types/data-structures/binary-tree/rb-tree.ts +6 -6
  36. package/src/types/data-structures/matrix/navigator.ts +1 -1
  37. package/src/types/utils/utils.ts +1 -1
  38. package/src/types/utils/validate-type.ts +2 -2
@@ -177,9 +177,12 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
177
177
  has<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
178
178
  has<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
179
179
  has<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C, beginRoot?: N, iterationType?: IterationType): boolean;
180
- get<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): N | null;
181
- get<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): N | null;
182
- get<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N, iterationType?: IterationType): N | null;
180
+ getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): N | null;
181
+ getNode<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): N | null;
182
+ getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N, iterationType?: IterationType): N | null;
183
+ get<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): V | undefined;
184
+ get<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): V | undefined;
185
+ get<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N, iterationType?: IterationType): V | undefined;
183
186
  /**
184
187
  * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
185
188
  * up to the root node, with the option to reverse the order of the nodes.
@@ -301,6 +304,14 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
301
304
  * @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
302
305
  */
303
306
  getPredecessor(node: N): N;
307
+ /**
308
+ * The function `getSuccessor` returns the next node in a binary tree given a node `x`, or `null` if
309
+ * `x` is the last node.
310
+ * @param {N} x - N - a node in a binary tree
311
+ * @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
312
+ * if there is no successor, or `undefined` if the input `x` is `undefined`.
313
+ */
314
+ getSuccessor(x: N): N | null | undefined;
304
315
  /**
305
316
  * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
306
317
  * algorithm and returns an array of values obtained by applying a callback function to each node.
@@ -171,7 +171,7 @@ class BinaryTree {
171
171
  return;
172
172
  }
173
173
  const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
174
- const existNode = key !== undefined ? this.get(key, (node) => node.key) : undefined;
174
+ const existNode = key !== undefined ? this.getNode(key, (node) => node.key) : undefined;
175
175
  if (this.root) {
176
176
  if (existNode) {
177
177
  existNode.value = value;
@@ -249,7 +249,7 @@ class BinaryTree {
249
249
  return bstDeletedResult;
250
250
  if (identifier instanceof BinaryTreeNode)
251
251
  callback = (node => node);
252
- const curr = this.get(identifier, callback);
252
+ const curr = this.getNode(identifier, callback);
253
253
  if (!curr)
254
254
  return bstDeletedResult;
255
255
  const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
@@ -302,9 +302,9 @@ class BinaryTree {
302
302
  */
303
303
  getDepth(distNode, beginRoot = this.root) {
304
304
  if (typeof distNode === 'number')
305
- distNode = this.get(distNode);
305
+ distNode = this.getNode(distNode);
306
306
  if (typeof beginRoot === 'number')
307
- beginRoot = this.get(beginRoot);
307
+ beginRoot = this.getNode(beginRoot);
308
308
  let depth = 0;
309
309
  while (distNode === null || distNode === void 0 ? void 0 : distNode.parent) {
310
310
  if (distNode === beginRoot) {
@@ -329,7 +329,7 @@ class BinaryTree {
329
329
  */
330
330
  getHeight(beginRoot = this.root, iterationType = this.iterationType) {
331
331
  if (typeof beginRoot === 'number')
332
- beginRoot = this.get(beginRoot);
332
+ beginRoot = this.getNode(beginRoot);
333
333
  if (!beginRoot)
334
334
  return -1;
335
335
  if (iterationType === types_1.IterationType.RECURSIVE) {
@@ -521,13 +521,35 @@ class BinaryTree {
521
521
  * performed when searching for a node in the binary tree. It can have one of the following values:
522
522
  * @returns either the found node (of type N) or null if no node is found.
523
523
  */
524
- get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
524
+ getNode(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
525
525
  var _a;
526
526
  if (identifier instanceof BinaryTreeNode)
527
527
  callback = (node => node);
528
528
  // TODO may support finding node by value equal
529
529
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
530
530
  }
531
+ /**
532
+ * The function `get` returns the first node value in a binary tree that matches the given property or key.
533
+ * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
534
+ * the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
535
+ * type.
536
+ * @param callback - The `callback` parameter is a function that is used to determine whether a node
537
+ * matches the desired criteria. It takes a node as input and returns a boolean value indicating
538
+ * whether the node matches the criteria or not. The default callback function
539
+ * (`((node: N) => node.key)`) is used if no callback function is
540
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
541
+ * the root node from which the search should begin.
542
+ * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
543
+ * performed when searching for a node in the binary tree. It can have one of the following values:
544
+ * @returns either the found value (of type V) or undefined if no node value is found.
545
+ */
546
+ get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
547
+ var _a;
548
+ if (identifier instanceof BinaryTreeNode)
549
+ callback = (node => node);
550
+ // TODO may support finding node by value equal
551
+ return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0].value) !== null && _a !== void 0 ? _a : undefined;
552
+ }
531
553
  /**
532
554
  * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
533
555
  * up to the root node, with the option to reverse the order of the nodes.
@@ -563,7 +585,7 @@ class BinaryTree {
563
585
  */
564
586
  getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
565
587
  if (typeof beginRoot === 'number')
566
- beginRoot = this.get(beginRoot);
588
+ beginRoot = this.getNode(beginRoot);
567
589
  if (!beginRoot)
568
590
  return beginRoot;
569
591
  if (iterationType === types_1.IterationType.RECURSIVE) {
@@ -686,7 +708,7 @@ class BinaryTree {
686
708
  */
687
709
  subTreeTraverse(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
688
710
  if (typeof beginRoot === 'number')
689
- beginRoot = this.get(beginRoot);
711
+ beginRoot = this.getNode(beginRoot);
690
712
  const ans = [];
691
713
  if (!beginRoot)
692
714
  return ans;
@@ -813,7 +835,7 @@ class BinaryTree {
813
835
  const ans = [];
814
836
  if (iterationType === types_1.IterationType.RECURSIVE) {
815
837
  const queue = new queue_1.Queue([beginRoot]);
816
- function traverse(level) {
838
+ const traverse = (level) => {
817
839
  if (queue.size === 0)
818
840
  return;
819
841
  const current = queue.shift();
@@ -823,7 +845,7 @@ class BinaryTree {
823
845
  if (current.right)
824
846
  queue.push(current.right);
825
847
  traverse(level + 1);
826
- }
848
+ };
827
849
  traverse(0);
828
850
  }
829
851
  else {
@@ -908,6 +930,24 @@ class BinaryTree {
908
930
  return node;
909
931
  }
910
932
  }
933
+ /**
934
+ * The function `getSuccessor` returns the next node in a binary tree given a node `x`, or `null` if
935
+ * `x` is the last node.
936
+ * @param {N} x - N - a node in a binary tree
937
+ * @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
938
+ * if there is no successor, or `undefined` if the input `x` is `undefined`.
939
+ */
940
+ getSuccessor(x) {
941
+ if (x.right) {
942
+ return this.getLeftMost(x.right);
943
+ }
944
+ let y = x.parent;
945
+ while (y && y && x === y.right) {
946
+ x = y;
947
+ y = y.parent;
948
+ }
949
+ return y;
950
+ }
911
951
  // --- start additional methods ---
912
952
  /**
913
953
  * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
@@ -1037,10 +1077,12 @@ class BinaryTree {
1037
1077
  }
1038
1078
  else {
1039
1079
  if (node.left) {
1080
+ // @ts-ignore
1040
1081
  yield* this[Symbol.iterator](node.left);
1041
1082
  }
1042
1083
  yield node.key;
1043
1084
  if (node.right) {
1085
+ // @ts-ignore
1044
1086
  yield* this[Symbol.iterator](node.right);
1045
1087
  }
1046
1088
  }
@@ -43,7 +43,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
43
43
  /**
44
44
  * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
45
45
  * maintaining balance.
46
- * @param {[BTNKey | N, N['value']][]} keysOrNodes - The `arr` parameter in the `addMany` function
46
+ * @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
47
47
  * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
48
48
  * array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
49
49
  * `null
@@ -72,7 +72,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
72
72
  * @returns either the first node that matches the given nodeProperty and callback, or null if no
73
73
  * matching node is found.
74
74
  */
75
- get<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
75
+ getNode<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
76
76
  /**
77
77
  * The function `lastKey` returns the key of the rightmost node if the comparison result is less
78
78
  * than, the key of the leftmost node if the comparison result is greater than, and the key of the
@@ -126,7 +126,7 @@ class BST extends binary_tree_1.BinaryTree {
126
126
  /**
127
127
  * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
128
128
  * maintaining balance.
129
- * @param {[BTNKey | N, N['value']][]} keysOrNodes - The `arr` parameter in the `addMany` function
129
+ * @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
130
130
  * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
131
131
  * array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
132
132
  * `null
@@ -223,7 +223,7 @@ class BST extends binary_tree_1.BinaryTree {
223
223
  * @returns either the first node that matches the given nodeProperty and callback, or null if no
224
224
  * matching node is found.
225
225
  */
226
- get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
226
+ getNode(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
227
227
  var _a;
228
228
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
229
229
  }
@@ -347,7 +347,7 @@ class BST extends binary_tree_1.BinaryTree {
347
347
  */
348
348
  lesserOrGreaterTraverse(callback = ((node) => node.key), lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
349
349
  if (typeof targetNode === 'number')
350
- targetNode = this.get(targetNode);
350
+ targetNode = this.getNode(targetNode);
351
351
  const ans = [];
352
352
  if (!targetNode)
353
353
  return ans;
@@ -1,11 +1,98 @@
1
- import { BTNKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
2
- import { IBinaryTree } from '../../interfaces';
3
- import { BST, BSTNode } from './bst';
4
- export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
5
- constructor(key: BTNKey, value?: V);
6
- color: RBColor;
1
+ import { RBTNColor } from '../../types';
2
+ export declare class RBTreeNode {
3
+ key: number;
4
+ parent: RBTreeNode;
5
+ left: RBTreeNode;
6
+ right: RBTreeNode;
7
+ color: number;
8
+ constructor(key: number, color?: RBTNColor);
7
9
  }
8
- export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
9
- constructor(options?: RBTreeOptions);
10
- createNode(key: BTNKey, value?: V): N;
10
+ export declare const SN: RBTreeNode;
11
+ export declare class RedBlackTree {
12
+ constructor();
13
+ protected _root: RBTreeNode;
14
+ get root(): RBTreeNode;
15
+ /**
16
+ * The `insert` function inserts a new node with a given key into a red-black tree and fixes any
17
+ * violations of the red-black tree properties.
18
+ * @param {number} key - The key parameter is a number that represents the value to be inserted into
19
+ * the RBTree.
20
+ * @returns The function does not explicitly return anything.
21
+ */
22
+ insert(key: number): void;
23
+ /**
24
+ * The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
25
+ * tree.
26
+ * @param {RBTreeNode} node - The `node` parameter is of type `RBTreeNode` and represents the current
27
+ * node being processed in the delete operation.
28
+ * @returns The `delete` function does not return anything. It has a return type of `void`.
29
+ */
30
+ delete(key: number): void;
31
+ isRealNode(node: RBTreeNode): node is RBTreeNode;
32
+ /**
33
+ * The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
34
+ * given key in a red-black tree.
35
+ * @param {number} key - The key parameter is a number that represents the value we are searching for
36
+ * in the RBTree.
37
+ * @param beginRoot - The `beginRoot` parameter is an optional parameter that represents the starting
38
+ * point for the search in the binary search tree. If no value is provided for `beginRoot`, it
39
+ * defaults to the root of the binary search tree (`this.root`).
40
+ * @returns a RBTreeNode.
41
+ */
42
+ getNode(key: number, beginRoot?: RBTreeNode): RBTreeNode;
43
+ /**
44
+ * The function returns the leftmost node in a red-black tree.
45
+ * @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
46
+ * a Red-Black Tree.
47
+ * @returns The leftmost node in the given RBTreeNode.
48
+ */
49
+ getLeftMost(node?: RBTreeNode): RBTreeNode;
50
+ /**
51
+ * The function returns the rightmost node in a red-black tree.
52
+ * @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
53
+ * @returns the rightmost node in a red-black tree.
54
+ */
55
+ getRightMost(node: RBTreeNode): RBTreeNode;
56
+ /**
57
+ * The function returns the successor of a given node in a red-black tree.
58
+ * @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
59
+ * @returns the successor of the given RBTreeNode.
60
+ */
61
+ getSuccessor(x: RBTreeNode): RBTreeNode;
62
+ /**
63
+ * The function returns the predecessor of a given node in a red-black tree.
64
+ * @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
65
+ * Red-Black Tree.
66
+ * @returns the predecessor of the given RBTreeNode 'x'.
67
+ */
68
+ getPredecessor(x: RBTreeNode): RBTreeNode;
69
+ /**
70
+ * The function performs a left rotation on a red-black tree node.
71
+ * @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
72
+ */
73
+ protected _leftRotate(x: RBTreeNode): void;
74
+ /**
75
+ * The function performs a right rotation on a red-black tree node.
76
+ * @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
77
+ * rotated.
78
+ */
79
+ protected _rightRotate(x: RBTreeNode): void;
80
+ /**
81
+ * The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
82
+ * @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
83
+ * red-black tree.
84
+ */
85
+ protected _fixDelete(x: RBTreeNode): void;
86
+ /**
87
+ * The function `_rbTransplant` replaces one node in a red-black tree with another node.
88
+ * @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
89
+ * @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
90
+ */
91
+ protected _rbTransplant(u: RBTreeNode, v: RBTreeNode): void;
92
+ /**
93
+ * The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
94
+ * @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
95
+ * red-black tree.
96
+ */
97
+ protected _fixInsert(k: RBTreeNode): void;
11
98
  }