data-structure-typed 1.18.7 → 1.19.0

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 (269) hide show
  1. package/README.md +208 -419
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +198 -167
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.js +413 -398
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +24 -16
  5. package/dist/data-structures/binary-tree/avl-tree.js +23 -17
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -26
  7. package/dist/data-structures/binary-tree/binary-tree.js +12 -27
  8. package/dist/data-structures/binary-tree/bst.d.ts +66 -90
  9. package/dist/data-structures/binary-tree/bst.js +85 -163
  10. package/dist/data-structures/binary-tree/rb-tree.d.ts +6 -17
  11. package/dist/data-structures/binary-tree/rb-tree.js +5 -17
  12. package/dist/data-structures/binary-tree/tree-multiset.d.ts +187 -18
  13. package/dist/data-structures/binary-tree/tree-multiset.js +716 -39
  14. package/dist/data-structures/graph/abstract-graph.d.ts +112 -52
  15. package/dist/data-structures/graph/abstract-graph.js +138 -72
  16. package/dist/data-structures/graph/directed-graph.d.ts +104 -101
  17. package/dist/data-structures/graph/directed-graph.js +134 -129
  18. package/dist/data-structures/graph/undirected-graph.d.ts +66 -65
  19. package/dist/data-structures/graph/undirected-graph.js +107 -106
  20. package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +12 -18
  21. package/dist/data-structures/interfaces/abstract-graph.d.ts +2 -4
  22. package/dist/data-structures/interfaces/avl-tree.d.ts +2 -2
  23. package/dist/data-structures/interfaces/binary-tree.d.ts +1 -1
  24. package/dist/data-structures/interfaces/bst.d.ts +4 -5
  25. package/dist/data-structures/interfaces/rb-tree.d.ts +2 -3
  26. package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -1
  27. package/dist/data-structures/tree/tree.d.ts +1 -4
  28. package/dist/data-structures/tree/tree.js +1 -12
  29. package/dist/data-structures/types/abstract-binary-tree.d.ts +10 -6
  30. package/dist/data-structures/types/abstract-binary-tree.js +9 -5
  31. package/dist/data-structures/types/avl-tree.d.ts +1 -1
  32. package/dist/data-structures/types/binary-tree.d.ts +1 -1
  33. package/dist/data-structures/types/bst.d.ts +1 -1
  34. package/dist/data-structures/types/bst.js +1 -1
  35. package/dist/data-structures/types/directed-graph.js +1 -1
  36. package/dist/data-structures/types/helpers.d.ts +1 -8
  37. package/dist/data-structures/types/rb-tree.d.ts +2 -0
  38. package/dist/data-structures/types/rb-tree.js +1 -1
  39. package/dist/data-structures/types/tree-multiset.d.ts +5 -5
  40. package/dist/utils/index.d.ts +1 -0
  41. package/dist/utils/index.js +1 -0
  42. package/dist/utils/types/index.d.ts +1 -0
  43. package/dist/utils/types/index.js +1 -0
  44. package/dist/utils/types/validate-type.d.ts +19 -0
  45. package/dist/utils/types/validate-type.js +2 -0
  46. package/dist/utils/utils.d.ts +2 -2
  47. package/dist/utils/utils.js +0 -62
  48. package/dist/utils/validate-type.d.ts +45 -0
  49. package/dist/utils/validate-type.js +58 -0
  50. package/package.json +5 -2
  51. package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
  52. package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
  53. package/backup/recursive-type/src/assets/logo.png +0 -0
  54. package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
  55. package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +0 -3
  56. package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +0 -288
  57. package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +0 -3
  58. package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
  59. package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +0 -1502
  60. package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +0 -503
  61. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  62. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  63. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  64. package/backup/recursive-type/src/data-structures/binary-tree/index.ts +0 -11
  65. package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +0 -110
  66. package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +0 -243
  67. package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +0 -3
  68. package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +0 -55
  69. package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  70. package/backup/recursive-type/src/data-structures/diagrams/README.md +0 -5
  71. package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +0 -985
  72. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  73. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  74. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  75. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  76. package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  77. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  78. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  79. package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  80. package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
  81. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  82. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  83. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  84. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  85. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  86. package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +0 -478
  87. package/backup/recursive-type/src/data-structures/graph/index.ts +0 -3
  88. package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +0 -293
  89. package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +0 -67
  90. package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +0 -56
  91. package/backup/recursive-type/src/data-structures/hash/hash-table.ts +0 -3
  92. package/backup/recursive-type/src/data-structures/hash/index.ts +0 -6
  93. package/backup/recursive-type/src/data-structures/hash/pair.ts +0 -3
  94. package/backup/recursive-type/src/data-structures/hash/tree-map.ts +0 -3
  95. package/backup/recursive-type/src/data-structures/hash/tree-set.ts +0 -3
  96. package/backup/recursive-type/src/data-structures/heap/heap.ts +0 -176
  97. package/backup/recursive-type/src/data-structures/heap/index.ts +0 -3
  98. package/backup/recursive-type/src/data-structures/heap/max-heap.ts +0 -31
  99. package/backup/recursive-type/src/data-structures/heap/min-heap.ts +0 -34
  100. package/backup/recursive-type/src/data-structures/index.ts +0 -15
  101. package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +0 -42
  102. package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +0 -1
  103. package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +0 -56
  104. package/backup/recursive-type/src/data-structures/interfaces/bst.ts +0 -1
  105. package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +0 -15
  106. package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
  107. package/backup/recursive-type/src/data-structures/interfaces/heap.ts +0 -1
  108. package/backup/recursive-type/src/data-structures/interfaces/index.ts +0 -13
  109. package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +0 -1
  110. package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +0 -1
  111. package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +0 -1
  112. package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +0 -1
  113. package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +0 -1
  114. package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +0 -3
  115. package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
  116. package/backup/recursive-type/src/data-structures/linked-list/index.ts +0 -3
  117. package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +0 -490
  118. package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +0 -3
  119. package/backup/recursive-type/src/data-structures/matrix/index.ts +0 -4
  120. package/backup/recursive-type/src/data-structures/matrix/matrix.ts +0 -27
  121. package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +0 -208
  122. package/backup/recursive-type/src/data-structures/matrix/navigator.ts +0 -122
  123. package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +0 -316
  124. package/backup/recursive-type/src/data-structures/priority-queue/index.ts +0 -3
  125. package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
  126. package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
  127. package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +0 -354
  128. package/backup/recursive-type/src/data-structures/queue/deque.ts +0 -251
  129. package/backup/recursive-type/src/data-structures/queue/index.ts +0 -2
  130. package/backup/recursive-type/src/data-structures/queue/queue.ts +0 -120
  131. package/backup/recursive-type/src/data-structures/stack/index.ts +0 -1
  132. package/backup/recursive-type/src/data-structures/stack/stack.ts +0 -98
  133. package/backup/recursive-type/src/data-structures/tree/index.ts +0 -1
  134. package/backup/recursive-type/src/data-structures/tree/tree.ts +0 -80
  135. package/backup/recursive-type/src/data-structures/trie/index.ts +0 -1
  136. package/backup/recursive-type/src/data-structures/trie/trie.ts +0 -227
  137. package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +0 -5
  138. package/backup/recursive-type/src/data-structures/types/avl-tree.ts +0 -8
  139. package/backup/recursive-type/src/data-structures/types/binary-tree.ts +0 -10
  140. package/backup/recursive-type/src/data-structures/types/bst.ts +0 -6
  141. package/backup/recursive-type/src/data-structures/types/directed-graph.ts +0 -8
  142. package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +0 -1
  143. package/backup/recursive-type/src/data-structures/types/heap.ts +0 -5
  144. package/backup/recursive-type/src/data-structures/types/index.ts +0 -12
  145. package/backup/recursive-type/src/data-structures/types/navigator.ts +0 -13
  146. package/backup/recursive-type/src/data-structures/types/priority-queue.ts +0 -9
  147. package/backup/recursive-type/src/data-structures/types/segment-tree.ts +0 -1
  148. package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +0 -1
  149. package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +0 -1
  150. package/backup/recursive-type/src/index.ts +0 -1
  151. package/backup/recursive-type/src/utils/index.ts +0 -2
  152. package/backup/recursive-type/src/utils/types/index.ts +0 -1
  153. package/backup/recursive-type/src/utils/types/utils.ts +0 -6
  154. package/backup/recursive-type/src/utils/utils.ts +0 -78
  155. package/docs/.nojekyll +0 -1
  156. package/docs/assets/highlight.css +0 -92
  157. package/docs/assets/main.js +0 -58
  158. package/docs/assets/search.js +0 -1
  159. package/docs/assets/style.css +0 -1367
  160. package/docs/classes/AVLTree.html +0 -2451
  161. package/docs/classes/AVLTreeNode.html +0 -499
  162. package/docs/classes/AaTree.html +0 -172
  163. package/docs/classes/AbstractBinaryTree.html +0 -2118
  164. package/docs/classes/AbstractBinaryTreeNode.html +0 -524
  165. package/docs/classes/AbstractEdge.html +0 -295
  166. package/docs/classes/AbstractGraph.html +0 -1043
  167. package/docs/classes/AbstractVertex.html +0 -258
  168. package/docs/classes/ArrayDeque.html +0 -439
  169. package/docs/classes/BST.html +0 -2297
  170. package/docs/classes/BSTNode.html +0 -503
  171. package/docs/classes/BTree.html +0 -172
  172. package/docs/classes/BinaryIndexedTree.html +0 -341
  173. package/docs/classes/BinaryTree.html +0 -2133
  174. package/docs/classes/BinaryTreeNode.html +0 -501
  175. package/docs/classes/Character.html +0 -220
  176. package/docs/classes/CoordinateMap.html +0 -483
  177. package/docs/classes/CoordinateSet.html +0 -444
  178. package/docs/classes/Deque.html +0 -975
  179. package/docs/classes/DirectedEdge.html +0 -366
  180. package/docs/classes/DirectedGraph.html +0 -1443
  181. package/docs/classes/DirectedVertex.html +0 -254
  182. package/docs/classes/DoublyLinkedList.html +0 -968
  183. package/docs/classes/DoublyLinkedListNode.html +0 -297
  184. package/docs/classes/HashTable.html +0 -172
  185. package/docs/classes/Heap.html +0 -423
  186. package/docs/classes/HeapItem.html +0 -255
  187. package/docs/classes/Matrix2D.html +0 -502
  188. package/docs/classes/MatrixNTI2D.html +0 -240
  189. package/docs/classes/MaxHeap.html +0 -436
  190. package/docs/classes/MaxPriorityQueue.html +0 -836
  191. package/docs/classes/MinHeap.html +0 -437
  192. package/docs/classes/MinPriorityQueue.html +0 -838
  193. package/docs/classes/Navigator.html +0 -313
  194. package/docs/classes/ObjectDeque.html +0 -455
  195. package/docs/classes/Pair.html +0 -172
  196. package/docs/classes/PriorityQueue.html +0 -760
  197. package/docs/classes/Queue.html +0 -392
  198. package/docs/classes/RBTree.html +0 -2388
  199. package/docs/classes/RBTreeNode.html +0 -516
  200. package/docs/classes/SegmentTree.html +0 -434
  201. package/docs/classes/SegmentTreeNode.html +0 -357
  202. package/docs/classes/SinglyLinkedList.html +0 -788
  203. package/docs/classes/SinglyLinkedListNode.html +0 -270
  204. package/docs/classes/SkipLinkedList.html +0 -172
  205. package/docs/classes/SplayTree.html +0 -172
  206. package/docs/classes/Stack.html +0 -368
  207. package/docs/classes/TreeMap.html +0 -172
  208. package/docs/classes/TreeMultiSet.html +0 -2297
  209. package/docs/classes/TreeMultiSetNode.html +0 -499
  210. package/docs/classes/TreeNode.html +0 -343
  211. package/docs/classes/TreeSet.html +0 -172
  212. package/docs/classes/Trie.html +0 -372
  213. package/docs/classes/TrieNode.html +0 -280
  214. package/docs/classes/TwoThreeTree.html +0 -172
  215. package/docs/classes/UndirectedEdge.html +0 -337
  216. package/docs/classes/UndirectedGraph.html +0 -1210
  217. package/docs/classes/UndirectedVertex.html +0 -254
  218. package/docs/classes/Vector2D.html +0 -805
  219. package/docs/enums/CP.html +0 -181
  220. package/docs/enums/FamilyPosition.html +0 -181
  221. package/docs/enums/LoopType.html +0 -182
  222. package/docs/enums/RBColor.html +0 -174
  223. package/docs/enums/TopologicalProperty.html +0 -181
  224. package/docs/index.html +0 -645
  225. package/docs/interfaces/IAVLTree.html +0 -1378
  226. package/docs/interfaces/IAVLTreeNode.html +0 -405
  227. package/docs/interfaces/IAbstractBinaryTree.html +0 -1124
  228. package/docs/interfaces/IAbstractBinaryTreeNode.html +0 -384
  229. package/docs/interfaces/IAbstractGraph.html +0 -433
  230. package/docs/interfaces/IBST.html +0 -1271
  231. package/docs/interfaces/IBSTNode.html +0 -408
  232. package/docs/interfaces/IDirectedGraph.html +0 -572
  233. package/docs/interfaces/IUNDirectedGraph.html +0 -465
  234. package/docs/modules.html +0 -262
  235. package/docs/types/AVLTreeOptions.html +0 -150
  236. package/docs/types/AbstractBinaryTreeNodeProperties.html +0 -152
  237. package/docs/types/AbstractBinaryTreeNodeProperty.html +0 -152
  238. package/docs/types/AbstractBinaryTreeOptions.html +0 -156
  239. package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -152
  240. package/docs/types/BSTComparator.html +0 -162
  241. package/docs/types/BSTOptions.html +0 -152
  242. package/docs/types/BinaryTreeDeletedResult.html +0 -159
  243. package/docs/types/BinaryTreeNodeId.html +0 -147
  244. package/docs/types/BinaryTreeNodePropertyName.html +0 -147
  245. package/docs/types/BinaryTreeOptions.html +0 -150
  246. package/docs/types/DFSOrderPattern.html +0 -147
  247. package/docs/types/DijkstraResult.html +0 -167
  248. package/docs/types/Direction.html +0 -147
  249. package/docs/types/EdgeId.html +0 -147
  250. package/docs/types/HeapOptions.html +0 -168
  251. package/docs/types/IdObject.html +0 -157
  252. package/docs/types/KeyValObject.html +0 -152
  253. package/docs/types/NavigatorParams.html +0 -181
  254. package/docs/types/NodeOrPropertyName.html +0 -147
  255. package/docs/types/PriorityQueueComparator.html +0 -167
  256. package/docs/types/PriorityQueueDFSOrderPattern.html +0 -147
  257. package/docs/types/PriorityQueueOptions.html +0 -161
  258. package/docs/types/RBTreeOptions.html +0 -150
  259. package/docs/types/RecursiveAVLTreeNode.html +0 -152
  260. package/docs/types/RecursiveBSTNode.html +0 -152
  261. package/docs/types/RecursiveBinaryTreeNode.html +0 -152
  262. package/docs/types/RecursiveTreeMultiSetNode.html +0 -152
  263. package/docs/types/SegmentTreeNodeVal.html +0 -147
  264. package/docs/types/TopologicalStatus.html +0 -147
  265. package/docs/types/TreeMultiSetOptions.html +0 -152
  266. package/docs/types/Turning.html +0 -147
  267. package/docs/types/VertexId.html +0 -147
  268. package/notes/bst.test.ts +0 -181
  269. package/notes/note.md +0 -34
