data-structure-typed 1.49.3 → 1.49.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 (162) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +70 -69
  3. package/README_zh-CN.md +44 -49
  4. package/benchmark/report.html +16 -16
  5. package/benchmark/report.json +187 -187
  6. package/dist/cjs/data-structures/base/iterable-base.d.ts +1 -1
  7. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +2 -14
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js +19 -49
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
  15. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +1 -43
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  17. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +2 -11
  18. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -19
  19. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  20. package/dist/cjs/data-structures/graph/directed-graph.js +4 -0
  21. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  22. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  24. package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
  25. package/dist/cjs/data-structures/hash/hash-map.js +2 -2
  26. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  27. package/dist/cjs/data-structures/heap/heap.js +2 -3
  28. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  29. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  30. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  31. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  32. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  33. package/dist/cjs/data-structures/matrix/index.d.ts +0 -2
  34. package/dist/cjs/data-structures/matrix/index.js +0 -2
  35. package/dist/cjs/data-structures/matrix/index.js.map +1 -1
  36. package/dist/cjs/data-structures/matrix/matrix.d.ts +128 -10
  37. package/dist/cjs/data-structures/matrix/matrix.js +400 -15
  38. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  39. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  40. package/dist/cjs/data-structures/queue/deque.d.ts +2 -2
  41. package/dist/cjs/data-structures/queue/deque.js +5 -7
  42. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  43. package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
  44. package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
  45. package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
  46. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  47. package/dist/cjs/utils/utils.d.ts +1 -0
  48. package/dist/cjs/utils/utils.js +6 -1
  49. package/dist/cjs/utils/utils.js.map +1 -1
  50. package/dist/mjs/data-structures/base/iterable-base.d.ts +1 -1
  51. package/dist/mjs/data-structures/binary-tree/avl-tree.js +2 -1
  52. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +2 -14
  53. package/dist/mjs/data-structures/binary-tree/binary-tree.js +20 -50
  54. package/dist/mjs/data-structures/binary-tree/bst.js +2 -1
  55. package/dist/mjs/data-structures/binary-tree/rb-tree.js +2 -1
  56. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
  57. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +3 -44
  58. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +2 -11
  59. package/dist/mjs/data-structures/graph/abstract-graph.js +4 -20
  60. package/dist/mjs/data-structures/graph/directed-graph.js +4 -0
  61. package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
  62. package/dist/mjs/data-structures/hash/hash-map.js +2 -2
  63. package/dist/mjs/data-structures/heap/heap.js +2 -3
  64. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  65. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
  66. package/dist/mjs/data-structures/matrix/index.d.ts +0 -2
  67. package/dist/mjs/data-structures/matrix/index.js +0 -2
  68. package/dist/mjs/data-structures/matrix/matrix.d.ts +128 -10
  69. package/dist/mjs/data-structures/matrix/matrix.js +399 -16
  70. package/dist/mjs/data-structures/queue/deque.d.ts +2 -2
  71. package/dist/mjs/data-structures/queue/deque.js +7 -9
  72. package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
  73. package/dist/mjs/data-structures/queue/queue.js +1 -1
  74. package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
  75. package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
  76. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  77. package/dist/mjs/utils/utils.d.ts +1 -0
  78. package/dist/mjs/utils/utils.js +4 -0
  79. package/dist/umd/data-structure-typed.js +361 -542
  80. package/dist/umd/data-structure-typed.min.js +2 -2
  81. package/dist/umd/data-structure-typed.min.js.map +1 -1
  82. package/package.json +4 -3
  83. package/src/data-structures/base/index.ts +1 -1
  84. package/src/data-structures/base/iterable-base.ts +7 -10
  85. package/src/data-structures/binary-tree/avl-tree.ts +15 -8
  86. package/src/data-structures/binary-tree/binary-tree.ts +57 -74
  87. package/src/data-structures/binary-tree/bst.ts +16 -13
  88. package/src/data-structures/binary-tree/rb-tree.ts +16 -10
  89. package/src/data-structures/binary-tree/tree-multimap.ts +11 -48
  90. package/src/data-structures/graph/abstract-graph.ts +14 -24
  91. package/src/data-structures/graph/directed-graph.ts +8 -6
  92. package/src/data-structures/graph/map-graph.ts +6 -1
  93. package/src/data-structures/graph/undirected-graph.ts +4 -7
  94. package/src/data-structures/hash/hash-map.ts +18 -16
  95. package/src/data-structures/heap/heap.ts +7 -10
  96. package/src/data-structures/heap/max-heap.ts +2 -1
  97. package/src/data-structures/heap/min-heap.ts +2 -1
  98. package/src/data-structures/linked-list/singly-linked-list.ts +3 -5
  99. package/src/data-structures/matrix/index.ts +0 -2
  100. package/src/data-structures/matrix/matrix.ts +442 -13
  101. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
  102. package/src/data-structures/queue/deque.ts +18 -39
  103. package/src/data-structures/queue/queue.ts +1 -1
  104. package/src/interfaces/binary-tree.ts +7 -2
  105. package/src/types/common.ts +4 -4
  106. package/src/types/data-structures/base/base.ts +14 -3
  107. package/src/types/data-structures/base/index.ts +1 -1
  108. package/src/types/data-structures/graph/abstract-graph.ts +4 -2
  109. package/src/types/data-structures/hash/hash-map.ts +3 -3
  110. package/src/types/data-structures/heap/heap.ts +2 -2
  111. package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
  112. package/src/utils/utils.ts +7 -1
  113. package/test/integration/avl-tree.test.ts +18 -1
  114. package/test/integration/bst.test.ts +2 -2
  115. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
  116. package/test/performance/data-structures/comparison/comparison.test.ts +18 -23
  117. package/test/performance/data-structures/hash/hash-map.test.ts +3 -4
  118. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +16 -28
  119. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +4 -12
  120. package/test/performance/data-structures/queue/deque.test.ts +11 -12
  121. package/test/performance/data-structures/queue/queue.test.ts +15 -14
  122. package/test/performance/data-structures/stack/stack.test.ts +11 -11
  123. package/test/performance/reportor.ts +14 -15
  124. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -3
  125. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +65 -15
  126. package/test/unit/data-structures/binary-tree/bst.test.ts +52 -26
  127. package/test/unit/data-structures/binary-tree/overall.test.ts +16 -18
  128. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -6
  129. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -21
  130. package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
  131. package/test/unit/data-structures/graph/directed-graph.test.ts +112 -40
  132. package/test/unit/data-structures/graph/undirected-graph.test.ts +63 -33
  133. package/test/unit/data-structures/hash/hash-map.test.ts +64 -23
  134. package/test/unit/data-structures/hash/hash-table.test.ts +0 -4
  135. package/test/unit/data-structures/heap/heap.test.ts +0 -1
  136. package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
  137. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -4
  138. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -4
  139. package/test/unit/data-structures/matrix/matrix.test.ts +345 -52
  140. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
  141. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
  142. package/test/unit/data-structures/queue/deque.test.ts +0 -6
  143. package/test/unit/data-structures/queue/queue.test.ts +1 -1
  144. package/test/unit/data-structures/stack/stack.test.ts +3 -4
  145. package/test/unit/unrestricted-interconversion.test.ts +50 -44
  146. package/test/utils/big-o.ts +14 -14
  147. package/test/utils/performanc.ts +1 -1
  148. package/typedoc.json +30 -0
  149. package/dist/cjs/data-structures/matrix/matrix2d.d.ts +0 -107
  150. package/dist/cjs/data-structures/matrix/matrix2d.js +0 -200
  151. package/dist/cjs/data-structures/matrix/matrix2d.js.map +0 -1
  152. package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -200
  153. package/dist/cjs/data-structures/matrix/vector2d.js +0 -291
  154. package/dist/cjs/data-structures/matrix/vector2d.js.map +0 -1
  155. package/dist/mjs/data-structures/matrix/matrix2d.d.ts +0 -107
  156. package/dist/mjs/data-structures/matrix/matrix2d.js +0 -196
  157. package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -200
  158. package/dist/mjs/data-structures/matrix/vector2d.js +0 -289
  159. package/src/data-structures/matrix/matrix2d.ts +0 -211
  160. package/src/data-structures/matrix/vector2d.ts +0 -315
  161. package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -345
  162. package/test/unit/data-structures/matrix/vector2d.test.ts +0 -171
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.49.3",
3
+ "version": "1.49.5",
4
4
  "description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
