data-structure-typed 1.19.2 → 1.19.4

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 (155) hide show
  1. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
  3. package/dist/data-structures/binary-tree/avl-tree.js +46 -90
  4. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
  6. package/dist/data-structures/binary-tree/binary-tree.js +9 -31
  7. package/dist/data-structures/binary-tree/bst.js +96 -139
  8. package/dist/data-structures/binary-tree/rb-tree.js +32 -56
  9. package/dist/data-structures/binary-tree/segment-tree.js +78 -120
  10. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
  12. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  13. package/dist/data-structures/graph/abstract-graph.js +340 -574
  14. package/dist/data-structures/graph/directed-graph.js +146 -276
  15. package/dist/data-structures/graph/undirected-graph.js +87 -176
  16. package/dist/data-structures/hash/coordinate-map.js +23 -45
  17. package/dist/data-structures/hash/coordinate-set.js +20 -42
  18. package/dist/data-structures/hash/hash-table.js +2 -5
  19. package/dist/data-structures/hash/pair.js +2 -5
  20. package/dist/data-structures/hash/tree-map.js +2 -5
  21. package/dist/data-structures/hash/tree-set.js +2 -5
  22. package/dist/data-structures/heap/heap.js +53 -77
  23. package/dist/data-structures/heap/max-heap.js +8 -26
  24. package/dist/data-structures/heap/min-heap.js +8 -26
  25. package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
  26. package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
  27. package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
  28. package/dist/data-structures/matrix/matrix.js +7 -8
  29. package/dist/data-structures/matrix/matrix2d.js +76 -93
  30. package/dist/data-structures/matrix/navigator.js +18 -37
  31. package/dist/data-structures/matrix/vector2d.js +80 -101
  32. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
  33. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
  34. package/dist/data-structures/priority-queue/priority-queue.js +93 -139
  35. package/dist/data-structures/queue/deque.js +82 -128
  36. package/dist/data-structures/queue/queue.js +24 -25
  37. package/dist/data-structures/stack/stack.js +21 -22
  38. package/dist/data-structures/tree/tree.js +32 -45
  39. package/dist/data-structures/trie/trie.js +93 -200
  40. package/dist/utils/utils.js +22 -107
  41. package/dist/utils/validate-type.js +2 -2
  42. package/package.json +1 -1
  43. package/src/assets/complexities-diff.jpg +0 -0
  44. package/src/assets/data-structure-complexities.jpg +0 -0
  45. package/src/assets/logo.png +0 -0
  46. package/src/assets/overview-diagram-of-data-structures.png +0 -0
  47. package/src/data-structures/binary-tree/aa-tree.ts +3 -0
  48. package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
  49. package/src/data-structures/binary-tree/avl-tree.ts +297 -0
  50. package/src/data-structures/binary-tree/b-tree.ts +3 -0
  51. package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
  52. package/src/data-structures/binary-tree/binary-tree.ts +40 -0
  53. package/src/data-structures/binary-tree/bst.ts +435 -0
  54. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  55. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  56. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  57. package/src/data-structures/binary-tree/index.ts +12 -0
  58. package/src/data-structures/binary-tree/rb-tree.ts +102 -0
  59. package/src/data-structures/binary-tree/segment-tree.ts +243 -0
  60. package/src/data-structures/binary-tree/splay-tree.ts +3 -0
  61. package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
  62. package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  63. package/src/data-structures/diagrams/README.md +5 -0
  64. package/src/data-structures/graph/abstract-graph.ts +1032 -0
  65. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  66. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  67. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  68. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  69. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  70. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  71. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  72. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  73. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  74. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  75. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  76. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  77. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  78. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  79. package/src/data-structures/graph/directed-graph.ts +472 -0
  80. package/src/data-structures/graph/index.ts +3 -0
  81. package/src/data-structures/graph/undirected-graph.ts +270 -0
  82. package/src/data-structures/hash/coordinate-map.ts +67 -0
  83. package/src/data-structures/hash/coordinate-set.ts +56 -0
  84. package/src/data-structures/hash/hash-table.ts +3 -0
  85. package/src/data-structures/hash/index.ts +6 -0
  86. package/src/data-structures/hash/pair.ts +3 -0
  87. package/src/data-structures/hash/tree-map.ts +3 -0
  88. package/src/data-structures/hash/tree-set.ts +3 -0
  89. package/src/data-structures/heap/heap.ts +183 -0
  90. package/src/data-structures/heap/index.ts +3 -0
  91. package/src/data-structures/heap/max-heap.ts +31 -0
  92. package/src/data-structures/heap/min-heap.ts +34 -0
  93. package/src/data-structures/index.ts +15 -0
  94. package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
  95. package/src/data-structures/interfaces/abstract-graph.ts +40 -0
  96. package/src/data-structures/interfaces/avl-tree.ts +28 -0
  97. package/src/data-structures/interfaces/binary-tree.ts +8 -0
  98. package/src/data-structures/interfaces/bst.ts +32 -0
  99. package/src/data-structures/interfaces/directed-graph.ts +20 -0
  100. package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
  101. package/src/data-structures/interfaces/heap.ts +1 -0
  102. package/src/data-structures/interfaces/index.ts +15 -0
  103. package/src/data-structures/interfaces/navigator.ts +1 -0
  104. package/src/data-structures/interfaces/priority-queue.ts +1 -0
  105. package/src/data-structures/interfaces/rb-tree.ts +11 -0
  106. package/src/data-structures/interfaces/segment-tree.ts +1 -0
  107. package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
  108. package/src/data-structures/interfaces/tree-multiset.ts +12 -0
  109. package/src/data-structures/interfaces/undirected-graph.ts +6 -0
  110. package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
  111. package/src/data-structures/linked-list/index.ts +3 -0
  112. package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
  113. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  114. package/src/data-structures/matrix/index.ts +4 -0
  115. package/src/data-structures/matrix/matrix.ts +27 -0
  116. package/src/data-structures/matrix/matrix2d.ts +208 -0
  117. package/src/data-structures/matrix/navigator.ts +122 -0
  118. package/src/data-structures/matrix/vector2d.ts +316 -0
  119. package/src/data-structures/priority-queue/index.ts +3 -0
  120. package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
  121. package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
  122. package/src/data-structures/priority-queue/priority-queue.ts +354 -0
  123. package/src/data-structures/queue/deque.ts +251 -0
  124. package/src/data-structures/queue/index.ts +2 -0
  125. package/src/data-structures/queue/queue.ts +120 -0
  126. package/src/data-structures/stack/index.ts +1 -0
  127. package/src/data-structures/stack/stack.ts +98 -0
  128. package/src/data-structures/tree/index.ts +1 -0
  129. package/src/data-structures/tree/tree.ts +69 -0
  130. package/src/data-structures/trie/index.ts +1 -0
  131. package/src/data-structures/trie/trie.ts +227 -0
  132. package/src/data-structures/types/abstract-binary-tree.ts +42 -0
  133. package/src/data-structures/types/abstract-graph.ts +5 -0
  134. package/src/data-structures/types/avl-tree.ts +5 -0
  135. package/src/data-structures/types/binary-tree.ts +9 -0
  136. package/src/data-structures/types/bst.ts +12 -0
  137. package/src/data-structures/types/directed-graph.ts +8 -0
  138. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  139. package/src/data-structures/types/heap.ts +5 -0
  140. package/src/data-structures/types/helpers.ts +1 -0
  141. package/src/data-structures/types/index.ts +15 -0
  142. package/src/data-structures/types/navigator.ts +13 -0
  143. package/src/data-structures/types/priority-queue.ts +9 -0
  144. package/src/data-structures/types/rb-tree.ts +8 -0
  145. package/src/data-structures/types/segment-tree.ts +1 -0
  146. package/src/data-structures/types/singly-linked-list.ts +1 -0
  147. package/src/data-structures/types/tree-multiset.ts +8 -0
  148. package/src/index.ts +2 -0
  149. package/src/utils/index.ts +3 -0
  150. package/src/utils/types/index.ts +2 -0
  151. package/src/utils/types/utils.ts +6 -0
  152. package/src/utils/types/validate-type.ts +25 -0
  153. package/src/utils/utils.ts +78 -0
  154. package/src/utils/validate-type.ts +69 -0
  155. package/tsconfig.json +1 -1