@@ -39,27 +39,14 @@ var BSTNode = /** @class */ (function (_super) {
39
39
  function BSTNode() {
40
40
  return _super !== null && _super.apply(this, arguments) || this;
41
41
  }
42
- /**
43
- * The function creates a new binary search tree node with the specified id, value, and count.
44
- * @param {BinaryTreeNodeId} id - The id parameter is the identifier for the binary tree node. It is used to uniquely
45
- * identify each node in the tree.
46
- * @param {T} [val] - The "val" parameter represents the value that will be stored in the binary tree node. It is an
47
- * optional parameter, meaning it can be omitted when calling the "createNode" function.
48
- * @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary
49
- * search tree node. It is an optional parameter, so it can be omitted when calling the `createNode` method.
50
- * @returns The method is returning a new instance of the BSTNode class, casted as the FAMILY type.
51
- */
52
- BSTNode.prototype.createNode = function (id, val, count) {
53
- return new BSTNode(id, (val === undefined ? id : val), count);
54
- };
55
42
  return BSTNode;
56
43
  }(binary_tree_1.BinaryTreeNode));
57
44
  exports.BSTNode = BSTNode;
58
45
  var BST = /** @class */ (function (_super) {
59
46
  __extends(BST, _super);
60
47
  /**
61
- * The constructor function accepts an optional options object and sets the comparator property if provided.
62
- * @param [options] - An optional object that can contain the following properties:
48
+ * The constructor function initializes a binary search tree object with an optional comparator function.
49
+ * @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
63
50
  */
64
51
  function BST(options) {
65
52
  var _this = _super.call(this, options) || this;
@@ -73,38 +60,31 @@ var BST = /** @class */ (function (_super) {
73
60
  return _this;
74
61
  }
75
62
  /**
76
- * The function creates a new binary search tree node with the given id, value, and count.
77
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
78
- * `BinaryTreeNodeId`.
79
- * @param {N['val'] | null} [val] - The `val` parameter is the value that will be stored in the node. It can be of any
80
- * type `N['val']` or `null`.
81
- * @param {number} [count] - The `count` parameter is an optional parameter that represents the number of occurrences
82
- * of a particular value in the binary search tree node.
83
- * @returns a new instance of the BSTNode class, casted as type N.
63
+ * The function creates a new binary search tree node with the given id and value.
64
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
65
+ * identify each node in the binary tree.
66
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
67
+ * that will be stored in the node.
68
+ * @returns a new instance of the BSTNode class with the specified id and value.
84
69
  */
85
- BST.prototype.createNode = function (id, val, count) {
86
- return new BSTNode(id, val === undefined ? id : val, count);
70
+ BST.prototype.createNode = function (id, val) {
71
+ return new BSTNode(id, val);
87
72
  };
88
73
  /**
89
- * The `add` function inserts a new node into a binary search tree, updating the count and value of an existing node if
90
- * the ID matches, and returns the inserted node.
91
- * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node. It is used to
92
- * determine the position of the node in the binary search tree.
93
- * @param {N | null} val - The `val` parameter represents the value to be stored in the binary search tree node. It can
94
- * be of type `N` (the generic type) or `null`.
95
- * @param {number} [count=1] - The `count` parameter represents the number of times the value should be inserted into
96
- * the binary search tree. By default, it is set to 1, meaning that if no count is specified, the value will be
97
- * inserted once.
98
- * @returns The method `add` returns a `N` object or `null`.
74
+ * The `add` function adds a new node to a binary tree, ensuring that duplicates are not accepted.
75
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add. It
76
+ * is of type `BinaryTreeNodeId`.
77
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It represents
78
+ * the value associated with the node.
79
+ * @returns The function `add` returns the inserted node (`inserted`) if it was successfully added to the binary tree.
80
+ * If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`.
99
81
  */
100
- BST.prototype.add = function (id, val, count) {
101
- if (count === void 0) { count = 1; }
82
+ BST.prototype.add = function (id, val) {
102
83
  var inserted = null;
103
- var newNode = this.createNode(id, val, count);
84
+ var newNode = this.createNode(id, val);
104
85
  if (this.root === null) {
105
86
  this._setRoot(newNode);
106
87
  this._setSize(this.size + 1);
107
- this._setCount(this.count + count);
108
88
  inserted = (this.root);
109
89
  }
110
90
  else {
@@ -114,8 +94,6 @@ var BST = /** @class */ (function (_super) {
114
94
  if (cur !== null && newNode !== null) {
115
95
  if (this._compare(cur.id, id) === types_1.CP.eq) {
116
96
  if (newNode) {
117
- cur.count += newNode.count;
118
- this._setCount(this.count + newNode.count);
119
97
  cur.val = newNode.val;
120
98
  }
121
99
  //Duplicates are not accepted.
@@ -127,12 +105,10 @@ var BST = /** @class */ (function (_super) {
127
105
  if (cur.left === undefined) {
128
106
  if (newNode) {
129
107
  newNode.parent = cur;
130
- newNode.familyPosition = types_1.FamilyPosition.LEFT;
131
108
  }
132
109
  //Add to the left of the current node
133
110
  cur.left = newNode;
134
111
  this._setSize(this.size + 1);
135
- this._setCount(this.count + newNode.count);
136
112
  traversing = false;
137
113
  inserted = cur.left;
138
114
  }
@@ -147,12 +123,10 @@ var BST = /** @class */ (function (_super) {
147
123
  if (cur.right === undefined) {
148
124
  if (newNode) {
149
125
  newNode.parent = cur;
150
- newNode.familyPosition = types_1.FamilyPosition.RIGHT;
151
126
  }
152
127
  //Add to the right of the current node
153
128
  cur.right = newNode;
154
129
  this._setSize(this.size + 1);
155
- this._setCount(this.count + newNode.count);
156
130
  traversing = false;
157
131
  inserted = (cur.right);
158
132
  }
@@ -171,13 +145,12 @@ var BST = /** @class */ (function (_super) {
171
145
  return inserted;
172
146
  };
173
147
  /**
174
- * The `get` function returns the first node in a binary search tree that matches the given property value or name.
148
+ * The function returns the first node in a binary tree that matches the given property name and value.
175
149
  * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
176
- * generic type `N`. It represents the value of the property that you want to search for in the binary search tree.
150
+ * generic type `N`. It represents the property of the binary tree node that you want to search for.
177
151
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
178
- * specifies the property name to use for searching the binary search tree nodes. If not provided, it defaults to
179
- * `'id'`.
180
- * @returns The method is returning a N object or null.
152
+ * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
153
+ * @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
181
154
  */
182
155
  BST.prototype.get = function (nodeProperty, propertyName) {
183
156
  var _a;
@@ -187,10 +160,9 @@ var BST = /** @class */ (function (_super) {
187
160
  /**
188
161
  * The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
189
162
  * leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
190
- * @returns The function `lastKey()` returns the ID of the rightmost node in a binary tree. If the comparison between
191
- * the first two elements in the tree is less than, it returns the ID of the rightmost node. If the comparison is
192
- * greater than, it returns the ID of the leftmost node. Otherwise, it also returns the ID of the rightmost node. If
193
- * there are no nodes in
163
+ * @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
164
+ * the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
165
+ * equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
194
166
  */
195
167
  BST.prototype.lastKey = function () {
196
168
  var _a, _b, _c, _d, _e, _f;
@@ -202,78 +174,15 @@ var BST = /** @class */ (function (_super) {
202
174
  return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
203
175
  };
204
176
  /**
205
- * The `remove` function in this TypeScript code removes a node from a binary search tree and returns information about
206
- * the deleted node and any nodes that need to be balanced.
207
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that needs to be removed
208
- * from the binary search tree.
209
- * @param {boolean} [ignoreCount] - A boolean flag indicating whether to ignore the count of the node being removed. If
210
- * set to true, the count of the node will not be considered and the node will be removed regardless of its count. If
211
- * set to false or not provided, the count of the node will be taken into account and the
212
- * @returns an array of `BSTDeletedResult<N>` objects.
213
- */
214
- BST.prototype.remove = function (id, ignoreCount) {
215
- var bstDeletedResult = [];
216
- if (!this.root)
217
- return bstDeletedResult;
218
- var curr = this.get(id);
219
- if (!curr)
220
- return bstDeletedResult;
221
- var parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
222
- var needBalanced = null, orgCurrent = curr;
223
- if (curr.count > 1 && !ignoreCount) {
224
- curr.count--;
225
- this._setCount(this.count - 1);
226
- }
227
- else {
228
- if (!curr.left) {
229
- if (!parent) {
230
- if (curr.right !== undefined)
231
- this._setRoot(curr.right);
232
- }
233
- else {
234
- switch (curr.familyPosition) {
235
- case types_1.FamilyPosition.LEFT:
236
- parent.left = curr.right;
237
- break;
238
- case types_1.FamilyPosition.RIGHT:
239
- parent.right = curr.right;
240
- break;
241
- }
242
- needBalanced = parent;
243
- }
244
- }
245
- else {
246
- var leftSubTreeMax = curr.left ? this.getRightMost(curr.left) : null;
247
- if (leftSubTreeMax) {
248
- var parentOfLeftSubTreeMax = leftSubTreeMax.parent;
249
- orgCurrent = curr.swapLocation(leftSubTreeMax);
250
- if (parentOfLeftSubTreeMax) {
251
- if (parentOfLeftSubTreeMax.right === leftSubTreeMax)
252
- parentOfLeftSubTreeMax.right = leftSubTreeMax.left;
253
- else
254
- parentOfLeftSubTreeMax.left = leftSubTreeMax.left;
255
- needBalanced = parentOfLeftSubTreeMax;
256
- }
257
- }
258
- }
259
- this._setSize(this.size - 1);
260
- this._setCount(this.count - curr.count);
261
- }
262
- bstDeletedResult.push({ deleted: orgCurrent, needBalanced: needBalanced });
263
- return bstDeletedResult;
264
- };
265
- /**
266
- * The function `getNodes` returns an array of binary search tree nodes that match a given property value, with the
267
- * option to specify the property name and whether to return only one node.
268
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
269
- * generic type `N`. It represents the property value that you want to search for in the binary search tree.
177
+ * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
178
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
179
+ * `N` type. It represents the property of the binary tree node that you want to compare with.
270
180
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
271
- * specifies the property of the nodes to compare with the `nodeProperty` parameter. If not provided, it defaults to
272
- * `'id'`.
273
- * @param {boolean} [onlyOne] - A boolean value indicating whether to return only one node that matches the given
274
- * nodeProperty. If set to true, the function will stop traversing the tree and return the first matching node. If set
275
- * to false or not provided, the function will return all nodes that match the given nodeProperty.
276
- * @returns an array of N objects.
181
+ * specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
182
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
183
+ * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
184
+ * is set to `true`, the function will return an array with only one node (if
185
+ * @returns an array of nodes (type N).
277
186
  */
278
187
  BST.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
279
188
  var _this = this;
@@ -324,29 +233,30 @@ var BST = /** @class */ (function (_super) {
324
233
  };
325
234
  // --- start additional functions
326
235
  /**
327
- * The `lesserSum` function calculates the sum of a specified property in all nodes with an ID less than a given ID in
328
- * a binary search tree.
329
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node for which you want to
330
- * calculate the lesser sum.
236
+ * The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
237
+ * less than a given node.
238
+ * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
331
239
  * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
332
- * specifies the property of the binary tree node to use for calculating the sum. If not provided, it defaults to 'id'.
240
+ * specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
333
241
  * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
334
- * binary search tree that have a property value lesser than the given `id`.
242
+ * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
335
243
  */
336
- BST.prototype.lesserSum = function (id, propertyName) {
244
+ BST.prototype.lesserSum = function (beginNode, propertyName) {
337
245
  var _this = this;
338
246
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
247
+ if (typeof beginNode === 'number')
248
+ beginNode = this.get(beginNode, 'id');
249
+ if (!beginNode)
250
+ return 0;
339
251
  if (!this.root)
340
252
  return 0;
253
+ var id = beginNode.id;
341
254
  var getSumByPropertyName = function (cur) {
342
255
  var needSum;
343
256
  switch (propertyName) {
344
257
  case 'id':
345
258
  needSum = cur.id;
346
259
  break;
347
- case 'count':
348
- needSum = cur.count;
349
- break;
350
260
  default:
351
261
  needSum = cur.id;
352
262
  break;
@@ -412,20 +322,25 @@ var BST = /** @class */ (function (_super) {
412
322
  return sum;
413
323
  };
414
324
  /**
415
- * The function `allGreaterNodesAdd` updates the value of a specified property for all nodes in a binary search tree
416
- * that have a greater value than a given node.
417
- * @param node - The `node` parameter is of type `N`, which represents a node in a binary search tree. It
418
- * contains properties such as `id` and `count`.
325
+ * The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
326
+ * have a greater value than a given node.
327
+ * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
328
+ * `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
419
329
  * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
420
- * each node should be increased.
421
- * @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
422
- * property of the BSTNode to be modified. It can be either 'id' or 'count'. If propertyName is not provided, it
423
- * defaults to 'id'.
330
+ * each greater node should be increased.
331
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
332
+ * specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
333
+ * 'id'.
424
334
  * @returns a boolean value.
425
335
  */
426
336
  BST.prototype.allGreaterNodesAdd = function (node, delta, propertyName) {
427
337
  var _this = this;
428
338
  propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
339
+ if (typeof node === 'number')
340
+ node = this.get(node, 'id');
341
+ if (!node)
342
+ return false;
343
+ var id = node.id;
429
344
  if (!this.root)
430
345
  return false;
431
346
  var _sumByPropertyName = function (cur) {
@@ -433,9 +348,6 @@ var BST = /** @class */ (function (_super) {
433
348
  case 'id':
434
349
  cur.id += delta;
435
350
  break;
436
- case 'count':
437
- cur.count += delta;
438
- break;
439
351
  default:
440
352
  cur.id += delta;
441
353
  break;
@@ -443,13 +355,14 @@ var BST = /** @class */ (function (_super) {
443
355
  };
444
356
  if (this.loopType === types_1.LoopType.RECURSIVE) {
445
357
  var _traverse_3 = function (cur) {
446
- var compared = _this._compare(cur.id, node.id);
447
- _sumByPropertyName(cur);
358
+ var compared = _this._compare(cur.id, id);
359
+ if (compared === types_1.CP.gt)
360
+ _sumByPropertyName(cur);
448
361
  if (!cur.left && !cur.right)
449
362
  return;
450
- if (cur.left && compared === types_1.CP.gt)
363
+ if (cur.left && _this._compare(cur.left.id, id) === types_1.CP.gt)
451
364
  _traverse_3(cur.left);
452
- else if (cur.right && compared === types_1.CP.gt)
365
+ if (cur.right && _this._compare(cur.right.id, id) === types_1.CP.gt)
453
366
  _traverse_3(cur.right);
454
367
  };
455
368
  _traverse_3(this.root);
@@ -460,11 +373,12 @@ var BST = /** @class */ (function (_super) {
460
373
  while (queue.length > 0) {
461
374
  var cur = queue.shift();
462
375
  if (cur) {
463
- var compared = this._compare(cur.id, node.id);
464
- _sumByPropertyName(cur);
465
- if (cur.left && compared === types_1.CP.gt)
376
+ var compared = this._compare(cur.id, id);
377
+ if (compared === types_1.CP.gt)
378
+ _sumByPropertyName(cur);
379
+ if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
466
380
  queue.push(cur.left);
467
- else if (cur.right && compared === types_1.CP.gt)
381
+ if (cur.right && this._compare(cur.right.id, id) === types_1.CP.gt)
468
382
  queue.push(cur.right);
469
383
  }
470
384
  }
@@ -472,11 +386,20 @@ var BST = /** @class */ (function (_super) {
472
386
  }
473
387
  };
474
388
  /**
475
- * The `balance` function takes a sorted array of nodes and builds a balanced binary search tree using either a
476
- * recursive or iterative approach.
477
- * @returns The `balance()` function returns a boolean value.
389
+ * Balancing Adjustment:
390
+ * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
391
+ * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
392
+ *
393
+ * Use Cases and Efficiency:
394
+ * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
395
+ * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
396
+ */
397
+ /**
398
+ * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
399
+ * constructs a balanced binary search tree using either a recursive or iterative approach.
400
+ * @returns The function `perfectlyBalance()` returns a boolean value.
478
401
  */
479
- BST.prototype.balance = function () {
402
+ BST.prototype.perfectlyBalance = function () {
480
403
  var _this = this;
481
404
  var sorted = this.DFS('in', 'node'), n = sorted.length;
482
405
  this.clear();
@@ -488,7 +411,7 @@ var BST = /** @class */ (function (_super) {
488
411
  return;
489
412
  var m = l + Math.floor((r - l) / 2);
490
413
  var midNode = sorted[m];
491
- _this.add(midNode.id, midNode.val, midNode.count);
414
+ _this.add(midNode.id, midNode.val);
492
415
  buildBalanceBST_1(l, m - 1);
493
416
  buildBalanceBST_1(m + 1, r);
494
417
  };
@@ -504,7 +427,7 @@ var BST = /** @class */ (function (_super) {
504
427
  if (l <= r) {
505
428
  var m = l + Math.floor((r - l) / 2);
506
429
  var midNode = sorted[m];
507
- this.add(midNode.id, midNode.val, midNode.count);
430
+ this.add(midNode.id, midNode.val);
508
431
  stack.push([m + 1, r]);
509
432
  stack.push([l, m - 1]);
510
433
  }
@@ -514,9 +437,8 @@ var BST = /** @class */ (function (_super) {
514
437
  }
515
438
  };
516
439
  /**
517
- * The function `isAVLBalanced` checks if a binary search tree is balanced according to the AVL tree property.
518
- * @returns The function `isAVLBalanced()` returns a boolean value. It returns `true` if the binary search tree (BST)
519
- * is balanced according to the AVL tree property, and `false` otherwise.
440
+ * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
441
+ * @returns a boolean value.
520
442
  */
521
443
  BST.prototype.isAVLBalanced = function () {
522
444
  var _a, _b;
@@ -1,27 +1,16 @@
1
- import { BinaryTreeNodeId, RBColor, RBTreeOptions } from '../types';
1
+ import { BinaryTreeNodeId, RBColor, RBTreeNodeNested, RBTreeOptions } from '../types';
2
2
  import { IRBTree, IRBTreeNode } from '../interfaces/rb-tree';
3
3
  import { BST, BSTNode } from './bst';
4
- export declare class RBTreeNode<T, FAMILY extends RBTreeNode<T, FAMILY>> extends BSTNode<T, FAMILY> implements IRBTreeNode<T, FAMILY> {
5
- constructor(id: number, val: T, count?: number);
4
+ export declare class RBTreeNode<T = any, NEIGHBOR extends RBTreeNode<T, NEIGHBOR> = RBTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IRBTreeNode<T, NEIGHBOR> {
5
+ constructor(id: BinaryTreeNodeId, color: RBColor, val?: T);
6
6
  private _color;
7
7
  get color(): RBColor;
8
8
  set color(value: RBColor);
9
- /**
10
- * The function creates a new RBTreeNode with the given id, value, and count and returns it as a FAMILY object.
11
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
12
- * identify each node in the tree.
13
- * @param {T | null} [val] - The "val" parameter represents the value to be stored in the node. It can be of type T
14
- * (generic type) or null.
15
- * @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree
16
- * node.
17
- * @returns The method is returning a new instance of the RBTreeNode class, casted as a FAMILY type.
18
- */
19
- createNode(id: BinaryTreeNodeId, val?: T | null, count?: number): FAMILY;
20
9
  }
21
- export declare class RBTree<N extends RBTreeNode<N['val'], N>> extends BST<N> implements IRBTree<N> {
10
+ export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
22
11
  constructor(options?: RBTreeOptions);
23
- createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N;
24
- insert(id: number, val: N | null): void;
12
+ createNode(id: BinaryTreeNodeId, val?: N['val']): N;
13
+ insert(id: number, val?: N | null): void;
25
14
  private leftRotate;
26
15
  private rightRotate;
27
16
  private insertFixup;
@@ -20,9 +20,10 @@ var types_1 = require("../types");
20
20
  var bst_1 = require("./bst");
21
21
  var RBTreeNode = /** @class */ (function (_super) {
22
22
  __extends(RBTreeNode, _super);
23
- function RBTreeNode(id, val, count) {
24
- var _this = _super.call(this, id, val, count) || this;
23
+ function RBTreeNode(id, color, val) {
24
+ var _this = _super.call(this, id, val) || this;
25
25
  _this._color = types_1.RBColor.RED;
26
+ _this._color = color;
26
27
  return _this;
27
28
  }
28
29
  Object.defineProperty(RBTreeNode.prototype, "color", {
@@ -35,19 +36,6 @@ var RBTreeNode = /** @class */ (function (_super) {
35
36
  enumerable: false,
36
37
  configurable: true
37
38
  });
38
- /**
39
- * The function creates a new RBTreeNode with the given id, value, and count and returns it as a FAMILY object.
40
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
41
- * identify each node in the tree.
42
- * @param {T | null} [val] - The "val" parameter represents the value to be stored in the node. It can be of type T
43
- * (generic type) or null.
44
- * @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree
45
- * node.
46
- * @returns The method is returning a new instance of the RBTreeNode class, casted as a FAMILY type.
47
- */
48
- RBTreeNode.prototype.createNode = function (id, val, count) {
49
- return new RBTreeNode(id, val, count);
50
- };
51
39
  return RBTreeNode;
52
40
  }(bst_1.BSTNode));
53
41
  exports.RBTreeNode = RBTreeNode;
@@ -56,8 +44,8 @@ var RBTree = /** @class */ (function (_super) {
56
44
  function RBTree(options) {
57
45
  return _super.call(this, options) || this;
58
46
  }
59
- RBTree.prototype.createNode = function (id, val, count) {
60
- return new RBTreeNode(id, val, count);
47
+ RBTree.prototype.createNode = function (id, val) {
48
+ return new RBTreeNode(id, types_1.RBColor.RED, val);
61
49
  };
62
50
  // private override _root: BinaryTreeNode<N> | null = null;
63
51
  //