@@ -14,11 +14,12 @@
14
14
  }
15
15
  },
16
16
  "scripts": {
17
- "build": "npm run build:mjs && npm run build:cjs && npm run build:umd && npm run build:docs",
17
+ "build": "npm run build:mjs && npm run build:cjs && npm run build:umd && npm run build:docs-class",
18
18
  "build:mjs": "rm -rf dist/mjs && tsc -p tsconfig-mjs.json",
19
19
  "build:cjs": "rm -rf dist/cjs && tsc -p tsconfig-cjs.json",
20
20
  "build:umd": "tsup",
21
21
  "build:docs": "typedoc --out docs ./src",
22
+ "build:docs-class": "typedoc --out docs ./src/data-structures",
22
23
  "test:unit": "jest --runInBand",
23
24
  "test": "npm run test:unit",
24
25
  "test:integration": "npm run update:subs && jest --config jest.integration.config.js",
@@ -87,7 +88,7 @@
87
88
  "ts-loader": "^9.4.4",
88
89
  "ts-node": "^10.9.1",
89
90
  "tsup": "^7.2.0",
90
- "typedoc": "^0.25.1",
91
+ "typedoc": "^0.25.4",
91
92
  "typescript": "^5.3.2"
92
93
  },
93
94
  "keywords": [
@@ -1 +1 @@
1
- export * from './iterable-base';
1
+ export * from './iterable-base';
@@ -1,7 +1,6 @@
1
- import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from "../../types";
1
+ import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from '../../types';
2
2
 
3
3
  export abstract class IterableEntryBase<K = any, V = any> {
4
-
5
4
  /**
6
5
  * Time Complexity: O(n)
7
6
  * Space Complexity: O(1)
@@ -147,7 +146,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
147
146
  let index = 0;
148
147
  for (const item of this) {
149
148
  const [key, value] = item;
150
- callbackfn.call(thisArg, value, key, index++, this)
149
+ callbackfn.call(thisArg, value, key, index++, this);
151
150
  }
152
151
  }
153
152
 
@@ -176,7 +175,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
176
175
  let index = 0;
177
176
  for (const item of this) {
178
177
  const [key, value] = item;
179
- accumulator = callbackfn(accumulator, value, key, index++, this)
178
+ accumulator = callbackfn(accumulator, value, key, index++, this);
180
179
  }
181
180
  return accumulator;
182
181
  }
@@ -193,14 +192,13 @@ export abstract class IterableEntryBase<K = any, V = any> {
193
192
  * Space Complexity: O(n)
194
193
  */
195
194
  print(): void {
196
- console.log([...this])
195
+ console.log([...this]);
197
196
  }
198
197
 
199
198
  protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
200
199
  }
201
200
 
202
201
  export abstract class IterableElementBase<V> {
203
-
204
202
  /**
205
203
  * Time Complexity: O(n)
206
204
  * Space Complexity: O(1)
@@ -310,7 +308,7 @@ export abstract class IterableElementBase<V> {
310
308
  forEach(callbackfn: ElementCallback<V, void>, thisArg?: any): void {
311
309
  let index = 0;
312
310
  for (const item of this) {
313
- callbackfn.call(thisArg, item as V, index++, this)
311
+ callbackfn.call(thisArg, item as V, index++, this);
314
312
  }
315
313
  }
316
314
 
@@ -335,18 +333,17 @@ export abstract class IterableElementBase<V> {
335
333
  let accumulator = initialValue;
336
334
  let index = 0;
337
335
  for (const item of this) {
338
- accumulator = callbackfn(accumulator, item as V, index++, this)
336
+ accumulator = callbackfn(accumulator, item as V, index++, this);
339
337
  }
340
338
  return accumulator;
341
339
  }
342
340
 
343
-
344
341
  /**
345
342
  * Time Complexity: O(n)
346
343
  * Space Complexity: O(n)
347
344
  */
348
345
  print(): void {
349
- console.log([...this])
346
+ console.log([...this]);
350
347
  }
351
348
 
352
349
  protected abstract _getIterator(...args: any[]): IterableIterator<V>;
@@ -18,7 +18,11 @@ import type {
18
18
  } from '../../types';
19
19
  import { IBinaryTree } from '../../interfaces';
20
20
 
21
- export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
21
+ export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<
22
+ K,
23
+ V,
24
+ N
25
+ > {
22
26
  height: number;
23
27
 
24
28
  constructor(key: K, value?: V) {
@@ -36,10 +40,14 @@ export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLT
36
40
  * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
37
41
  * 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
38
42
  */
39
- export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>>
43
+ export class AVLTree<
44
+ K = any,
45
+ V = any,
46
+ N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
47
+ TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>
48
+ >
40
49
  extends BST<K, V, N, TREE>
41
50
  implements IBinaryTree<K, V, N, TREE> {
42
-
43
51
  /**
44
52
  * The constructor function initializes an AVLTree object with optional elements and options.
45
53
  * @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
@@ -77,7 +85,8 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
77
85
  override createTree(options?: AVLTreeOptions<K>): TREE {
78
86
  return new AVLTree<K, V, N, TREE>([], {
79
87
  iterationType: this.iterationType,
80
- variant: this.variant, ...options
88
+ variant: this.variant,
89
+ ...options
81
90
  }) as TREE;
82
91
  }
83
92
 
@@ -97,7 +106,7 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
97
106
  * @returns a boolean value indicating whether the potentialKey is of type number or not.
98
107
  */
99
108
  override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
100
- return !(potentialKey instanceof AVLTreeNode)
109
+ return !(potentialKey instanceof AVLTreeNode);
101
110
  }
102
111
 
103
112
  /**
@@ -105,7 +114,6 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
105
114
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
106
115
  */
107
116
 
108
-
109
117
  /**
110
118
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
111
119
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -159,7 +167,6 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
159
167
  return deletedResults;
160
168
  }
161
169
 
162
-
163
170
  /**
164
171
  * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
165
172
  * tree.
@@ -489,6 +496,6 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
489
496
  protected _replaceNode(oldNode: N, newNode: N): N {
490
497
  newNode.height = oldNode.height;
491
498
 
492
- return super._replaceNode(oldNode, newNode)
499
+ return super._replaceNode(oldNode, newNode);
493
500
  }
494
501
  }
@@ -24,14 +24,18 @@ import { FamilyPosition, IterationType } from '../../types';
24
24
  import { IBinaryTree } from '../../interfaces';
25
25
  import { trampoline } from '../../utils';
26
26
  import { Queue } from '../queue';
27
- import { IterableEntryBase } from "../base";
27
+ import { IterableEntryBase } from '../base';
28
28
 
29
29
  /**
30
30
  * Represents a node in a binary tree.
31
31
  * @template V - The type of data stored in the node.
32
32
  * @template N - The type of the family relationship in the binary tree.
33
33
  */
34
- export class BinaryTreeNode<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>> {
34
+ export class BinaryTreeNode<
35
+ K = any,
36
+ V = any,
37
+ N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>
38
+ > {
35
39
  key: K;
36
40
 
37
41
  value?: V;
@@ -97,10 +101,15 @@ export class BinaryTreeNode<K = any, V = any, N extends BinaryTreeNode<K, V, N>
97
101
  * 5. Leaf Nodes: Nodes without children are leaves.
98
102
  */
99
103
 
100
- export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends IterableEntryBase<K, V | undefined>
101
-
104
+ export class BinaryTree<
105
+ K = any,
106
+ V = any,
107
+ N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
108
+ TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>
109
+ >
110
+ extends IterableEntryBase<K, V | undefined>
102
111
  implements IBinaryTree<K, V, N, TREE> {
103
- iterationType = IterationType.ITERATIVE
112
+ iterationType = IterationType.ITERATIVE;
104
113
 
105
114
  /**
106
115
  * The constructor function initializes a binary tree object with optional elements and options.
@@ -128,7 +137,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
128
137
  if (elements) this.addMany(elements);
129
138
  }
130
139
 
131
- protected _extractor = (key: K) => Number(key)
140
+ protected _extractor = (key: K) => Number(key);
132
141
 
133
142
  get extractor() {
134
143
  return this._extractor;
@@ -287,7 +296,6 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
287
296
  return undefined; // If the insertion position cannot be found, return undefined
288
297
  }
289
298
 
290
-
291
299
  /**
292
300
  * Time Complexity: O(k log n) - O(k * n)
293
301
  * Space Complexity: O(1)
@@ -329,7 +337,6 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
329
337
  return inserted;
330
338
  }
331
339
 
332
-
333
340
  /**
334
341
  * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
335
342
  * Space Complexity: O(1)
@@ -985,10 +992,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
985
992
  * @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
986
993
  * is no leftmost node, it returns `null` or `undefined` depending on the input.
987
994
  */
988
- getLeftMost(
989
- beginRoot: BTNKeyOrNode<K, N> = this.root,
990
- iterationType = this.iterationType
991
- ): N | null | undefined {
995
+ getLeftMost(beginRoot: BTNKeyOrNode<K, N> = this.root, iterationType = this.iterationType): N | null | undefined {
992
996
  beginRoot = this.ensureNode(beginRoot);
993
997
 
994
998
  if (!beginRoot) return beginRoot;
@@ -1031,10 +1035,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1031
1035
  * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
1032
1036
  * is no rightmost node, it returns `null` or `undefined`, depending on the input.
1033
1037
  */
1034
- getRightMost(
1035
- beginRoot: BTNKeyOrNode<K, N> = this.root,
1036
- iterationType = this.iterationType
1037
- ): N | null | undefined {
1038
+ getRightMost(beginRoot: BTNKeyOrNode<K, N> = this.root, iterationType = this.iterationType): N | null | undefined {
1038
1039
  // TODO support get right most by passing key in
1039
1040
  beginRoot = this.ensureNode(beginRoot);
1040
1041
  if (!beginRoot) return beginRoot;
@@ -1262,7 +1263,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1262
1263
  * @returns a boolean value indicating whether the potentialKey is of type number or not.
1263
1264
  */
1264
1265
  isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
1265
- return !(potentialKey instanceof BinaryTreeNode)
1266
+ return !(potentialKey instanceof BinaryTreeNode);
1266
1267
  }
1267
1268
 
1268
1269
  dfs<C extends BTNCallback<N>>(
@@ -1637,7 +1638,6 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1637
1638
  * after the given node in the inorder traversal of the binary tree.
1638
1639
  */
1639
1640
  getSuccessor(x?: K | N | null): N | null | undefined {
1640
-
1641
1641
  x = this.ensureNode(x);
1642
1642
  if (!this.isRealNode(x)) return undefined;
1643
1643
 
@@ -1858,11 +1858,14 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1858
1858
  beginRoot = this.ensureNode(beginRoot);
1859
1859
  if (!beginRoot) return;
1860
1860
 
1861
- if (opts.isShowUndefined) console.log(`U for undefined
1861
+ if (opts.isShowUndefined)
1862
+ console.log(`U for undefined
1862
1863
  `);
1863
- if (opts.isShowNull) console.log(`N for null
1864
+ if (opts.isShowNull)
1865
+ console.log(`N for null
1864
1866
  `);
1865
- if (opts.isShowRedBlackNIL) console.log(`S for Sentinel Node
1867
+ if (opts.isShowRedBlackNIL)
1868
+ console.log(`S for Sentinel Node
1866
1869
  `);
1867
1870
 
1868
1871
  const display = (root: N | null | undefined): void => {
@@ -1920,30 +1923,42 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1920
1923
  } else if (node !== null && node !== undefined) {
1921
1924
  // Display logic of normal nodes
1922
1925
 
1923
- const key = node.key, line = isNaN(this.extractor(key)) ? 'S' : this.extractor(key).toString(),
1926
+ const key = node.key,
1927
+ line = isNaN(this.extractor(key)) ? 'S' : this.extractor(key).toString(),
1924
1928
  width = line.length;
1925
1929
 
1926
- return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options))
1927
-
1930
+ return _buildNodeDisplay(
1931
+ line,
1932
+ width,
1933
+ this._displayAux(node.left, options),
1934
+ this._displayAux(node.right, options)
1935
+ );
1928
1936
  } else {
1929
1937
  // For cases where none of the conditions are met, null, undefined, and NaN nodes are not displayed
1930
- const line = node === undefined ? 'U' : 'N', width = line.length;
1938
+ const line = node === undefined ? 'U' : 'N',
1939
+ width = line.length;
1931
1940
 
1932
- return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0])
1941
+ return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0]);
1933
1942
  }
1934
1943
 
1935
1944
  function _buildNodeDisplay(line: string, width: number, left: NodeDisplayLayout, right: NodeDisplayLayout) {
1936
1945
  const [leftLines, leftWidth, leftHeight, leftMiddle] = left;
1937
1946
  const [rightLines, rightWidth, rightHeight, rightMiddle] = right;
1938
- const firstLine = ' '.repeat(Math.max(0, leftMiddle + 1))
1939
- + '_'.repeat(Math.max(0, leftWidth - leftMiddle - 1))
1940
- + line
1941
- + '_'.repeat(Math.max(0, rightMiddle))
1942
- + ' '.repeat(Math.max(0, rightWidth - rightMiddle));
1943
-
1944
- const secondLine = (leftHeight > 0 ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1) : ' '.repeat(leftWidth))
1945
- + ' '.repeat(width)
1946
- + (rightHeight > 0 ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1) : ' '.repeat(rightWidth));
1947
+ const firstLine =
1948
+ ' '.repeat(Math.max(0, leftMiddle + 1)) +
1949
+ '_'.repeat(Math.max(0, leftWidth - leftMiddle - 1)) +
1950
+ line +
1951
+ '_'.repeat(Math.max(0, rightMiddle)) +
1952
+ ' '.repeat(Math.max(0, rightWidth - rightMiddle));
1953
+
1954
+ const secondLine =
1955
+ (leftHeight > 0
1956
+ ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1)
1957
+ : ' '.repeat(leftWidth)) +
1958
+ ' '.repeat(width) +
1959
+ (rightHeight > 0
1960
+ ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1)
1961
+ : ' '.repeat(rightWidth));
1947
1962
 
1948
1963
  const mergedLines = [firstLine, secondLine];
1949
1964
 
@@ -1953,11 +1968,16 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
1953
1968
  mergedLines.push(leftLine + ' '.repeat(width) + rightLine);
1954
1969
  }
1955
1970
 
1956
- return <NodeDisplayLayout>[mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
1971
+ return <NodeDisplayLayout>[
1972
+ mergedLines,
1973
+ leftWidth + width + rightWidth,
1974
+ Math.max(leftHeight, rightHeight) + 2,
1975
+ leftWidth + Math.floor(width / 2)
1976
+ ];
1957
1977
  }
1958
1978
  }
1959
1979
 
1960
- protected _defaultOneParamCallback = (node: N) => node.key;
1980
+ protected _defaultOneParamCallback = (node: N | null | undefined) => (node ? node.key : undefined);
1961
1981
 
1962
1982
  /**
1963
1983
  * Swap the data of two nodes in the binary tree.
@@ -2012,43 +2032,6 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
2012
2032
  return newNode;
2013
2033
  }
2014
2034
 
2015
- /**
2016
- * The function `_addTo` adds a new node to a binary tree if there is an available position.
2017
- * @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
2018
- * the binary tree. It can be either a node object or `null`.
2019
- * @param {N} parent - The `parent` parameter represents the parent node to which the new node will
2020
- * be added as a child.
2021
- * @returns either the left or right child node of the parent node, depending on which child is
2022
- * available for adding the new node. If a new node is added, the function also updates the size of
2023
- * the binary tree. If neither the left nor right child is available, the function returns undefined.
2024
- * If the parent node is null, the function also returns undefined.
2025
- */
2026
- protected _addTo(newNode: N | null | undefined, parent: BTNKeyOrNode<K, N>): N | null | undefined {
2027
- if (this.isNotNodeInstance(parent)) parent = this.getNode(parent);
2028
-
2029
- if (parent) {
2030
- // When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
2031
- // In this scenario, null nodes serve as "sentinel nodes," "virtual nodes," or "placeholder nodes."
2032
- if (parent.left === undefined) {
2033
- parent.left = newNode;
2034
- if (newNode) {
2035
- this._size = this.size + 1;
2036
- }
2037
- return parent.left;
2038
- } else if (parent.right === undefined) {
2039
- parent.right = newNode;
2040
- if (newNode) {
2041
- this._size = this.size + 1;
2042
- }
2043
- return parent.right;
2044
- } else {
2045
- return;
2046
- }
2047
- } else {
2048
- return;
2049
- }
2050
- }
2051
-
2052
2035
  /**
2053
2036
  * The function sets the root property of an object to a given value, and if the value is not null,
2054
2037
  * it also sets the parent property of the value to undefined.
@@ -20,7 +20,11 @@ import { BinaryTree, BinaryTreeNode } from './binary-tree';
20
20
  import { IBinaryTree } from '../../interfaces';
21
21
  import { Queue } from '../queue';
22
22
 
23
- export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, N> {
23
+ export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNested<K, V>> extends BinaryTreeNode<
24
+ K,
25
+ V,
26
+ N
27
+ > {
24
28
  override parent?: N;
25
29
 
26
30
  constructor(key: K, value?: V) {
@@ -80,11 +84,14 @@ export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNeste
80
84
  * 6. Balance Variability: Can become unbalanced; special types maintain balance.
81
85
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
82
86
  */
83
- export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>>
87
+ export class BST<
88
+ K = any,
89
+ V = any,
90
+ N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>,
91
+ TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>
92
+ >
84
93
  extends BinaryTree<K, V, N, TREE>
85
94
  implements IBinaryTree<K, V, N, TREE> {
86
-
87
-
88
95
  /**
89
96
  * This is the constructor function for a binary search tree class in TypeScript, which initializes
90
97
  * the tree with optional elements and options.
@@ -114,7 +121,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
114
121
  return this._root;
115
122
  }
116
123
 
117
- protected _variant = BSTVariant.MIN
124
+ protected _variant = BSTVariant.MIN;
118
125
 
119
126
  get variant() {
120
127
  return this._variant;
@@ -142,7 +149,8 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
142
149
  override createTree(options?: Partial<BSTOptions<K>>): TREE {
143
150
  return new BST<K, V, N, TREE>([], {
144
151
  iterationType: this.iterationType,
145
- variant: this.variant, ...options
152
+ variant: this.variant,
153
+ ...options
146
154
  }) as TREE;
147
155
  }
148
156
 
@@ -155,7 +163,6 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
155
163
  return exemplar instanceof BSTNode;
156
164
  }
157
165
 
158
-
159
166
  /**
160
167
  * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
161
168
  * otherwise it returns undefined.
@@ -301,7 +308,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
301
308
  const isRealBTNExemplar = (kve: BTNExemplar<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
302
309
  if (kve === undefined || kve === null) return false;
303
310
  return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
304
- }
311
+ };
305
312
 
306
313
  for (const kve of keysOrNodesOrEntries) {
307
314
  isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
@@ -359,7 +366,6 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
359
366
  return inserted;
360
367
  }
361
368
 
362
-
363
369
  /**
364
370
  * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
365
371
  * Space Complexity: O(n) - Additional space is required for the sorted array.
@@ -398,7 +404,6 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
398
404
  return current.key;
399
405
  }
400
406
 
401
-
402
407
  /**
403
408
  * Time Complexity: O(log n) - Average case for a balanced tree.
404
409
  * Space Complexity: O(1) - Constant space is used.
@@ -450,7 +455,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
450
455
  * @returns a boolean value indicating whether the potentialKey is of type number or not.
451
456
  */
452
457
  override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
453
- return !(potentialKey instanceof BSTNode)
458
+ return !(potentialKey instanceof BSTNode);
454
459
  }
455
460
 
456
461
  /**
@@ -738,7 +743,6 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
738
743
  return balanced;
739
744
  }
740
745
 
741
-
742
746
  protected _setRoot(v: N | undefined) {
743
747
  if (v) {
744
748
  v.parent = undefined;
@@ -761,5 +765,4 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
761
765
 
762
766
  return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
763
767
  }
764
-
765
768
  }
@@ -21,10 +21,11 @@ import {
21
21
  import { BST, BSTNode } from './bst';
22
22
  import { IBinaryTree } from '../../interfaces';
23
23
 
24
- export class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<
25
- K, V,
26
- N
27
- > {
24
+ export class RedBlackTreeNode<
25
+ K = any,
26
+ V = any,
27
+ N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>
28
+ > extends BSTNode<K, V, N> {
28
29
  color: RBTNColor;
29
30
 
30
31
  constructor(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK) {
@@ -40,7 +41,12 @@ export class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V,
40
41
  * 4. Red nodes must have black children.
41
42
  * 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
42
43
  */
43
- export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, N, TREE> = RedBlackTree<K, V, N, RedBlackTreeNested<K, V, N>>>
44
+ export class RedBlackTree<
45
+ K = any,
46
+ V = any,
47
+ N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
48
+ TREE extends RedBlackTree<K, V, N, TREE> = RedBlackTree<K, V, N, RedBlackTreeNested<K, V, N>>
49
+ >
44
50
  extends BST<K, V, N, TREE>
45
51
  implements IBinaryTree<K, V, N, TREE> {
46
52
  Sentinel: N = new RedBlackTreeNode<K, V>(NaN as K) as unknown as N;
@@ -101,7 +107,8 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
101
107
  override createTree(options?: RBTreeOptions<K>): TREE {
102
108
  return new RedBlackTree<K, V, N, TREE>([], {
103
109
  iterationType: this.iterationType,
104
- variant: this.variant, ...options
110
+ variant: this.variant,
111
+ ...options
105
112
  }) as TREE;
106
113
  }
107
114
 
@@ -122,7 +129,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
122
129
  * @returns a boolean value indicating whether the potentialKey is of type number or not.
123
130
  */
124
131
  override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
125
- return !(potentialKey instanceof RedBlackTreeNode)
132
+ return !(potentialKey instanceof RedBlackTreeNode);
126
133
  }
127
134
 
128
135
  /**
@@ -191,12 +198,11 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
191
198
  x = x?.right;
192
199
  } else {
193
200
  if (newNode !== x) {
194
- this._replaceNode(x, newNode)
201
+ this._replaceNode(x, newNode);
195
202
  }
196
203
  return;
197
204
  }
198
205
  }
199
-
200
206
  }
201
207
 
202
208
  newNode.parent = y;
@@ -649,6 +655,6 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
649
655
  protected _replaceNode(oldNode: N, newNode: N): N {
650
656
  newNode.color = oldNode.color;
651
657
 
652
- return super._replaceNode(oldNode, newNode)
658
+ return super._replaceNode(oldNode, newNode);
653
659
  }
654
660
  }