data-structure-typed 1.17.4 → 1.18.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 (293) hide show
  1. package/README.md +193 -66
  2. package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
  3. package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
  4. package/backup/recursive-type/src/assets/logo.png +0 -0
  5. package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
  6. package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +3 -0
  7. package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +288 -0
  8. package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +3 -0
  9. package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
  10. package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +1502 -0
  11. package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +503 -0
  12. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  13. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  14. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  15. package/backup/recursive-type/src/data-structures/binary-tree/index.ts +11 -0
  16. package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +110 -0
  17. package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +243 -0
  18. package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +3 -0
  19. package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +55 -0
  20. package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  21. package/backup/recursive-type/src/data-structures/diagrams/README.md +5 -0
  22. package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +985 -0
  23. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  24. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  25. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  26. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  27. package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  28. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  29. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  30. package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  31. package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
  32. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  33. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  34. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  35. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  36. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  37. package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +478 -0
  38. package/backup/recursive-type/src/data-structures/graph/index.ts +3 -0
  39. package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +293 -0
  40. package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +67 -0
  41. package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +56 -0
  42. package/backup/recursive-type/src/data-structures/hash/hash-table.ts +3 -0
  43. package/backup/recursive-type/src/data-structures/hash/index.ts +6 -0
  44. package/backup/recursive-type/src/data-structures/hash/pair.ts +3 -0
  45. package/backup/recursive-type/src/data-structures/hash/tree-map.ts +3 -0
  46. package/backup/recursive-type/src/data-structures/hash/tree-set.ts +3 -0
  47. package/backup/recursive-type/src/data-structures/heap/heap.ts +176 -0
  48. package/backup/recursive-type/src/data-structures/heap/index.ts +3 -0
  49. package/backup/recursive-type/src/data-structures/heap/max-heap.ts +31 -0
  50. package/backup/recursive-type/src/data-structures/heap/min-heap.ts +34 -0
  51. package/backup/recursive-type/src/data-structures/index.ts +15 -0
  52. package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +42 -0
  53. package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +1 -0
  54. package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +56 -0
  55. package/backup/recursive-type/src/data-structures/interfaces/bst.ts +1 -0
  56. package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +15 -0
  57. package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
  58. package/backup/recursive-type/src/data-structures/interfaces/heap.ts +1 -0
  59. package/backup/recursive-type/src/data-structures/interfaces/index.ts +13 -0
  60. package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +1 -0
  61. package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +1 -0
  62. package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +1 -0
  63. package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +1 -0
  64. package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +1 -0
  65. package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +3 -0
  66. package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
  67. package/backup/recursive-type/src/data-structures/linked-list/index.ts +3 -0
  68. package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +490 -0
  69. package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  70. package/backup/recursive-type/src/data-structures/matrix/index.ts +4 -0
  71. package/backup/recursive-type/src/data-structures/matrix/matrix.ts +27 -0
  72. package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +208 -0
  73. package/backup/recursive-type/src/data-structures/matrix/navigator.ts +122 -0
  74. package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +316 -0
  75. package/backup/recursive-type/src/data-structures/priority-queue/index.ts +3 -0
  76. package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
  77. package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
  78. package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +354 -0
  79. package/backup/recursive-type/src/data-structures/queue/deque.ts +251 -0
  80. package/backup/recursive-type/src/data-structures/queue/index.ts +2 -0
  81. package/backup/recursive-type/src/data-structures/queue/queue.ts +120 -0
  82. package/backup/recursive-type/src/data-structures/stack/index.ts +1 -0
  83. package/backup/recursive-type/src/data-structures/stack/stack.ts +98 -0
  84. package/backup/recursive-type/src/data-structures/tree/index.ts +1 -0
  85. package/backup/recursive-type/src/data-structures/tree/tree.ts +80 -0
  86. package/backup/recursive-type/src/data-structures/trie/index.ts +1 -0
  87. package/backup/recursive-type/src/data-structures/trie/trie.ts +227 -0
  88. package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +5 -0
  89. package/backup/recursive-type/src/data-structures/types/avl-tree.ts +8 -0
  90. package/backup/recursive-type/src/data-structures/types/binary-tree.ts +10 -0
  91. package/backup/recursive-type/src/data-structures/types/bst.ts +6 -0
  92. package/backup/recursive-type/src/data-structures/types/directed-graph.ts +8 -0
  93. package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +1 -0
  94. package/backup/recursive-type/src/data-structures/types/heap.ts +5 -0
  95. package/backup/recursive-type/src/data-structures/types/index.ts +12 -0
  96. package/backup/recursive-type/src/data-structures/types/navigator.ts +13 -0
  97. package/backup/recursive-type/src/data-structures/types/priority-queue.ts +9 -0
  98. package/backup/recursive-type/src/data-structures/types/segment-tree.ts +1 -0
  99. package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +1 -0
  100. package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +1 -0
  101. package/backup/recursive-type/src/index.ts +1 -0
  102. package/backup/recursive-type/src/utils/index.ts +2 -0
  103. package/backup/recursive-type/src/utils/types/index.ts +1 -0
  104. package/backup/recursive-type/src/utils/types/utils.ts +6 -0
  105. package/backup/recursive-type/src/utils/utils.ts +78 -0
  106. package/dist/data-structures/binary-tree/avl-tree.d.ts +19 -25
  107. package/dist/data-structures/binary-tree/avl-tree.js +12 -20
  108. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -1
  109. package/dist/data-structures/binary-tree/binary-indexed-tree.js +10 -0
  110. package/dist/data-structures/binary-tree/binary-tree.d.ts +135 -161
  111. package/dist/data-structures/binary-tree/binary-tree.js +192 -164
  112. package/dist/data-structures/binary-tree/bst.d.ts +21 -21
  113. package/dist/data-structures/binary-tree/bst.js +33 -36
  114. package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -2
  115. package/dist/data-structures/binary-tree/rb-tree.js +68 -5
  116. package/dist/data-structures/binary-tree/segment-tree.d.ts +17 -39
  117. package/dist/data-structures/binary-tree/segment-tree.js +34 -47
  118. package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -8
  119. package/dist/data-structures/binary-tree/tree-multiset.js +7 -6
  120. package/dist/data-structures/graph/abstract-graph.d.ts +56 -71
  121. package/dist/data-structures/graph/abstract-graph.js +87 -92
  122. package/dist/data-structures/graph/directed-graph.d.ts +128 -105
  123. package/dist/data-structures/graph/directed-graph.js +161 -121
  124. package/dist/data-structures/graph/undirected-graph.d.ts +81 -62
  125. package/dist/data-structures/graph/undirected-graph.js +99 -78
  126. package/dist/data-structures/hash/coordinate-map.d.ts +1 -5
  127. package/dist/data-structures/hash/coordinate-map.js +3 -9
  128. package/dist/data-structures/hash/coordinate-set.d.ts +2 -6
  129. package/dist/data-structures/hash/coordinate-set.js +3 -9
  130. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  131. package/dist/data-structures/hash/hash-table.js +7 -0
  132. package/dist/data-structures/hash/pair.d.ts +2 -1
  133. package/dist/data-structures/hash/pair.js +7 -0
  134. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  135. package/dist/data-structures/hash/tree-map.js +7 -0
  136. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  137. package/dist/data-structures/hash/tree-set.js +7 -0
  138. package/dist/data-structures/heap/heap.d.ts +0 -14
  139. package/dist/data-structures/heap/heap.js +3 -27
  140. package/dist/data-structures/index.d.ts +1 -0
  141. package/dist/data-structures/index.js +1 -0
  142. package/dist/data-structures/interfaces/abstract-graph.d.ts +22 -0
  143. package/dist/data-structures/interfaces/abstract-graph.js +2 -0
  144. package/dist/data-structures/interfaces/avl-tree.d.ts +1 -0
  145. package/dist/data-structures/interfaces/avl-tree.js +2 -0
  146. package/dist/data-structures/interfaces/binary-tree.d.ts +27 -0
  147. package/dist/data-structures/interfaces/binary-tree.js +2 -0
  148. package/dist/data-structures/interfaces/bst.d.ts +1 -0
  149. package/dist/data-structures/interfaces/bst.js +2 -0
  150. package/dist/data-structures/interfaces/directed-graph.d.ts +9 -0
  151. package/dist/data-structures/interfaces/directed-graph.js +2 -0
  152. package/dist/data-structures/interfaces/doubly-linked-list.d.ts +1 -0
  153. package/dist/data-structures/interfaces/doubly-linked-list.js +2 -0
  154. package/dist/data-structures/interfaces/heap.d.ts +1 -0
  155. package/dist/data-structures/interfaces/heap.js +2 -0
  156. package/dist/data-structures/interfaces/index.d.ts +13 -0
  157. package/dist/data-structures/interfaces/index.js +29 -0
  158. package/dist/data-structures/interfaces/navigator.d.ts +1 -0
  159. package/dist/data-structures/interfaces/navigator.js +2 -0
  160. package/dist/data-structures/interfaces/priority-queue.d.ts +1 -0
  161. package/dist/data-structures/interfaces/priority-queue.js +2 -0
  162. package/dist/data-structures/interfaces/segment-tree.d.ts +1 -0
  163. package/dist/data-structures/interfaces/segment-tree.js +2 -0
  164. package/dist/data-structures/interfaces/singly-linked-list.d.ts +1 -0
  165. package/dist/data-structures/interfaces/singly-linked-list.js +2 -0
  166. package/dist/data-structures/interfaces/tree-multiset.d.ts +1 -0
  167. package/dist/data-structures/interfaces/tree-multiset.js +2 -0
  168. package/dist/data-structures/interfaces/undirected-graph.d.ts +2 -0
  169. package/dist/data-structures/interfaces/undirected-graph.js +2 -0
  170. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -3
  171. package/dist/data-structures/linked-list/doubly-linked-list.js +12 -18
  172. package/dist/data-structures/linked-list/singly-linked-list.d.ts +1 -2
  173. package/dist/data-structures/linked-list/singly-linked-list.js +12 -15
  174. package/dist/data-structures/priority-queue/priority-queue.d.ts +1 -1
  175. package/dist/data-structures/priority-queue/priority-queue.js +4 -4
  176. package/dist/data-structures/queue/deque.d.ts +5 -5
  177. package/dist/data-structures/queue/deque.js +6 -6
  178. package/dist/data-structures/queue/queue.d.ts +1 -1
  179. package/dist/data-structures/stack/stack.d.ts +1 -1
  180. package/dist/data-structures/tree/tree.d.ts +12 -4
  181. package/dist/data-structures/tree/tree.js +44 -12
  182. package/dist/data-structures/trie/trie.d.ts +3 -3
  183. package/dist/data-structures/trie/trie.js +10 -10
  184. package/dist/data-structures/types/abstract-graph.d.ts +1 -20
  185. package/dist/data-structures/types/avl-tree.d.ts +5 -4
  186. package/dist/data-structures/types/binary-tree.d.ts +6 -5
  187. package/dist/data-structures/types/bst.d.ts +4 -3
  188. package/dist/data-structures/types/directed-graph.d.ts +5 -9
  189. package/dist/data-structures/types/directed-graph.js +7 -0
  190. package/dist/data-structures/types/doubly-linked-list.d.ts +1 -1
  191. package/dist/data-structures/types/heap.d.ts +2 -2
  192. package/dist/data-structures/types/index.d.ts +0 -1
  193. package/dist/data-structures/types/index.js +0 -1
  194. package/dist/data-structures/types/navigator.d.ts +2 -2
  195. package/dist/data-structures/types/priority-queue.d.ts +2 -2
  196. package/dist/data-structures/types/tree-multiset.d.ts +3 -4
  197. package/docs/assets/search.js +1 -1
  198. package/docs/classes/AVLTree.html +552 -405
  199. package/docs/classes/AVLTreeNode.html +107 -242
  200. package/docs/classes/AaTree.html +18 -13
  201. package/docs/classes/AbstractEdge.html +77 -68
  202. package/docs/classes/AbstractGraph.html +223 -115
  203. package/docs/classes/AbstractVertex.html +71 -45
  204. package/docs/classes/ArrayDeque.html +31 -26
  205. package/docs/classes/BST.html +543 -391
  206. package/docs/classes/BSTNode.html +107 -236
  207. package/docs/classes/BTree.html +18 -13
  208. package/docs/classes/BinaryIndexedTree.html +56 -21
  209. package/docs/classes/BinaryTree.html +564 -355
  210. package/docs/classes/BinaryTreeNode.html +146 -199
  211. package/docs/classes/Character.html +21 -16
  212. package/docs/classes/CoordinateMap.html +49 -52
  213. package/docs/classes/CoordinateSet.html +50 -53
  214. package/docs/classes/Deque.html +51 -68
  215. package/docs/classes/DirectedEdge.html +98 -103
  216. package/docs/classes/DirectedGraph.html +449 -210
  217. package/docs/classes/DirectedVertex.html +63 -52
  218. package/docs/classes/DoublyLinkedList.html +56 -71
  219. package/docs/classes/DoublyLinkedListNode.html +28 -23
  220. package/docs/classes/HashTable.html +155 -0
  221. package/docs/classes/Heap.html +33 -109
  222. package/docs/classes/HeapItem.html +25 -20
  223. package/docs/classes/Matrix2D.html +33 -28
  224. package/docs/classes/MatrixNTI2D.html +21 -16
  225. package/docs/classes/MaxHeap.html +33 -114
  226. package/docs/classes/MaxPriorityQueue.html +71 -61
  227. package/docs/classes/MinHeap.html +33 -114
  228. package/docs/classes/MinPriorityQueue.html +71 -61
  229. package/docs/classes/Navigator.html +28 -23
  230. package/docs/classes/ObjectDeque.html +66 -51
  231. package/docs/classes/{RBTree.html → Pair.html} +25 -20
  232. package/docs/classes/PriorityQueue.html +66 -56
  233. package/docs/classes/Queue.html +33 -28
  234. package/docs/classes/SegmentTree.html +129 -57
  235. package/docs/classes/SegmentTreeNode.html +62 -140
  236. package/docs/classes/SinglyLinkedList.html +53 -58
  237. package/docs/classes/SinglyLinkedListNode.html +25 -20
  238. package/docs/classes/SkipLinkedList.html +18 -13
  239. package/docs/classes/SplayTree.html +18 -13
  240. package/docs/classes/Stack.html +31 -26
  241. package/docs/classes/TreeMap.html +155 -0
  242. package/docs/classes/TreeMultiSet.html +541 -388
  243. package/docs/classes/TreeNode.html +125 -35
  244. package/docs/classes/TreeSet.html +155 -0
  245. package/docs/classes/Trie.html +30 -25
  246. package/docs/classes/TrieNode.html +33 -28
  247. package/docs/classes/TwoThreeTree.html +18 -13
  248. package/docs/classes/UndirectedEdge.html +87 -80
  249. package/docs/classes/UndirectedGraph.html +356 -178
  250. package/docs/classes/UndirectedVertex.html +63 -52
  251. package/docs/classes/Vector2D.html +45 -40
  252. package/docs/enums/CP.html +21 -16
  253. package/docs/enums/FamilyPosition.html +21 -16
  254. package/docs/enums/LoopType.html +20 -15
  255. package/docs/{interfaces/AVLTreeDeleted.html → enums/TopologicalProperty.html} +47 -44
  256. package/docs/index.html +203 -69
  257. package/docs/interfaces/{PriorityQueueOptions.html → IBinaryTree.html} +49 -40
  258. package/docs/interfaces/IBinaryTreeNode.html +383 -0
  259. package/docs/interfaces/IDirectedGraph.html +24 -19
  260. package/docs/interfaces/IGraph.html +122 -89
  261. package/docs/interfaces/{HeapOptions.html → IUNDirectedGraph.html} +26 -53
  262. package/docs/modules.html +33 -23
  263. package/docs/types/{ToThunkFn.html → AVLTreeDeleted.html} +31 -22
  264. package/docs/types/BSTComparator.html +18 -13
  265. package/docs/types/BSTDeletedResult.html +23 -18
  266. package/docs/types/BinaryTreeDeleted.html +23 -18
  267. package/docs/types/BinaryTreeNodeId.html +18 -13
  268. package/docs/types/BinaryTreeNodePropertyName.html +18 -13
  269. package/docs/types/DFSOrderPattern.html +18 -13
  270. package/docs/types/DijkstraResult.html +18 -13
  271. package/docs/types/Direction.html +18 -13
  272. package/docs/types/{DoublyLinkedListGetBy.html → EdgeId.html} +22 -17
  273. package/docs/types/{TrlFn.html → HeapOptions.html} +33 -20
  274. package/docs/types/{TrlAsyncFn.html → NavigatorParams.html} +46 -20
  275. package/docs/types/NodeOrPropertyName.html +18 -13
  276. package/docs/types/PriorityQueueComparator.html +18 -13
  277. package/docs/types/PriorityQueueDFSOrderPattern.html +18 -13
  278. package/docs/types/{SpecifyOptional.html → PriorityQueueOptions.html} +32 -20
  279. package/docs/types/RecursiveAVLTreeNode.html +135 -0
  280. package/docs/types/RecursiveBSTNode.html +135 -0
  281. package/docs/types/RecursiveBinaryTreeNode.html +135 -0
  282. package/docs/types/ResultByProperty.html +21 -16
  283. package/docs/types/ResultsByProperty.html +21 -16
  284. package/docs/types/SegmentTreeNodeVal.html +18 -13
  285. package/docs/types/TopologicalStatus.html +18 -13
  286. package/docs/types/TreeMultiSetDeletedResult.html +23 -18
  287. package/docs/types/Turning.html +18 -13
  288. package/docs/types/VertexId.html +18 -13
  289. package/notes/note.md +12 -1
  290. package/package.json +11 -3
  291. package/tsconfig.json +2 -2
  292. package/docs/interfaces/NavigatorParams.html +0 -197
  293. package/docs/types/Thunk.html +0 -133
