data-structure-typed 1.19.9 → 1.21.0

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