data-structure-typed 1.15.1 → 1.16.1

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