@@ -0,0 +1,503 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type {
9
+ BinaryTreeNodeId,
10
+ BinaryTreeNodePropertyName,
11
+ BSTComparator,
12
+ BSTDeletedResult,
13
+ RecursiveBSTNode
14
+ } from '../types';
15
+ import {BinaryTree, BinaryTreeNode, FamilyPosition, LoopType,} from './binary-tree';
16
+ import {IBinaryTree, IBinaryTreeNode} from '../interfaces';
17
+
18
+ export enum CP {lt = -1, eq = 0, gt = 1}
19
+
20
+ export class BSTNode<T, FAMILY extends BSTNode<T, FAMILY> = RecursiveBSTNode<T>> extends BinaryTreeNode<T, FAMILY> implements IBinaryTreeNode<T, FAMILY> {
21
+
22
+ }
23
+
24
+ export class BST<N extends BSTNode<N['val'], N> = BSTNode<number>> extends BinaryTree<N> implements IBinaryTree<N> {
25
+ /**
26
+ * The constructor function accepts an optional options object and sets the comparator property if provided.
27
+ * @param [options] - An optional object that can contain the following properties:
28
+ */
29
+ constructor(options?: {
30
+ comparator?: BSTComparator,
31
+ loopType?: LoopType
32
+ }) {
33
+ super(options);
34
+ if (options !== undefined) {
35
+ const {comparator} = options;
36
+ if (comparator !== undefined) {
37
+ this._comparator = comparator;
38
+ }
39
+ }
40
+ }
41
+
42
+ override _createNode(id: BinaryTreeNodeId, val: N['val'] | null, count?: number): N | null {
43
+ const node = val !== null ? new BSTNode<N['val'], N>(id, val, count) : null;
44
+ return node as N;
45
+ }
46
+
47
+ /**
48
+ * The `add` function inserts a new node into a binary search tree, updating the count and value of an existing node if
49
+ * the ID matches, and returns the inserted node.
50
+ * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node. It is used to
51
+ * determine the position of the node in the binary search tree.
52
+ * @param {N | null} val - The `val` parameter represents the value to be stored in the binary search tree node. It can
53
+ * be of type `N` (the generic type) or `null`.
54
+ * @param {number} [count=1] - The `count` parameter represents the number of times the value should be inserted into
55
+ * the binary search tree. By default, it is set to 1, meaning that if no count is specified, the value will be
56
+ * inserted once.
57
+ * @returns The method `add` returns a `N` object or `null`.
58
+ */
59
+ override add(id: BinaryTreeNodeId, val: N['val'] | null, count: number = 1): N | null {
60
+ let inserted: N | null = null;
61
+ const newNode = this._createNode(id, val, count);
62
+ if (this.root === null) {
63
+ this._setRoot(newNode);
64
+ this._setSize(this.size + 1);
65
+ this._setCount(this.count + count);
66
+ inserted = (this.root);
67
+ } else {
68
+ let cur = this.root;
69
+ let traversing = true;
70
+ while (traversing) {
71
+ if (cur !== null && newNode !== null) {
72
+ if (this._compare(cur.id, id) === CP.eq) {
73
+ if (newNode) {
74
+ cur.count += newNode.count;
75
+ this._setCount(this.count + newNode.count);
76
+ cur.val = newNode.val;
77
+ }
78
+ //Duplicates are not accepted.
79
+ traversing = false;
80
+ inserted = cur;
81
+ } else if (this._compare(cur.id, id) === CP.gt) {
82
+ // Traverse left of the node
83
+ if (cur.left === undefined) {
84
+ if (newNode) {
85
+ newNode.parent = cur;
86
+ newNode.familyPosition = FamilyPosition.left;
87
+ }
88
+ //Add to the left of the current node
89
+ cur.left = newNode;
90
+ this._setSize(this.size + 1);
91
+ this._setCount(this.count + newNode.count);
92
+ traversing = false;
93
+ inserted = cur.left;
94
+ } else {
95
+ //Traverse the left of the current node
96
+ if (cur.left) cur = cur.left;
97
+ }
98
+ } else if (this._compare(cur.id, id) === CP.lt) {
99
+ // Traverse right of the node
100
+ if (cur.right === undefined) {
101
+ if (newNode) {
102
+ newNode.parent = cur;
103
+ newNode.familyPosition = FamilyPosition.right;
104
+ }
105
+ //Add to the right of the current node
106
+ cur.right = newNode;
107
+ this._setSize(this.size + 1);
108
+ this._setCount(this.count + newNode.count);
109
+ traversing = false;
110
+ inserted = (cur.right);
111
+ } else {
112
+ //Traverse the left of the current node
113
+ if (cur.right) cur = cur.right;
114
+ }
115
+ }
116
+ } else {
117
+ traversing = false;
118
+ }
119
+ }
120
+ }
121
+ return inserted;
122
+ }
123
+
124
+ /**
125
+ * The `get` function returns the first node in a binary search tree that matches the given property value or name.
126
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
127
+ * generic type `N`. It represents the value of the property that you want to search for in the binary search tree.
128
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
129
+ * specifies the property name to use for searching the binary search tree nodes. If not provided, it defaults to
130
+ * `'id'`.
131
+ * @returns The method is returning a N object or null.
132
+ */
133
+ override get(nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName): N | null{
134
+ propertyName = propertyName ?? 'id';
135
+ return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
136
+ }
137
+
138
+ /**
139
+ * The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
140
+ * leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
141
+ * @returns The function `lastKey()` returns the ID of the rightmost node in a binary tree. If the comparison between
142
+ * the first two elements in the tree is less than, it returns the ID of the rightmost node. If the comparison is
143
+ * greater than, it returns the ID of the leftmost node. Otherwise, it also returns the ID of the rightmost node. If
144
+ * there are no nodes in
145
+ */
146
+ lastKey() {
147
+ if (this._compare(0, 1) === CP.lt) return this.getRightMost()?.id ?? 0;
148
+ else if (this._compare(0, 1) === CP.gt) return this.getLeftMost()?.id ?? 0;
149
+ else return this.getRightMost()?.id ?? 0;
150
+ }
151
+
152
+ /**
153
+ * The `remove` function in this TypeScript code removes a node from a binary search tree and returns information about
154
+ * the deleted node and any nodes that need to be balanced.
155
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that needs to be removed
156
+ * from the binary search tree.
157
+ * @param {boolean} [ignoreCount] - A boolean flag indicating whether to ignore the count of the node being removed. If
158
+ * set to true, the count of the node will not be considered and the node will be removed regardless of its count. If
159
+ * set to false or not provided, the count of the node will be taken into account and the
160
+ * @returns an array of `BSTDeletedResult<N>` objects.
161
+ */
162
+ override remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BSTDeletedResult<N>[] {
163
+ const bstDeletedResult: BSTDeletedResult<N>[] = [];
164
+ if (!this.root) return bstDeletedResult;
165
+
166
+ const curr: N | null = this.get(id);
167
+ if (!curr) return bstDeletedResult;
168
+
169
+ const parent: N | null = curr?.parent ? curr.parent : null;
170
+ let needBalanced: N | null = null, orgCurrent = curr;
171
+
172
+ if (curr.count > 1 && !ignoreCount) {
173
+ curr.count--;
174
+ this._setCount(this.count - 1);
175
+ } else {
176
+ if (!curr.left) {
177
+ if (!parent) {
178
+ if (curr.right !== undefined) this._setRoot(curr.right);
179
+ } else {
180
+ switch (curr.familyPosition) {
181
+ case FamilyPosition.left:
182
+ parent.left = curr.right;
183
+ break;
184
+ case FamilyPosition.right:
185
+ parent.right = curr.right;
186
+ break;
187
+ }
188
+ needBalanced = parent;
189
+ }
190
+ } else {
191
+ const leftSubTreeMax = curr.left ? this.getRightMost(curr.left) : null;
192
+ if (leftSubTreeMax) {
193
+ const parentOfLeftSubTreeMax = leftSubTreeMax.parent;
194
+ orgCurrent = curr.swapLocation(leftSubTreeMax);
195
+ if (parentOfLeftSubTreeMax) {
196
+ if (parentOfLeftSubTreeMax.right === leftSubTreeMax) parentOfLeftSubTreeMax.right = leftSubTreeMax.left;
197
+ else parentOfLeftSubTreeMax.left = leftSubTreeMax.left;
198
+ needBalanced = parentOfLeftSubTreeMax;
199
+ }
200
+ }
201
+ }
202
+ this._setSize(this.size - 1);
203
+ this._setCount(this.count - curr.count);
204
+ }
205
+
206
+ bstDeletedResult.push({deleted: orgCurrent, needBalanced});
207
+ return bstDeletedResult;
208
+ }
209
+
210
+ /**
211
+ * The function `getNodes` returns an array of binary search tree nodes that match a given property value, with the
212
+ * option to specify the property name and whether to return only one node.
213
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
214
+ * generic type `N`. It represents the property value that you want to search for in the binary search tree.
215
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
216
+ * specifies the property of the nodes to compare with the `nodeProperty` parameter. If not provided, it defaults to
217
+ * `'id'`.
218
+ * @param {boolean} [onlyOne] - A boolean value indicating whether to return only one node that matches the given
219
+ * nodeProperty. If set to true, the function will stop traversing the tree and return the first matching node. If set
220
+ * to false or not provided, the function will return all nodes that match the given nodeProperty.
221
+ * @returns an array of N objects.
222
+ */
223
+ override getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean): N[] {
224
+ propertyName = propertyName ?? 'id';
225
+ if (!this.root) return [];
226
+ const result: N[] = [];
227
+
228
+ if (this.loopType === LoopType.recursive) {
229
+ const _traverse = (cur: N) => {
230
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return;
231
+
232
+ if (!cur.left && !cur.right) return;
233
+ if (propertyName === 'id') {
234
+ if (this._compare(cur.id, nodeProperty as number) === CP.gt) cur.left && _traverse(cur.left);
235
+ if (this._compare(cur.id, nodeProperty as number) === CP.lt) cur.right && _traverse(cur.right);
236
+ } else {
237
+ cur.left && _traverse(cur.left);
238
+ cur.right && _traverse(cur.right);
239
+ }
240
+ }
241
+
242
+ _traverse(this.root);
243
+ } else {
244
+ const queue: N[] = [this.root];
245
+ while (queue.length > 0) {
246
+ const cur = queue.shift();
247
+ if (cur) {
248
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result;
249
+ if (propertyName === 'id') {
250
+ if (this._compare(cur.id, nodeProperty as number) === CP.gt) cur.left && queue.push(cur.left);
251
+ if (this._compare(cur.id, nodeProperty as number) === CP.lt) cur.right && queue.push(cur.right);
252
+ } else {
253
+ cur.left && queue.push(cur.left);
254
+ cur.right && queue.push(cur.right);
255
+ }
256
+ }
257
+ }
258
+ }
259
+
260
+ return result;
261
+ }
262
+
263
+ // --- start additional functions
264
+ /**
265
+ * The `lesserSum` function calculates the sum of a specified property in all nodes with an ID less than a given ID in
266
+ * a binary search tree.
267
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node for which you want to
268
+ * calculate the lesser sum.
269
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
270
+ * specifies the property of the binary tree node to use for calculating the sum. If not provided, it defaults to 'id'.
271
+ * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
272
+ * binary search tree that have a property value lesser than the given `id`.
273
+ */
274
+ lesserSum(id: BinaryTreeNodeId, propertyName ?: BinaryTreeNodePropertyName): number {
275
+ propertyName = propertyName ?? 'id';
276
+ if (!this.root) return 0;
277
+
278
+ const getSumByPropertyName = (cur: N) => {
279
+ let needSum: number;
280
+ switch (propertyName) {
281
+ case 'id':
282
+ needSum = cur.id;
283
+ break;
284
+ case 'count':
285
+ needSum = cur.count;
286
+ break;
287
+ default:
288
+ needSum = cur.id;
289
+ break;
290
+ }
291
+ return needSum;
292
+ }
293
+
294
+ let sum = 0;
295
+
296
+ if (this.loopType === LoopType.recursive) {
297
+ const _traverse = (cur: N): void => {
298
+ const compared = this._compare(cur.id, id);
299
+ if (compared === CP.eq) {
300
+ if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
301
+ return;
302
+ } else if (compared === CP.lt) {
303
+ if (cur.left) sum += this.subTreeSum(cur.left, propertyName);
304
+ sum += getSumByPropertyName(cur);
305
+ if (cur.right) _traverse(cur.right);
306
+ else return;
307
+ } else {
308
+ if (cur.left) _traverse(cur.left);
309
+ else return;
310
+ }
311
+ };
312
+
313
+ _traverse(this.root);
314
+ } else {
315
+ const queue: N[] = [this.root];
316
+ while (queue.length > 0) {
317
+ const cur = queue.shift();
318
+ if (cur) {
319
+ const compared = this._compare(cur.id, id);
320
+ if (compared === CP.eq) {
321
+ if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
322
+ return sum;
323
+ } else if (compared === CP.lt) { // todo maybe a bug
324
+ if (cur.left) sum += this.subTreeSum(cur.left, propertyName);
325
+ sum += getSumByPropertyName(cur);
326
+ if (cur.right) queue.push(cur.right);
327
+ else return sum;
328
+ } else {
329
+ if (cur.left) queue.push(cur.left);
330
+ else return sum;
331
+ }
332
+ }
333
+ }
334
+ }
335
+
336
+ return sum;
337
+ }
338
+
339
+ /**
340
+ * The function `allGreaterNodesAdd` updates the value of a specified property for all nodes in a binary search tree
341
+ * that have a greater value than a given node.
342
+ * @param node - The `node` parameter is of type `N`, which represents a node in a binary search tree. It
343
+ * contains properties such as `id` and `count`.
344
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
345
+ * each node should be increased.
346
+ * @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
347
+ * property of the BSTNode to be modified. It can be either 'id' or 'count'. If propertyName is not provided, it
348
+ * defaults to 'id'.
349
+ * @returns a boolean value.
350
+ */
351
+ allGreaterNodesAdd(node: N, delta: number, propertyName ?: BinaryTreeNodePropertyName): boolean {
352
+ propertyName = propertyName ?? 'id';
353
+ if (!this.root) return false;
354
+
355
+ const _sumByPropertyName = (cur: N) => {
356
+ switch (propertyName) {
357
+ case 'id':
358
+ cur.id += delta;
359
+ break;
360
+ case 'count':
361
+ cur.count += delta;
362
+ break;
363
+ default:
364
+ cur.id += delta;
365
+ break;
366
+ }
367
+ }
368
+
369
+ if (this.loopType === LoopType.recursive) {
370
+ const _traverse = (cur: N) => {
371
+ const compared = this._compare(cur.id, node.id);
372
+ _sumByPropertyName(cur);
373
+
374
+ if (!cur.left && !cur.right) return;
375
+ if (cur.left && compared === CP.gt) _traverse(cur.left);
376
+ else if (cur.right && compared === CP.gt) _traverse(cur.right);
377
+ };
378
+
379
+ _traverse(this.root);
380
+ return true;
381
+ } else {
382
+ const queue: N[] = [this.root];
383
+ while (queue.length > 0) {
384
+ const cur = queue.shift();
385
+ if (cur) {
386
+ const compared = this._compare(cur.id, node.id);
387
+ _sumByPropertyName(cur);
388
+
389
+ if (cur.left && compared === CP.gt) queue.push(cur.left);
390
+ else if (cur.right && compared === CP.gt) queue.push(cur.right);
391
+ }
392
+ }
393
+ return true;
394
+ }
395
+ }
396
+
397
+ /**
398
+ * The `balance` function takes a sorted array of nodes and builds a balanced binary search tree using either a
399
+ * recursive or iterative approach.
400
+ * @returns The `balance()` function returns a boolean value.
401
+ */
402
+ balance(): boolean {
403
+ const sorted = this.DFS('in', 'node'), n = sorted.length;
404
+ this.clear();
405
+
406
+ if (sorted.length < 1) return false;
407
+ if (this.loopType === LoopType.recursive) {
408
+ const buildBalanceBST = (l: number, r: number) => {
409
+ if (l > r) return;
410
+ const m = l + Math.floor((r - l) / 2);
411
+ const midNode = sorted[m];
412
+ this.add(midNode.id, midNode.val, midNode.count);
413
+ buildBalanceBST(l, m - 1);
414
+ buildBalanceBST(m + 1, r);
415
+ };
416
+
417
+ buildBalanceBST(0, n - 1);
418
+ return true;
419
+ } else {
420
+ const stack: [[number, number]] = [[0, n - 1]];
421
+ while (stack.length > 0) {
422
+ const popped = stack.pop();
423
+ if (popped) {
424
+ const [l, r] = popped;
425
+ if (l <= r) {
426
+ const m = l + Math.floor((r - l) / 2);
427
+ const midNode = sorted[m];
428
+ this.add(midNode.id, midNode.val, midNode.count);
429
+ stack.push([m + 1, r]);
430
+ stack.push([l, m - 1]);
431
+ }
432
+ }
433
+ }
434
+ return true;
435
+ }
436
+ }
437
+
438
+ /**
439
+ * The function `isAVLBalanced` checks if a binary search tree is balanced according to the AVL tree property.
440
+ * @returns The function `isAVLBalanced()` returns a boolean value. It returns `true` if the binary search tree (BST)
441
+ * is balanced according to the AVL tree property, and `false` otherwise.
442
+ */
443
+ isAVLBalanced(): boolean {
444
+ if (!this.root) return true;
445
+
446
+ let balanced = true;
447
+
448
+ if (this.loopType === LoopType.recursive) {
449
+ const _height = (cur: N | null | undefined): number => {
450
+ if (!cur) return 0;
451
+ const leftHeight = _height(cur.left), rightHeight = _height(cur.right);
452
+ if (Math.abs(leftHeight - rightHeight) > 1) balanced = false;
453
+ return Math.max(leftHeight, rightHeight) + 1;
454
+ };
455
+ _height(this.root);
456
+ } else {
457
+ const stack: N[] = [];
458
+ let node: N | null | undefined = this.root, last: N | null = null;
459
+ const depths: Map<N, number> = new Map();
460
+
461
+ while (stack.length > 0 || node) {
462
+ if (node) {
463
+ stack.push(node);
464
+ node = node.left;
465
+ } else {
466
+ node = stack[stack.length - 1]
467
+ if (!node.right || last === node.right) {
468
+ node = stack.pop();
469
+ if (node) {
470
+ const left = node.left ? depths.get(node.left) ?? -1 : -1;
471
+ const right = node.right ? depths.get(node.right) ?? -1 : -1;
472
+ if (Math.abs(left - right) > 1) return false;
473
+ depths.set(node, 1 + Math.max(left, right));
474
+ last = node;
475
+ node = null;
476
+ }
477
+ } else node = node.right
478
+ }
479
+ }
480
+ }
481
+
482
+ return balanced;
483
+ }
484
+
485
+ protected _comparator: BSTComparator = (a, b) => a - b;
486
+
487
+ /**
488
+ * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
489
+ * greater than, less than, or equal to the second ID.
490
+ * @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
491
+ * @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
492
+ * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
493
+ * than), or CP.eq (equal).
494
+ */
495
+ protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP {
496
+ const compared = this._comparator(a, b);
497
+ if (compared > 0) return CP.gt;
498
+ else if (compared < 0) return CP.lt;
499
+ else return CP.eq;
500
+ }
501
+
502
+ // --- end additional functions
503
+ }
@@ -0,0 +1,11 @@
1
+ export * from './binary-tree';
2
+ export * from './bst';
3
+ export * from './binary-indexed-tree';
4
+ export * from './segment-tree';
5
+ export * from './avl-tree';
6
+ export * from './b-tree';
7
+ export * from './rb-tree';
8
+ export * from './splay-tree';
9
+ export * from './aa-tree';
10
+ export * from './tree-multiset';
11
+ export * from './two-three-tree';
@@ -0,0 +1,110 @@
1
+ import {BinaryTree, BinaryTreeNode, LoopType} from './binary-tree';
2
+ import {IBinaryTree, IBinaryTreeNode} from '../interfaces';
3
+
4
+ enum RBColor { Red, Black }
5
+
6
+ class RBNode<T, FAMILY extends RBNode<T, FAMILY>> extends BinaryTreeNode<T, FAMILY> implements IBinaryTreeNode<T, FAMILY> {
7
+ // override createNode(id: BinaryTreeNodeId, val: T | null, count?: number): RBNode<T> | null {
8
+ // return val !== null ? new RBNode<T>(id, val, count) : null;
9
+ // }
10
+
11
+ constructor(id: number, val: T, count?: number) {
12
+ super(id, val, count);
13
+ }
14
+
15
+ private _color: RBColor = RBColor.Red;
16
+
17
+ get color(): RBColor {
18
+ return this._color;
19
+ }
20
+
21
+ set color(value: RBColor) {
22
+ this._color = value;
23
+ }
24
+
25
+ // private override _parent: RBNode<T> | null;
26
+ // override set parent(v: RBNode<T> | null) {
27
+ // this._parent = v;
28
+ // }
29
+ // override get parent(): RBNode<T> | null {
30
+ // return this._parent;
31
+ // }
32
+ // private override _left?: RBNode<T> | null;
33
+ //
34
+ // override get left(): RBNode<T> | null | undefined {
35
+ // return this._left;
36
+ // }
37
+ //
38
+ // override set left(v: RBNode<T> | null | undefined) {
39
+ // if (v) {
40
+ // v.parent = this;
41
+ // v.familyPosition = FamilyPosition.left;
42
+ // }
43
+ // this._left = v;
44
+ // }
45
+ //
46
+ // private override _right?: RBNode<T> | null;
47
+ //
48
+ // override get right(): RBNode<T> | null | undefined {
49
+ // return this._right;
50
+ // }
51
+ //
52
+ // override set right(v: RBNode<T> | null | undefined) {
53
+ // if (v) {
54
+ // v.parent = this;
55
+ // v.familyPosition = FamilyPosition.right;
56
+ // }
57
+ // this._right = v;
58
+ // }
59
+ }
60
+
61
+ class RBTree<N extends RBNode<N['val'], N>> extends BinaryTree<N> implements IBinaryTree<N> {
62
+ constructor(options?: {
63
+ loopType?: LoopType,
64
+ autoIncrementId?: boolean,
65
+ isDuplicatedVal?: boolean
66
+ }) {
67
+ super(options);
68
+ }
69
+
70
+ // override _createNode(id: BinaryTreeNodeId, val: N | null, count?: number): RBNode<N> | null {
71
+ // return val !== null ? new RBNode<N>(id, val, count) : null;
72
+ // }
73
+
74
+ // private override _root: BinaryTreeNode<N> | null = null;
75
+ //
76
+ // override get root(): BinaryTreeNode<N> | null {
77
+ // return this._root;
78
+ // }
79
+
80
+ insert(id: number, val: N | null) {
81
+
82
+ }
83
+
84
+ private leftRotate(node: N) {
85
+
86
+ }
87
+
88
+ private rightRotate(node: N) {
89
+
90
+ }
91
+
92
+ private insertFixup(node: N) {
93
+
94
+ }
95
+
96
+ private deleteFixup(node: N) {
97
+
98
+ }
99
+
100
+ private transplant(u: N, v: N) {
101
+
102
+ }
103
+
104
+ // override remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeleted<N>[] {
105
+ //
106
+ // return [{deleted: new N(0, 0), needBalanced: null}];
107
+ // }
108
+
109
+
110
+ }