data-structure-typed 1.18.7 → 1.18.8

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 (260) hide show
  1. package/README.md +154 -366
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +29 -32
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.js +179 -136
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +4 -4
  5. package/dist/data-structures/binary-tree/avl-tree.js +2 -2
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +3 -3
  7. package/dist/data-structures/binary-tree/binary-tree.js +4 -4
  8. package/dist/data-structures/binary-tree/bst.d.ts +13 -25
  9. package/dist/data-structures/binary-tree/bst.js +20 -74
  10. package/dist/data-structures/binary-tree/rb-tree.d.ts +6 -6
  11. package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
  12. package/dist/data-structures/binary-tree/tree-multiset.js +7 -14
  13. package/dist/data-structures/graph/abstract-graph.d.ts +11 -9
  14. package/dist/data-structures/graph/abstract-graph.js +39 -22
  15. package/dist/data-structures/graph/directed-graph.d.ts +17 -15
  16. package/dist/data-structures/graph/directed-graph.js +47 -42
  17. package/dist/data-structures/graph/undirected-graph.d.ts +7 -7
  18. package/dist/data-structures/graph/undirected-graph.js +48 -48
  19. package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +2 -3
  20. package/dist/data-structures/interfaces/abstract-graph.d.ts +2 -4
  21. package/dist/data-structures/interfaces/avl-tree.d.ts +1 -1
  22. package/dist/data-structures/interfaces/bst.d.ts +2 -2
  23. package/dist/data-structures/interfaces/rb-tree.d.ts +2 -2
  24. package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -1
  25. package/dist/data-structures/tree/tree.d.ts +1 -4
  26. package/dist/data-structures/tree/tree.js +1 -12
  27. package/dist/data-structures/types/abstract-binary-tree.d.ts +9 -5
  28. package/dist/data-structures/types/abstract-binary-tree.js +9 -5
  29. package/dist/data-structures/types/avl-tree.d.ts +1 -1
  30. package/dist/data-structures/types/binary-tree.d.ts +1 -1
  31. package/dist/data-structures/types/bst.d.ts +1 -1
  32. package/dist/data-structures/types/bst.js +1 -1
  33. package/dist/data-structures/types/directed-graph.js +1 -1
  34. package/dist/data-structures/types/helpers.d.ts +1 -8
  35. package/dist/data-structures/types/rb-tree.d.ts +2 -0
  36. package/dist/data-structures/types/rb-tree.js +1 -1
  37. package/dist/data-structures/types/tree-multiset.d.ts +4 -4
  38. package/dist/utils/types/utils.d.ts +18 -0
  39. package/dist/utils/utils.d.ts +6 -1
  40. package/dist/utils/utils.js +21 -1
  41. package/package.json +2 -2
  42. package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
  43. package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
  44. package/backup/recursive-type/src/assets/logo.png +0 -0
  45. package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
  46. package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +0 -3
  47. package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +0 -288
  48. package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +0 -3
  49. package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
  50. package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +0 -1502
  51. package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +0 -503
  52. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  53. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  54. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  55. package/backup/recursive-type/src/data-structures/binary-tree/index.ts +0 -11
  56. package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +0 -110
  57. package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +0 -243
  58. package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +0 -3
  59. package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +0 -55
  60. package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  61. package/backup/recursive-type/src/data-structures/diagrams/README.md +0 -5
  62. package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +0 -985
  63. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  64. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  65. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  66. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  67. package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  68. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  69. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  70. package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  71. package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
  72. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  73. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  74. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  75. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  76. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  77. package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +0 -478
  78. package/backup/recursive-type/src/data-structures/graph/index.ts +0 -3
  79. package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +0 -293
  80. package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +0 -67
  81. package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +0 -56
  82. package/backup/recursive-type/src/data-structures/hash/hash-table.ts +0 -3
  83. package/backup/recursive-type/src/data-structures/hash/index.ts +0 -6
  84. package/backup/recursive-type/src/data-structures/hash/pair.ts +0 -3
  85. package/backup/recursive-type/src/data-structures/hash/tree-map.ts +0 -3
  86. package/backup/recursive-type/src/data-structures/hash/tree-set.ts +0 -3
  87. package/backup/recursive-type/src/data-structures/heap/heap.ts +0 -176
  88. package/backup/recursive-type/src/data-structures/heap/index.ts +0 -3
  89. package/backup/recursive-type/src/data-structures/heap/max-heap.ts +0 -31
  90. package/backup/recursive-type/src/data-structures/heap/min-heap.ts +0 -34
  91. package/backup/recursive-type/src/data-structures/index.ts +0 -15
  92. package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +0 -42
  93. package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +0 -1
  94. package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +0 -56
  95. package/backup/recursive-type/src/data-structures/interfaces/bst.ts +0 -1
  96. package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +0 -15
  97. package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
  98. package/backup/recursive-type/src/data-structures/interfaces/heap.ts +0 -1
  99. package/backup/recursive-type/src/data-structures/interfaces/index.ts +0 -13
  100. package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +0 -1
  101. package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +0 -1
  102. package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +0 -1
  103. package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +0 -1
  104. package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +0 -1
  105. package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +0 -3
  106. package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
  107. package/backup/recursive-type/src/data-structures/linked-list/index.ts +0 -3
  108. package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +0 -490
  109. package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +0 -3
  110. package/backup/recursive-type/src/data-structures/matrix/index.ts +0 -4
  111. package/backup/recursive-type/src/data-structures/matrix/matrix.ts +0 -27
  112. package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +0 -208
  113. package/backup/recursive-type/src/data-structures/matrix/navigator.ts +0 -122
  114. package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +0 -316
  115. package/backup/recursive-type/src/data-structures/priority-queue/index.ts +0 -3
  116. package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
  117. package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
  118. package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +0 -354
  119. package/backup/recursive-type/src/data-structures/queue/deque.ts +0 -251
  120. package/backup/recursive-type/src/data-structures/queue/index.ts +0 -2
  121. package/backup/recursive-type/src/data-structures/queue/queue.ts +0 -120
  122. package/backup/recursive-type/src/data-structures/stack/index.ts +0 -1
  123. package/backup/recursive-type/src/data-structures/stack/stack.ts +0 -98
  124. package/backup/recursive-type/src/data-structures/tree/index.ts +0 -1
  125. package/backup/recursive-type/src/data-structures/tree/tree.ts +0 -80
  126. package/backup/recursive-type/src/data-structures/trie/index.ts +0 -1
  127. package/backup/recursive-type/src/data-structures/trie/trie.ts +0 -227
  128. package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +0 -5
  129. package/backup/recursive-type/src/data-structures/types/avl-tree.ts +0 -8
  130. package/backup/recursive-type/src/data-structures/types/binary-tree.ts +0 -10
  131. package/backup/recursive-type/src/data-structures/types/bst.ts +0 -6
  132. package/backup/recursive-type/src/data-structures/types/directed-graph.ts +0 -8
  133. package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +0 -1
  134. package/backup/recursive-type/src/data-structures/types/heap.ts +0 -5
  135. package/backup/recursive-type/src/data-structures/types/index.ts +0 -12
  136. package/backup/recursive-type/src/data-structures/types/navigator.ts +0 -13
  137. package/backup/recursive-type/src/data-structures/types/priority-queue.ts +0 -9
  138. package/backup/recursive-type/src/data-structures/types/segment-tree.ts +0 -1
  139. package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +0 -1
  140. package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +0 -1
  141. package/backup/recursive-type/src/index.ts +0 -1
  142. package/backup/recursive-type/src/utils/index.ts +0 -2
  143. package/backup/recursive-type/src/utils/types/index.ts +0 -1
  144. package/backup/recursive-type/src/utils/types/utils.ts +0 -6
  145. package/backup/recursive-type/src/utils/utils.ts +0 -78
  146. package/docs/.nojekyll +0 -1
  147. package/docs/assets/highlight.css +0 -92
  148. package/docs/assets/main.js +0 -58
  149. package/docs/assets/search.js +0 -1
  150. package/docs/assets/style.css +0 -1367
  151. package/docs/classes/AVLTree.html +0 -2451
  152. package/docs/classes/AVLTreeNode.html +0 -499
  153. package/docs/classes/AaTree.html +0 -172
  154. package/docs/classes/AbstractBinaryTree.html +0 -2118
  155. package/docs/classes/AbstractBinaryTreeNode.html +0 -524
  156. package/docs/classes/AbstractEdge.html +0 -295
  157. package/docs/classes/AbstractGraph.html +0 -1043
  158. package/docs/classes/AbstractVertex.html +0 -258
  159. package/docs/classes/ArrayDeque.html +0 -439
  160. package/docs/classes/BST.html +0 -2297
  161. package/docs/classes/BSTNode.html +0 -503
  162. package/docs/classes/BTree.html +0 -172
  163. package/docs/classes/BinaryIndexedTree.html +0 -341
  164. package/docs/classes/BinaryTree.html +0 -2133
  165. package/docs/classes/BinaryTreeNode.html +0 -501
  166. package/docs/classes/Character.html +0 -220
  167. package/docs/classes/CoordinateMap.html +0 -483
  168. package/docs/classes/CoordinateSet.html +0 -444
  169. package/docs/classes/Deque.html +0 -975
  170. package/docs/classes/DirectedEdge.html +0 -366
  171. package/docs/classes/DirectedGraph.html +0 -1443
  172. package/docs/classes/DirectedVertex.html +0 -254
  173. package/docs/classes/DoublyLinkedList.html +0 -968
  174. package/docs/classes/DoublyLinkedListNode.html +0 -297
  175. package/docs/classes/HashTable.html +0 -172
  176. package/docs/classes/Heap.html +0 -423
  177. package/docs/classes/HeapItem.html +0 -255
  178. package/docs/classes/Matrix2D.html +0 -502
  179. package/docs/classes/MatrixNTI2D.html +0 -240
  180. package/docs/classes/MaxHeap.html +0 -436
  181. package/docs/classes/MaxPriorityQueue.html +0 -836
  182. package/docs/classes/MinHeap.html +0 -437
  183. package/docs/classes/MinPriorityQueue.html +0 -838
  184. package/docs/classes/Navigator.html +0 -313
  185. package/docs/classes/ObjectDeque.html +0 -455
  186. package/docs/classes/Pair.html +0 -172
  187. package/docs/classes/PriorityQueue.html +0 -760
  188. package/docs/classes/Queue.html +0 -392
  189. package/docs/classes/RBTree.html +0 -2388
  190. package/docs/classes/RBTreeNode.html +0 -516
  191. package/docs/classes/SegmentTree.html +0 -434
  192. package/docs/classes/SegmentTreeNode.html +0 -357
  193. package/docs/classes/SinglyLinkedList.html +0 -788
  194. package/docs/classes/SinglyLinkedListNode.html +0 -270
  195. package/docs/classes/SkipLinkedList.html +0 -172
  196. package/docs/classes/SplayTree.html +0 -172
  197. package/docs/classes/Stack.html +0 -368
  198. package/docs/classes/TreeMap.html +0 -172
  199. package/docs/classes/TreeMultiSet.html +0 -2297
  200. package/docs/classes/TreeMultiSetNode.html +0 -499
  201. package/docs/classes/TreeNode.html +0 -343
  202. package/docs/classes/TreeSet.html +0 -172
  203. package/docs/classes/Trie.html +0 -372
  204. package/docs/classes/TrieNode.html +0 -280
  205. package/docs/classes/TwoThreeTree.html +0 -172
  206. package/docs/classes/UndirectedEdge.html +0 -337
  207. package/docs/classes/UndirectedGraph.html +0 -1210
  208. package/docs/classes/UndirectedVertex.html +0 -254
  209. package/docs/classes/Vector2D.html +0 -805
  210. package/docs/enums/CP.html +0 -181
  211. package/docs/enums/FamilyPosition.html +0 -181
  212. package/docs/enums/LoopType.html +0 -182
  213. package/docs/enums/RBColor.html +0 -174
  214. package/docs/enums/TopologicalProperty.html +0 -181
  215. package/docs/index.html +0 -645
  216. package/docs/interfaces/IAVLTree.html +0 -1378
  217. package/docs/interfaces/IAVLTreeNode.html +0 -405
  218. package/docs/interfaces/IAbstractBinaryTree.html +0 -1124
  219. package/docs/interfaces/IAbstractBinaryTreeNode.html +0 -384
  220. package/docs/interfaces/IAbstractGraph.html +0 -433
  221. package/docs/interfaces/IBST.html +0 -1271
  222. package/docs/interfaces/IBSTNode.html +0 -408
  223. package/docs/interfaces/IDirectedGraph.html +0 -572
  224. package/docs/interfaces/IUNDirectedGraph.html +0 -465
  225. package/docs/modules.html +0 -262
  226. package/docs/types/AVLTreeOptions.html +0 -150
  227. package/docs/types/AbstractBinaryTreeNodeProperties.html +0 -152
  228. package/docs/types/AbstractBinaryTreeNodeProperty.html +0 -152
  229. package/docs/types/AbstractBinaryTreeOptions.html +0 -156
  230. package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -152
  231. package/docs/types/BSTComparator.html +0 -162
  232. package/docs/types/BSTOptions.html +0 -152
  233. package/docs/types/BinaryTreeDeletedResult.html +0 -159
  234. package/docs/types/BinaryTreeNodeId.html +0 -147
  235. package/docs/types/BinaryTreeNodePropertyName.html +0 -147
  236. package/docs/types/BinaryTreeOptions.html +0 -150
  237. package/docs/types/DFSOrderPattern.html +0 -147
  238. package/docs/types/DijkstraResult.html +0 -167
  239. package/docs/types/Direction.html +0 -147
  240. package/docs/types/EdgeId.html +0 -147
  241. package/docs/types/HeapOptions.html +0 -168
  242. package/docs/types/IdObject.html +0 -157
  243. package/docs/types/KeyValObject.html +0 -152
  244. package/docs/types/NavigatorParams.html +0 -181
  245. package/docs/types/NodeOrPropertyName.html +0 -147
  246. package/docs/types/PriorityQueueComparator.html +0 -167
  247. package/docs/types/PriorityQueueDFSOrderPattern.html +0 -147
  248. package/docs/types/PriorityQueueOptions.html +0 -161
  249. package/docs/types/RBTreeOptions.html +0 -150
  250. package/docs/types/RecursiveAVLTreeNode.html +0 -152
  251. package/docs/types/RecursiveBSTNode.html +0 -152
  252. package/docs/types/RecursiveBinaryTreeNode.html +0 -152
  253. package/docs/types/RecursiveTreeMultiSetNode.html +0 -152
  254. package/docs/types/SegmentTreeNodeVal.html +0 -147
  255. package/docs/types/TopologicalStatus.html +0 -147
  256. package/docs/types/TreeMultiSetOptions.html +0 -152
  257. package/docs/types/Turning.html +0 -147
  258. package/docs/types/VertexId.html +0 -147
  259. package/notes/bst.test.ts +0 -181
  260. package/notes/note.md +0 -34
@@ -1,503 +0,0 @@
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
- }
@@ -1,11 +0,0 @@
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';
@@ -1,110 +0,0 @@
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
- }