@@ -0,0 +1,435 @@
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, BSTNodeNested, BSTOptions} from '../types';
9
+ import {CP, LoopType} from '../types';
10
+ import {BinaryTree, BinaryTreeNode} from './binary-tree';
11
+ import {IBST, IBSTNode} from '../interfaces';
12
+
13
+ export class BSTNode<T = any, NEIGHBOR extends BSTNode<T, NEIGHBOR> = BSTNodeNested<T>> extends BinaryTreeNode<T, NEIGHBOR> implements IBSTNode<T, NEIGHBOR> {
14
+
15
+ }
16
+
17
+ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBST<N> {
18
+ /**
19
+ * The constructor function initializes a binary search tree object with an optional comparator function.
20
+ * @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
21
+ */
22
+ constructor(options?: BSTOptions) {
23
+ super(options);
24
+ if (options !== undefined) {
25
+ const {comparator} = options;
26
+ if (comparator !== undefined) {
27
+ this._comparator = comparator;
28
+ }
29
+ }
30
+ }
31
+
32
+ /**
33
+ * The function creates a new binary search tree node with the given id and value.
34
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
35
+ * identify each node in the binary tree.
36
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
37
+ * that will be stored in the node.
38
+ * @returns a new instance of the BSTNode class with the specified id and value.
39
+ */
40
+ override createNode(id: BinaryTreeNodeId, val?: N['val']): N {
41
+ return new BSTNode<N['val'], N>(id, val) as N;
42
+ }
43
+
44
+ /**
45
+ * The `add` function adds a new node to a binary tree, ensuring that duplicates are not accepted.
46
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add. It
47
+ * is of type `BinaryTreeNodeId`.
48
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It represents
49
+ * the value associated with the node.
50
+ * @returns The function `add` returns the inserted node (`inserted`) if it was successfully added to the binary tree.
51
+ * If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`.
52
+ */
53
+ override add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined {
54
+ let inserted: N | null = null;
55
+ const newNode = this.createNode(id, val);
56
+ if (this.root === null) {
57
+ this._setRoot(newNode);
58
+ this._setSize(this.size + 1);
59
+ inserted = (this.root);
60
+ } else {
61
+ let cur = this.root;
62
+ let traversing = true;
63
+ while (traversing) {
64
+ if (cur !== null && newNode !== null) {
65
+ if (this._compare(cur.id, id) === CP.eq) {
66
+ if (newNode) {
67
+ cur.val = newNode.val;
68
+ }
69
+ //Duplicates are not accepted.
70
+ traversing = false;
71
+ inserted = cur;
72
+ } else if (this._compare(cur.id, id) === CP.gt) {
73
+ // Traverse left of the node
74
+ if (cur.left === undefined) {
75
+ if (newNode) {
76
+ newNode.parent = cur;
77
+ }
78
+ //Add to the left of the current node
79
+ cur.left = newNode;
80
+ this._setSize(this.size + 1);
81
+ traversing = false;
82
+ inserted = cur.left;
83
+ } else {
84
+ //Traverse the left of the current node
85
+ if (cur.left) cur = cur.left;
86
+ }
87
+ } else if (this._compare(cur.id, id) === CP.lt) {
88
+ // Traverse right of the node
89
+ if (cur.right === undefined) {
90
+ if (newNode) {
91
+ newNode.parent = cur;
92
+ }
93
+ //Add to the right of the current node
94
+ cur.right = newNode;
95
+ this._setSize(this.size + 1);
96
+ traversing = false;
97
+ inserted = (cur.right);
98
+ } else {
99
+ //Traverse the left of the current node
100
+ if (cur.right) cur = cur.right;
101
+ }
102
+ }
103
+ } else {
104
+ traversing = false;
105
+ }
106
+ }
107
+ }
108
+ return inserted;
109
+ }
110
+
111
+ /**
112
+ * The function returns the first node in a binary tree that matches the given property name and value.
113
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
114
+ * generic type `N`. It represents the property of the binary tree node that you want to search for.
115
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
116
+ * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
117
+ * @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
118
+ */
119
+ override get(nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName): N | null {
120
+ propertyName = propertyName ?? 'id';
121
+ return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
122
+ }
123
+
124
+ /**
125
+ * The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
126
+ * leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
127
+ * @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
128
+ * the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
129
+ * equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
130
+ */
131
+ lastKey(): BinaryTreeNodeId {
132
+ if (this._compare(0, 1) === CP.lt) return this.getRightMost()?.id ?? 0;
133
+ else if (this._compare(0, 1) === CP.gt) return this.getLeftMost()?.id ?? 0;
134
+ else return this.getRightMost()?.id ?? 0;
135
+ }
136
+
137
+ /**
138
+ * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
139
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
140
+ * `N` type. It represents the property of the binary tree node that you want to compare with.
141
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
142
+ * specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
143
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
144
+ * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
145
+ * is set to `true`, the function will return an array with only one node (if
146
+ * @returns an array of nodes (type N).
147
+ */
148
+ override getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean): N[] {
149
+ propertyName = propertyName ?? 'id';
150
+ if (!this.root) return [];
151
+ const result: N[] = [];
152
+
153
+ if (this.loopType === LoopType.RECURSIVE) {
154
+ const _traverse = (cur: N) => {
155
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return;
156
+
157
+ if (!cur.left && !cur.right) return;
158
+ if (propertyName === 'id') {
159
+ if (this._compare(cur.id, nodeProperty as number) === CP.gt) cur.left && _traverse(cur.left);
160
+ if (this._compare(cur.id, nodeProperty as number) === CP.lt) cur.right && _traverse(cur.right);
161
+ } else {
162
+ cur.left && _traverse(cur.left);
163
+ cur.right && _traverse(cur.right);
164
+ }
165
+ }
166
+
167
+ _traverse(this.root);
168
+ } else {
169
+ const queue: N[] = [this.root];
170
+ while (queue.length > 0) {
171
+ const cur = queue.shift();
172
+ if (cur) {
173
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result;
174
+ if (propertyName === 'id') {
175
+ if (this._compare(cur.id, nodeProperty as number) === CP.gt) cur.left && queue.push(cur.left);
176
+ if (this._compare(cur.id, nodeProperty as number) === CP.lt) cur.right && queue.push(cur.right);
177
+ } else {
178
+ cur.left && queue.push(cur.left);
179
+ cur.right && queue.push(cur.right);
180
+ }
181
+ }
182
+ }
183
+ }
184
+
185
+ return result;
186
+ }
187
+
188
+ // --- start additional functions
189
+ /**
190
+ * The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
191
+ * less than a given node.
192
+ * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
193
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
194
+ * specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
195
+ * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
196
+ * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
197
+ */
198
+ lesserSum(beginNode: N | BinaryTreeNodeId | null, propertyName ?: BinaryTreeNodePropertyName): number {
199
+ propertyName = propertyName ?? 'id';
200
+ if (typeof beginNode === 'number') beginNode = this.get(beginNode, 'id');
201
+ if (!beginNode) return 0;
202
+ if (!this.root) return 0;
203
+ const id = beginNode.id;
204
+ const getSumByPropertyName = (cur: N) => {
205
+ let needSum: number;
206
+ switch (propertyName) {
207
+ case 'id':
208
+ needSum = cur.id;
209
+ break;
210
+ default:
211
+ needSum = cur.id;
212
+ break;
213
+ }
214
+ return needSum;
215
+ }
216
+
217
+ let sum = 0;
218
+
219
+ if (this.loopType === LoopType.RECURSIVE) {
220
+ const _traverse = (cur: N): void => {
221
+ const compared = this._compare(cur.id, id);
222
+ if (compared === CP.eq) {
223
+ if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
224
+ return;
225
+ } else if (compared === CP.lt) {
226
+ if (cur.left) sum += this.subTreeSum(cur.left, propertyName);
227
+ sum += getSumByPropertyName(cur);
228
+ if (cur.right) _traverse(cur.right);
229
+ else return;
230
+ } else {
231
+ if (cur.left) _traverse(cur.left);
232
+ else return;
233
+ }
234
+ };
235
+
236
+ _traverse(this.root);
237
+ } else {
238
+ const queue: N[] = [this.root];
239
+ while (queue.length > 0) {
240
+ const cur = queue.shift();
241
+ if (cur) {
242
+ const compared = this._compare(cur.id, id);
243
+ if (compared === CP.eq) {
244
+ if (cur.right) sum += this.subTreeSum(cur.right, propertyName);
245
+ return sum;
246
+ } else if (compared === CP.lt) { // todo maybe a bug
247
+ if (cur.left) sum += this.subTreeSum(cur.left, propertyName);
248
+ sum += getSumByPropertyName(cur);
249
+ if (cur.right) queue.push(cur.right);
250
+ else return sum;
251
+ } else {
252
+ if (cur.left) queue.push(cur.left);
253
+ else return sum;
254
+ }
255
+ }
256
+ }
257
+ }
258
+
259
+ return sum;
260
+ }
261
+
262
+ /**
263
+ * The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
264
+ * have a greater value than a given node.
265
+ * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
266
+ * `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
267
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
268
+ * each greater node should be increased.
269
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
270
+ * specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
271
+ * 'id'.
272
+ * @returns a boolean value.
273
+ */
274
+ allGreaterNodesAdd(node: N | BinaryTreeNodeId | null, delta: number, propertyName ?: BinaryTreeNodePropertyName): boolean {
275
+ propertyName = propertyName ?? 'id';
276
+ if (typeof node === 'number') node = this.get(node, 'id');
277
+ if (!node) return false;
278
+ const id = node.id;
279
+ if (!this.root) return false;
280
+
281
+ const _sumByPropertyName = (cur: N) => {
282
+ switch (propertyName) {
283
+ case 'id':
284
+ cur.id += delta;
285
+ break;
286
+ default:
287
+ cur.id += delta;
288
+ break;
289
+ }
290
+ }
291
+ if (this.loopType === LoopType.RECURSIVE) {
292
+ const _traverse = (cur: N) => {
293
+ const compared = this._compare(cur.id, id);
294
+ if (compared === CP.gt) _sumByPropertyName(cur);
295
+
296
+ if (!cur.left && !cur.right) return;
297
+ if (cur.left && this._compare(cur.left.id, id) === CP.gt) _traverse(cur.left);
298
+ if (cur.right && this._compare(cur.right.id, id) === CP.gt) _traverse(cur.right);
299
+ };
300
+
301
+ _traverse(this.root);
302
+ return true;
303
+ } else {
304
+ const queue: N[] = [this.root];
305
+ while (queue.length > 0) {
306
+ const cur = queue.shift();
307
+ if (cur) {
308
+ const compared = this._compare(cur.id, id);
309
+ if (compared === CP.gt) _sumByPropertyName(cur);
310
+
311
+ if (cur.left && this._compare(cur.left.id, id) === CP.gt) queue.push(cur.left);
312
+ if (cur.right && this._compare(cur.right.id, id) === CP.gt) queue.push(cur.right);
313
+ }
314
+ }
315
+ return true;
316
+ }
317
+ }
318
+
319
+ /**
320
+ * Balancing Adjustment:
321
+ * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
322
+ * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
323
+ *
324
+ * Use Cases and Efficiency:
325
+ * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
326
+ * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
327
+ */
328
+
329
+
330
+ /**
331
+ * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
332
+ * constructs a balanced binary search tree using either a recursive or iterative approach.
333
+ * @returns The function `perfectlyBalance()` returns a boolean value.
334
+ */
335
+ perfectlyBalance(): boolean {
336
+ const sorted = this.DFS('in', 'node'), n = sorted.length;
337
+ this.clear();
338
+
339
+ if (sorted.length < 1) return false;
340
+ if (this.loopType === LoopType.RECURSIVE) {
341
+ const buildBalanceBST = (l: number, r: number) => {
342
+ if (l > r) return;
343
+ const m = l + Math.floor((r - l) / 2);
344
+ const midNode = sorted[m];
345
+ this.add(midNode.id, midNode.val);
346
+ buildBalanceBST(l, m - 1);
347
+ buildBalanceBST(m + 1, r);
348
+ };
349
+
350
+ buildBalanceBST(0, n - 1);
351
+ return true;
352
+ } else {
353
+ const stack: [[number, number]] = [[0, n - 1]];
354
+ while (stack.length > 0) {
355
+ const popped = stack.pop();
356
+ if (popped) {
357
+ const [l, r] = popped;
358
+ if (l <= r) {
359
+ const m = l + Math.floor((r - l) / 2);
360
+ const midNode = sorted[m];
361
+ this.add(midNode.id, midNode.val);
362
+ stack.push([m + 1, r]);
363
+ stack.push([l, m - 1]);
364
+ }
365
+ }
366
+ }
367
+ return true;
368
+ }
369
+ }
370
+
371
+ /**
372
+ * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
373
+ * @returns a boolean value.
374
+ */
375
+ isAVLBalanced(): boolean {
376
+ if (!this.root) return true;
377
+
378
+ let balanced = true;
379
+
380
+ if (this.loopType === LoopType.RECURSIVE) {
381
+ const _height = (cur: N | null | undefined): number => {
382
+ if (!cur) return 0;
383
+ const leftHeight = _height(cur.left), rightHeight = _height(cur.right);
384
+ if (Math.abs(leftHeight - rightHeight) > 1) balanced = false;
385
+ return Math.max(leftHeight, rightHeight) + 1;
386
+ };
387
+ _height(this.root);
388
+ } else {
389
+ const stack: N[] = [];
390
+ let node: N | null | undefined = this.root, last: N | null = null;
391
+ const depths: Map<N, number> = new Map();
392
+
393
+ while (stack.length > 0 || node) {
394
+ if (node) {
395
+ stack.push(node);
396
+ node = node.left;
397
+ } else {
398
+ node = stack[stack.length - 1]
399
+ if (!node.right || last === node.right) {
400
+ node = stack.pop();
401
+ if (node) {
402
+ const left = node.left ? depths.get(node.left) ?? -1 : -1;
403
+ const right = node.right ? depths.get(node.right) ?? -1 : -1;
404
+ if (Math.abs(left - right) > 1) return false;
405
+ depths.set(node, 1 + Math.max(left, right));
406
+ last = node;
407
+ node = null;
408
+ }
409
+ } else node = node.right
410
+ }
411
+ }
412
+ }
413
+
414
+ return balanced;
415
+ }
416
+
417
+ protected _comparator: BSTComparator = (a, b) => a - b;
418
+
419
+ /**
420
+ * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
421
+ * greater than, less than, or equal to the second ID.
422
+ * @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
423
+ * @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
424
+ * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
425
+ * than), or CP.eq (equal).
426
+ */
427
+ protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP {
428
+ const compared = this._comparator(a, b);
429
+ if (compared > 0) return CP.gt;
430
+ else if (compared < 0) return CP.lt;
431
+ else return CP.eq;
432
+ }
433
+
434
+ // --- end additional functions
435
+ }
@@ -0,0 +1,12 @@
1
+ export * from './abstract-binary-tree';
2
+ export * from './binary-tree';
3
+ export * from './bst';
4
+ export * from './binary-indexed-tree';
5
+ export * from './segment-tree';
6
+ export * from './avl-tree';
7
+ export * from './b-tree';
8
+ export * from './rb-tree';
9
+ export * from './splay-tree';
10
+ export * from './aa-tree';
11
+ export * from './tree-multiset';
12
+ export * from './two-three-tree';
@@ -0,0 +1,102 @@
1
+ import {BinaryTreeNodeId, RBColor, RBTreeNodeNested, RBTreeOptions} from '../types';
2
+ import {IRBTree, IRBTreeNode} from '../interfaces/rb-tree';
3
+ import {BST, BSTNode} from './bst';
4
+
5
+
6
+ export class RBTreeNode<T = any, NEIGHBOR extends RBTreeNode<T, NEIGHBOR> = RBTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IRBTreeNode<T, NEIGHBOR> {
7
+ constructor(id: BinaryTreeNodeId, color: RBColor, val?: T) {
8
+ super(id, val);
9
+ this._color = color;
10
+ }
11
+
12
+ private _color: RBColor = RBColor.RED;
13
+
14
+ get color(): RBColor {
15
+ return this._color;
16
+ }
17
+
18
+ set color(value: RBColor) {
19
+ this._color = value;
20
+ }
21
+
22
+
23
+ // private override _parent: RBNode<T> | null;
24
+ // override set parent(v: RBNode<T> | null) {
25
+ // this._parent = v;
26
+ // }
27
+ // override get parent(): RBNode<T> | null {
28
+ // return this._parent;
29
+ // }
30
+ // private override _left?: RBNode<T> | null;
31
+ //
32
+ // override get left(): RBNode<T> | null | undefined {
33
+ // return this._left;
34
+ // }
35
+ //
36
+ // override set left(v: RBNode<T> | null | undefined) {
37
+ // if (v) {
38
+ // v.parent = this;
39
+ // }
40
+ // this._left = v;
41
+ // }
42
+ //
43
+ // private override _right?: RBNode<T> | null;
44
+ //
45
+ // override get right(): RBNode<T> | null | undefined {
46
+ // return this._right;
47
+ // }
48
+ //
49
+ // override set right(v: RBNode<T> | null | undefined) {
50
+ // if (v) {
51
+ // v.parent = this;
52
+ // }
53
+ // this._right = v;
54
+ // }
55
+ }
56
+
57
+ export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
58
+ constructor(options?: RBTreeOptions) {
59
+ super(options);
60
+ }
61
+
62
+ override createNode(id: BinaryTreeNodeId, val?: N['val']): N {
63
+ return new RBTreeNode(id, RBColor.RED, val) as N;
64
+ }
65
+
66
+ // private override _root: BinaryTreeNode<N> | null = null;
67
+ //
68
+ // override get root(): BinaryTreeNode<N> | null {
69
+ // return this._root;
70
+ // }
71
+
72
+ insert(id: number, val?: N | null) {
73
+
74
+ }
75
+
76
+ private leftRotate(node: N) {
77
+
78
+ }
79
+
80
+ private rightRotate(node: N) {
81
+
82
+ }
83
+
84
+ private insertFixup(node: N) {
85
+
86
+ }
87
+
88
+ private deleteFixup(node: N) {
89
+
90
+ }
91
+
92
+ private transplant(u: N, v: N) {
93
+
94
+ }
95
+
96
+ // override remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeleted<N>[] {
97
+ //
98
+ // return [{deleted: new N(0, 0), needBalanced: null}];
99
+ // }
100
+
101
+
102
+ }