data-structure-typed 1.48.7 → 1.48.9
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.
- package/CHANGELOG.md +1 -1
- package/README.md +100 -106
- package/README_zh-CN.md +93 -100
- package/benchmark/report.html +1 -46
- package/benchmark/report.json +23 -458
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +0 -4
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +36 -34
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +0 -110
- package/dist/cjs/data-structures/queue/deque.js +1 -172
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +0 -4
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +36 -34
- package/dist/mjs/data-structures/queue/deque.d.ts +0 -110
- package/dist/mjs/data-structures/queue/deque.js +0 -170
- package/dist/umd/data-structure-typed.js +32 -195
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +43 -33
- package/src/data-structures/queue/deque.ts +1 -191
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +15 -8
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +5 -5
- package/test/unit/data-structures/queue/deque.test.ts +265 -367
- package/test/unit/data-structures/queue/queue.test.ts +158 -158
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.48.
|
|
3
|
+
"version": "1.48.9",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -98,10 +98,6 @@ export class BinaryTreeNode<K = any, V = any, N extends BinaryTreeNode<K, V, N>
|
|
|
98
98
|
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
99
99
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
100
100
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
101
|
-
* 6. Internal Nodes: Nodes with at least one child are internal.
|
|
102
|
-
* 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
|
|
103
|
-
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
104
|
-
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
105
101
|
*/
|
|
106
102
|
|
|
107
103
|
export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends IterableEntryBase<K, V | undefined>
|
|
@@ -231,7 +227,6 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
231
227
|
* Space Complexity O(1)
|
|
232
228
|
*/
|
|
233
229
|
|
|
234
|
-
|
|
235
230
|
/**
|
|
236
231
|
* Time Complexity O(log n) - O(n)
|
|
237
232
|
* Space Complexity O(1)
|
|
@@ -243,50 +238,65 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
243
238
|
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
244
239
|
*/
|
|
245
240
|
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
|
|
246
|
-
|
|
247
|
-
let inserted: N | null | undefined;
|
|
248
241
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
249
242
|
if (newNode === undefined) return;
|
|
250
243
|
|
|
251
|
-
//
|
|
252
|
-
if (
|
|
244
|
+
// If the tree is empty, directly set the new node as the root node
|
|
245
|
+
if (!this.root) {
|
|
246
|
+
this._root = newNode;
|
|
247
|
+
this._size = 1;
|
|
248
|
+
return newNode;
|
|
249
|
+
}
|
|
253
250
|
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
251
|
+
const queue = new Queue<N>([this.root]);
|
|
252
|
+
let potentialParent: N | undefined; // Record the parent node of the potential insertion location
|
|
253
|
+
|
|
254
|
+
while (queue.size > 0) {
|
|
255
|
+
const cur = queue.shift();
|
|
256
|
+
|
|
257
|
+
if (!cur) continue;
|
|
258
|
+
|
|
259
|
+
// Check for duplicate keys when newNode is not null
|
|
260
|
+
if (newNode !== null && cur.key === newNode.key) {
|
|
261
|
+
this._replaceNode(cur, newNode);
|
|
262
|
+
return newNode; // If duplicate keys are found, no insertion is performed
|
|
266
263
|
}
|
|
267
|
-
};
|
|
268
264
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
265
|
+
// Record the first possible insertion location found
|
|
266
|
+
if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
|
|
267
|
+
potentialParent = cur;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Continue traversing the left and right subtrees
|
|
271
|
+
if (cur.left !== null) {
|
|
272
|
+
cur.left && queue.push(cur.left);
|
|
273
|
+
}
|
|
274
|
+
if (cur.right !== null) {
|
|
275
|
+
cur.right && queue.push(cur.right);
|
|
277
276
|
}
|
|
278
|
-
inserted = this.root;
|
|
279
277
|
}
|
|
280
|
-
|
|
278
|
+
|
|
279
|
+
// At the end of the traversal, if the insertion position is found, insert
|
|
280
|
+
if (potentialParent) {
|
|
281
|
+
if (potentialParent.left === undefined) {
|
|
282
|
+
potentialParent.left = newNode;
|
|
283
|
+
} else if (potentialParent.right === undefined) {
|
|
284
|
+
potentialParent.right = newNode;
|
|
285
|
+
}
|
|
286
|
+
this._size++;
|
|
287
|
+
return newNode;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return undefined; // If the insertion position cannot be found, return undefined
|
|
281
291
|
}
|
|
282
292
|
|
|
293
|
+
|
|
283
294
|
/**
|
|
284
295
|
* Time Complexity: O(k log n) - O(k * n)
|
|
285
296
|
* Space Complexity: O(1)
|
|
286
297
|
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
287
298
|
*/
|
|
288
299
|
|
|
289
|
-
|
|
290
300
|
/**
|
|
291
301
|
* Time Complexity: O(k log n) - O(k * n)
|
|
292
302
|
* Space Complexity: O(1)
|
|
@@ -849,194 +849,4 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
849
849
|
|
|
850
850
|
return { bucketIndex, indexInBucket };
|
|
851
851
|
}
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
// O(1) time complexity of obtaining the element
|
|
855
|
-
// O(n) time complexity of adding at the beginning and the end
|
|
856
|
-
// todo tested slowest one
|
|
857
|
-
export class ObjectDeque<E = number> {
|
|
858
|
-
constructor(capacity?: number) {
|
|
859
|
-
if (capacity !== undefined) this._capacity = capacity;
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
protected _nodes: { [key: number]: E } = {};
|
|
863
|
-
|
|
864
|
-
get nodes(): { [p: number]: E } {
|
|
865
|
-
return this._nodes;
|
|
866
|
-
}
|
|
867
|
-
|
|
868
|
-
protected _capacity = Number.MAX_SAFE_INTEGER;
|
|
869
|
-
|
|
870
|
-
get capacity(): number {
|
|
871
|
-
return this._capacity;
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
protected _first = -1;
|
|
875
|
-
|
|
876
|
-
get first(): number {
|
|
877
|
-
return this._first;
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
protected _last = -1;
|
|
881
|
-
|
|
882
|
-
get last(): number {
|
|
883
|
-
return this._last;
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
protected _size = 0;
|
|
887
|
-
|
|
888
|
-
get size(): number {
|
|
889
|
-
return this._size;
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
/**
|
|
893
|
-
* Time Complexity: O(1)
|
|
894
|
-
* Space Complexity: O(1)
|
|
895
|
-
*/
|
|
896
|
-
|
|
897
|
-
/**
|
|
898
|
-
* Time Complexity: O(1)
|
|
899
|
-
* Space Complexity: O(1)
|
|
900
|
-
*
|
|
901
|
-
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
902
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
903
|
-
* structure.
|
|
904
|
-
*/
|
|
905
|
-
addFirst(element: E) {
|
|
906
|
-
if (this.size === 0) {
|
|
907
|
-
const mid = Math.floor(this.capacity / 2);
|
|
908
|
-
this._first = mid;
|
|
909
|
-
this._last = mid;
|
|
910
|
-
} else {
|
|
911
|
-
this._first--;
|
|
912
|
-
}
|
|
913
|
-
this.nodes[this.first] = element;
|
|
914
|
-
this._size++;
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
/**
|
|
918
|
-
* Time Complexity: O(1)
|
|
919
|
-
* Space Complexity: O(1)
|
|
920
|
-
*/
|
|
921
|
-
|
|
922
|
-
/**
|
|
923
|
-
* Time Complexity: O(1)
|
|
924
|
-
* Space Complexity: O(1)
|
|
925
|
-
*
|
|
926
|
-
* The addLast function adds an element to the end of an array-like data structure.
|
|
927
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
928
|
-
*/
|
|
929
|
-
addLast(element: E) {
|
|
930
|
-
if (this.size === 0) {
|
|
931
|
-
const mid = Math.floor(this.capacity / 2);
|
|
932
|
-
this._first = mid;
|
|
933
|
-
this._last = mid;
|
|
934
|
-
} else {
|
|
935
|
-
this._last++;
|
|
936
|
-
}
|
|
937
|
-
this.nodes[this.last] = element;
|
|
938
|
-
this._size++;
|
|
939
|
-
}
|
|
940
|
-
|
|
941
|
-
/**
|
|
942
|
-
* Time Complexity: O(1)
|
|
943
|
-
* Space Complexity: O(1)
|
|
944
|
-
*/
|
|
945
|
-
|
|
946
|
-
/**
|
|
947
|
-
* Time Complexity: O(1)
|
|
948
|
-
* Space Complexity: O(1)
|
|
949
|
-
*
|
|
950
|
-
* The function `pollFirst()` removes and returns the first element in a data structure.
|
|
951
|
-
* @returns The element of the first element in the data structure.
|
|
952
|
-
*/
|
|
953
|
-
pollFirst() {
|
|
954
|
-
if (!this.size) return;
|
|
955
|
-
const element = this.getFirst();
|
|
956
|
-
delete this.nodes[this.first];
|
|
957
|
-
this._first++;
|
|
958
|
-
this._size--;
|
|
959
|
-
return element;
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
/**
|
|
963
|
-
* Time Complexity: O(1)
|
|
964
|
-
* Space Complexity: O(1)
|
|
965
|
-
*/
|
|
966
|
-
|
|
967
|
-
/**
|
|
968
|
-
* Time Complexity: O(1)
|
|
969
|
-
* Space Complexity: O(1)
|
|
970
|
-
*
|
|
971
|
-
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
972
|
-
* @returns The element at the first position of the `_nodes` array.
|
|
973
|
-
*/
|
|
974
|
-
getFirst() {
|
|
975
|
-
if (this.size) return this.nodes[this.first];
|
|
976
|
-
}
|
|
977
|
-
|
|
978
|
-
/**
|
|
979
|
-
* Time Complexity: O(1)
|
|
980
|
-
* Space Complexity: O(1)
|
|
981
|
-
*/
|
|
982
|
-
|
|
983
|
-
/**
|
|
984
|
-
* Time Complexity: O(1)
|
|
985
|
-
* Space Complexity: O(1)
|
|
986
|
-
*
|
|
987
|
-
* The `pollLast()` function removes and returns the last element in a data structure.
|
|
988
|
-
* @returns The element that was removed from the data structure.
|
|
989
|
-
*/
|
|
990
|
-
pollLast() {
|
|
991
|
-
if (!this.size) return;
|
|
992
|
-
const element = this.getLast();
|
|
993
|
-
delete this.nodes[this.last];
|
|
994
|
-
this._last--;
|
|
995
|
-
this._size--;
|
|
996
|
-
|
|
997
|
-
return element;
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
/**
|
|
1001
|
-
* Time Complexity: O(1)
|
|
1002
|
-
* Space Complexity: O(1)
|
|
1003
|
-
*/
|
|
1004
|
-
|
|
1005
|
-
/**
|
|
1006
|
-
* Time Complexity: O(1)
|
|
1007
|
-
* Space Complexity: O(1)
|
|
1008
|
-
*
|
|
1009
|
-
* The `getLast()` function returns the last element in an array-like data structure.
|
|
1010
|
-
* @returns The last element in the array "_nodes" is being returned.
|
|
1011
|
-
*/
|
|
1012
|
-
getLast() {
|
|
1013
|
-
if (this.size) return this.nodes[this.last];
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
/**
|
|
1017
|
-
* Time Complexity: O(1)
|
|
1018
|
-
* Space Complexity: O(1)
|
|
1019
|
-
*/
|
|
1020
|
-
|
|
1021
|
-
/**
|
|
1022
|
-
* Time Complexity: O(1)
|
|
1023
|
-
* Space Complexity: O(1)
|
|
1024
|
-
*
|
|
1025
|
-
* The get function returns the element at the specified index in an array-like data structure.
|
|
1026
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
1027
|
-
* retrieve from the array.
|
|
1028
|
-
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
1029
|
-
* index, `undefined` is returned.
|
|
1030
|
-
*/
|
|
1031
|
-
get(index: number) {
|
|
1032
|
-
return this.nodes[this.first + index] || undefined;
|
|
1033
|
-
}
|
|
1034
|
-
|
|
1035
|
-
/**
|
|
1036
|
-
* The function checks if the size of a data structure is less than or equal to zero.
|
|
1037
|
-
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
1038
|
-
*/
|
|
1039
|
-
isEmpty() {
|
|
1040
|
-
return this.size <= 0;
|
|
1041
|
-
}
|
|
1042
|
-
}
|
|
852
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode, FamilyPosition, IterationType } from '../../../../src';
|
|
2
2
|
import { getRandomIntArray } from '../../../utils';
|
|
3
|
-
|
|
3
|
+
import { isDebugTest } from '../../../config';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const isDebug = isDebugTest;
|
|
6
6
|
|
|
7
7
|
describe('BinaryTreeNode', () => {
|
|
8
8
|
it('should create an instance of BinaryTreeNode', () => {
|
|
@@ -106,7 +106,7 @@ describe('BinaryTree', () => {
|
|
|
106
106
|
it('should delete nodes', () => {
|
|
107
107
|
expect(tree.getHeight(tree.root, IterationType.ITERATIVE)).toBe(-1);
|
|
108
108
|
expect(tree.getMinHeight()).toBe(-1);
|
|
109
|
-
const
|
|
109
|
+
const node1 = tree.add(1);
|
|
110
110
|
expect(tree.size).toBe(1);
|
|
111
111
|
|
|
112
112
|
const leftChild = new BinaryTreeNode<number>(2);
|
|
@@ -127,10 +127,10 @@ describe('BinaryTree', () => {
|
|
|
127
127
|
tree.delete(new BinaryTreeNode<number>(200));
|
|
128
128
|
tree.delete(rightChild);
|
|
129
129
|
|
|
130
|
-
if (
|
|
131
|
-
const result = tree.delete(
|
|
130
|
+
if (node1) {
|
|
131
|
+
const result = tree.delete(node1);
|
|
132
132
|
expect(result).toHaveLength(1);
|
|
133
|
-
expect(tree.size).toBe(
|
|
133
|
+
expect(tree.size).toBe(4);
|
|
134
134
|
expect(tree.getMinHeight(tree.root, IterationType.RECURSIVE)).toBe(1);
|
|
135
135
|
}
|
|
136
136
|
});
|
|
@@ -260,11 +260,18 @@ describe('BinaryTree', () => {
|
|
|
260
260
|
expect(tree.size).toBe(0);
|
|
261
261
|
expect(tree.root).toBeUndefined();
|
|
262
262
|
});
|
|
263
|
+
|
|
264
|
+
it('should duplicated nodes just replace the node exists', function () {
|
|
265
|
+
tree.clear();
|
|
266
|
+
tree.addMany([-10, -10, -10, 9, 9, 20, null, null, 15, 7, 8, null, 2, null, 6, null, null, 8, 8, 8]);
|
|
267
|
+
|
|
268
|
+
expect(tree.bfs(node => node ? node.key : null, undefined, undefined, true)).toEqual([-10, 9, 20, null, null, 15, 7, 8, null, 2, null, 6, null, null])
|
|
269
|
+
});
|
|
263
270
|
});
|
|
264
271
|
|
|
265
272
|
describe('BinaryTree Morris Traversal', () => {
|
|
266
273
|
// Create a binary tree
|
|
267
|
-
const tree = new BinaryTree<number
|
|
274
|
+
const tree = new BinaryTree<number>();
|
|
268
275
|
tree.add(1);
|
|
269
276
|
tree.add(2);
|
|
270
277
|
tree.add(3);
|
|
@@ -448,7 +455,7 @@ describe('BinaryTree', () => {
|
|
|
448
455
|
|
|
449
456
|
it('should get the height of the tree', () => {
|
|
450
457
|
tree.add([5, 'A']);
|
|
451
|
-
tree.add(
|
|
458
|
+
tree.add(3, 'B');
|
|
452
459
|
tree.add([7, 'C']);
|
|
453
460
|
|
|
454
461
|
expect(tree.getHeight()).toBe(1);
|
|
@@ -5,7 +5,7 @@ const isDebug = isDebugTest;
|
|
|
5
5
|
|
|
6
6
|
describe('BST operations test', () => {
|
|
7
7
|
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
8
|
-
const bst = new BST();
|
|
8
|
+
const bst = new BST<number, number>();
|
|
9
9
|
expect(bst).toBeInstanceOf(BST);
|
|
10
10
|
bst.add([11, 11]);
|
|
11
11
|
bst.add([3, 3]);
|
|
@@ -6,10 +6,10 @@ import { OrderedMap } from 'js-sdsl';
|
|
|
6
6
|
const isDebug = isDebugTest;
|
|
7
7
|
|
|
8
8
|
describe('RedBlackTree', () => {
|
|
9
|
-
let tree: RedBlackTree
|
|
9
|
+
let tree: RedBlackTree<number>;
|
|
10
10
|
|
|
11
11
|
beforeEach(() => {
|
|
12
|
-
tree = new RedBlackTree();
|
|
12
|
+
tree = new RedBlackTree<number>();
|
|
13
13
|
});
|
|
14
14
|
|
|
15
15
|
describe('add and getNode', () => {
|
|
@@ -141,10 +141,10 @@ describe('RedBlackTree', () => {
|
|
|
141
141
|
});
|
|
142
142
|
|
|
143
143
|
describe('RedBlackTree', () => {
|
|
144
|
-
let tree: RedBlackTree
|
|
144
|
+
let tree: RedBlackTree<number>;
|
|
145
145
|
|
|
146
146
|
beforeEach(() => {
|
|
147
|
-
tree = new RedBlackTree();
|
|
147
|
+
tree = new RedBlackTree<number>();
|
|
148
148
|
});
|
|
149
149
|
|
|
150
150
|
it('should add nodes into the tree', () => {
|
|
@@ -511,7 +511,7 @@ describe('RedBlackTree iterative methods test', () => {
|
|
|
511
511
|
beforeEach(() => {
|
|
512
512
|
rbTree = new RedBlackTree();
|
|
513
513
|
rbTree.add([1, 'a']);
|
|
514
|
-
rbTree.add(
|
|
514
|
+
rbTree.add(2, 'b');
|
|
515
515
|
rbTree.add([3, 'c']);
|
|
516
516
|
});
|
|
517
517
|
|