data-structure-typed 1.48.6 → 1.48.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +42 -51
  3. package/README_zh-CN.md +35 -44
  4. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +1 -9
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.js +5 -10
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/bst.d.ts +20 -0
  8. package/dist/cjs/data-structures/binary-tree/bst.js +37 -26
  9. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  10. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  11. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +4 -4
  12. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +4 -4
  13. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  14. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +4 -4
  15. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +4 -4
  16. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  17. package/dist/cjs/data-structures/queue/deque.d.ts +9 -9
  18. package/dist/cjs/data-structures/queue/deque.js +9 -9
  19. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  20. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +1 -9
  21. package/dist/mjs/data-structures/binary-tree/binary-tree.js +5 -10
  22. package/dist/mjs/data-structures/binary-tree/bst.d.ts +20 -0
  23. package/dist/mjs/data-structures/binary-tree/bst.js +37 -26
  24. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +4 -4
  25. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +4 -4
  26. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +4 -4
  27. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +4 -4
  28. package/dist/mjs/data-structures/queue/deque.d.ts +9 -9
  29. package/dist/mjs/data-structures/queue/deque.js +9 -9
  30. package/dist/umd/data-structure-typed.js +55 -53
  31. package/dist/umd/data-structure-typed.min.js +2 -2
  32. package/dist/umd/data-structure-typed.min.js.map +1 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/binary-tree/binary-tree.ts +5 -10
  35. package/src/data-structures/binary-tree/bst.ts +38 -26
  36. package/src/data-structures/hash/hash-map.ts +1 -1
  37. package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
  38. package/src/data-structures/linked-list/singly-linked-list.ts +4 -4
  39. package/src/data-structures/queue/deque.ts +10 -10
  40. package/src/data-structures/queue/queue.ts +1 -1
  41. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  42. package/src/types/data-structures/heap/heap.ts +1 -1
  43. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  44. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +2 -2
  45. package/test/unit/data-structures/queue/deque.test.ts +15 -15
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.48.6",
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. Usability is comparable to Python",
3
+ "version": "1.48.8",
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",
7
7
  "types": "dist/mjs/index.d.ts",
@@ -248,6 +248,9 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
248
248
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
249
249
  if (newNode === undefined) return;
250
250
 
251
+ // TODO There are still some problems with the way duplicate nodes are handled
252
+ if (newNode !== null && this.has(newNode.key)) return undefined;
253
+
251
254
  const _bfs = (root: N, newNode: N | null): N | undefined | null => {
252
255
  const queue = new Queue<N>([root]);
253
256
  while (queue.size > 0) {
@@ -325,17 +328,9 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
325
328
  * Space Complexity: O(1)
326
329
  */
327
330
 
328
- /**
329
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
330
- * Space Complexity: O(1)
331
- *
332
- * The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
333
- * @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
334
- * contain either `BTNodeExemplar` objects, keys, or entries.
335
- */
336
- refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<K, V, N>>): void {
331
+ refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): void {
337
332
  this.clear();
338
- this.addMany(nodesOrKeysOrEntries);
333
+ this.addMany(nodesOrKeysOrEntries, values);
339
334
  }
340
335
 
341
336
  /**
@@ -359,31 +359,44 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
359
359
  }
360
360
 
361
361
 
362
- // /**
363
- // * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
364
- // * Space Complexity: O(n) - Additional space is required for the sorted array.
365
- // */
366
- //
367
- // /**
368
- // * Time Complexity: O(log n) - Average case for a balanced tree.
369
- // * Space Complexity: O(1) - Constant space is used.
370
- // *
371
- // * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
372
- // * leftmost node if the comparison result is greater than.
373
- // * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
374
- // * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
375
- // * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
376
- // * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
377
- // * be performed. It can have one of the following values:
378
- // * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
379
- // * the key of the leftmost node if the comparison result is greater than, and the key of the
380
- // * rightmost node otherwise. If no node is found, it returns 0.
381
- // */
382
- // lastKey(beginRoot: BSTNodeKeyOrNode<K,N> = this.root, iterationType = this.iterationType): K {
383
- // if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
384
- // else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
385
- // else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
386
- // }
362
+ /**
363
+ * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
364
+ * Space Complexity: O(n) - Additional space is required for the sorted array.
365
+ */
366
+
367
+ /**
368
+ * Time Complexity: O(log n) - Average case for a balanced tree.
369
+ * Space Complexity: O(1) - Constant space is used.
370
+ *
371
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
372
+ * leftmost node if the comparison result is greater than.
373
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
374
+ * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
375
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
376
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
377
+ * be performed. It can have one of the following values:
378
+ * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
379
+ * the key of the leftmost node if the comparison result is greater than, and the key of the
380
+ * rightmost node otherwise. If no node is found, it returns 0.
381
+ */
382
+ lastKey(beginRoot: BSTNodeKeyOrNode<K, N> = this.root): K | undefined {
383
+ let current = this.ensureNode(beginRoot);
384
+ if (!current) return undefined;
385
+
386
+ if (this._variant === BSTVariant.MIN) {
387
+ // For BSTVariant.MIN, find the rightmost node
388
+ while (current.right !== undefined) {
389
+ current = current.right;
390
+ }
391
+ } else {
392
+ // For BSTVariant.MAX, find the leftmost node
393
+ while (current.left !== undefined) {
394
+ current = current.left;
395
+ }
396
+ }
397
+ return current.key;
398
+ }
399
+
387
400
 
388
401
  /**
389
402
  * Time Complexity: O(log n) - Average case for a balanced tree.
@@ -636,7 +649,6 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
636
649
  if (l <= r) {
637
650
  const m = l + Math.floor((r - l) / 2);
638
651
  const midNode = sorted[m];
639
- debugger;
640
652
  this.add([midNode.key, midNode.value]);
641
653
  stack.push([m + 1, r]);
642
654
  stack.push([l, m - 1]);
@@ -625,7 +625,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
625
625
  protected* _getIterator() {
626
626
  let node = this._head;
627
627
  while (node !== this._sentinel) {
628
- yield <[K, V]>[node.key, node.value];
628
+ yield [node.key, node.value] as [K, V];
629
629
  node = node.next;
630
630
  }
631
631
  }
@@ -162,11 +162,11 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
162
162
  * Time Complexity: O(1)
163
163
  * Space Complexity: O(1)
164
164
  *
165
- * The `popLast()` function removes and returns the value of the last node in a doubly linked list.
165
+ * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
166
166
  * @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
167
167
  * list is empty, it returns undefined.
168
168
  */
169
- popLast(): E | undefined {
169
+ pollLast(): E | undefined {
170
170
  return this.pop();
171
171
  }
172
172
 
@@ -206,11 +206,11 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
206
206
  * Time Complexity: O(1)
207
207
  * Space Complexity: O(1)
208
208
  *
209
- * The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
209
+ * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
210
210
  * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
211
211
  * list.
212
212
  */
213
- popFirst(): E | undefined {
213
+ pollFirst(): E | undefined {
214
214
  return this.shift();
215
215
  }
216
216
 
@@ -164,12 +164,12 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
164
164
  * Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
165
165
  * Space Complexity: O(1) - Constant space.
166
166
  *
167
- * The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
167
+ * The `pollLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
168
168
  * pointers accordingly.
169
169
  * @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
170
170
  * the linked list is empty, it returns `undefined`.
171
171
  */
172
- popLast(): E | undefined {
172
+ pollLast(): E | undefined {
173
173
  return this.pop();
174
174
  }
175
175
 
@@ -202,10 +202,10 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
202
202
  * Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
203
203
  * Space Complexity: O(1) - Constant space.
204
204
  *
205
- * The `popFirst()` function removes and returns the value of the first node in a linked list.
205
+ * The `pollFirst()` function removes and returns the value of the first node in a linked list.
206
206
  * @returns The value of the node that is being removed from the beginning of the linked list.
207
207
  */
208
- popFirst(): E | undefined {
208
+ pollFirst(): E | undefined {
209
209
  return this.shift();
210
210
  }
211
211
 
@@ -120,10 +120,10 @@ export class Deque<E> extends IterableElementBase<E> {
120
120
  * Time Complexity: O(1) - Removes the last element.
121
121
  * Space Complexity: O(1) - Operates in-place.
122
122
  *
123
- * The function "popLast" removes and returns the last element of an array.
123
+ * The function "pollLast" removes and returns the last element of an array.
124
124
  * @returns The last element of the array is being returned.
125
125
  */
126
- popLast(): E | undefined {
126
+ pollLast(): E | undefined {
127
127
  return this.pop();
128
128
  }
129
129
 
@@ -143,11 +143,11 @@ export class Deque<E> extends IterableElementBase<E> {
143
143
  * Time Complexity: O(1) - Removes the first element.
144
144
  * Space Complexity: O(1) - In-place operation.
145
145
  *
146
- * The function "popFirst" removes and returns the first element of an array.
147
- * @returns The method `popFirst()` is returning the first element of the array after removing it
146
+ * The function "pollFirst" removes and returns the first element of an array.
147
+ * @returns The method `pollFirst()` is returning the first element of the array after removing it
148
148
  * from the beginning. If the array is empty, it will return `undefined`.
149
149
  */
150
- popFirst(): E | undefined {
150
+ pollFirst(): E | undefined {
151
151
  return this.shift();
152
152
  }
153
153
 
@@ -947,10 +947,10 @@ export class ObjectDeque<E = number> {
947
947
  * Time Complexity: O(1)
948
948
  * Space Complexity: O(1)
949
949
  *
950
- * The function `popFirst()` removes and returns the first element in a data structure.
950
+ * The function `pollFirst()` removes and returns the first element in a data structure.
951
951
  * @returns The element of the first element in the data structure.
952
952
  */
953
- popFirst() {
953
+ pollFirst() {
954
954
  if (!this.size) return;
955
955
  const element = this.getFirst();
956
956
  delete this.nodes[this.first];
@@ -984,10 +984,10 @@ export class ObjectDeque<E = number> {
984
984
  * Time Complexity: O(1)
985
985
  * Space Complexity: O(1)
986
986
  *
987
- * The `popLast()` function removes and returns the last element in a data structure.
987
+ * The `pollLast()` function removes and returns the last element in a data structure.
988
988
  * @returns The element that was removed from the data structure.
989
989
  */
990
- popLast() {
990
+ pollLast() {
991
991
  if (!this.size) return;
992
992
  const element = this.getLast();
993
993
  delete this.nodes[this.last];
@@ -1039,4 +1039,4 @@ export class ObjectDeque<E = number> {
1039
1039
  isEmpty() {
1040
1040
  return this.size <= 0;
1041
1041
  }
1042
- }
1042
+ }
@@ -378,4 +378,4 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
378
378
  peek(): E | undefined {
379
379
  return this.getFirst();
380
380
  }
381
- }
381
+ }
@@ -7,4 +7,4 @@ export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNo
7
7
 
8
8
  export type RedBlackTreeNested<K, V, N extends RedBlackTreeNode<K, V, N>> = RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
9
9
 
10
- export type RBTreeOptions<K> = BSTOptions<K> & {};
10
+ export type RBTreeOptions<K> = BSTOptions<K> & {};
@@ -1,3 +1,3 @@
1
1
  import { Comparator } from "../../common";
2
2
 
3
- export type HeapOptions<T> = { comparator: Comparator<T> }
3
+ export type HeapOptions<T> = { comparator: Comparator<T> }
@@ -1,3 +1,3 @@
1
1
  import { HeapOptions } from "../heap";
2
2
 
3
- export type PriorityQueueOptions<T> = HeapOptions<T> & {}
3
+ export type PriorityQueueOptions<T> = HeapOptions<T> & {}
@@ -32,7 +32,7 @@ describe('SinglyLinkedList Operation Test', () => {
32
32
  list.push(3);
33
33
  const popped = list.pop();
34
34
  expect(popped).toBe(3);
35
- expect(list.popLast()).toBe(2);
35
+ expect(list.pollLast()).toBe(2);
36
36
  expect(list.toArray()).toEqual([1]);
37
37
  });
38
38
 
@@ -49,7 +49,7 @@ describe('SinglyLinkedList Operation Test', () => {
49
49
  list.push(3);
50
50
  const shifted = list.shift();
51
51
  expect(shifted).toBe(1);
52
- expect(list.popFirst()).toBe(2);
52
+ expect(list.pollFirst()).toBe(2);
53
53
  expect(list.toArray()).toEqual([3]);
54
54
  });
55
55
 
@@ -22,13 +22,13 @@ describe('Deque Tests', () => {
22
22
  it('should delete elements from the beginning and end', () => {
23
23
  deque.addFirst(1);
24
24
  deque.addLast(2);
25
- deque.popFirst();
26
- deque.popLast();
25
+ deque.pollFirst();
26
+ deque.pollLast();
27
27
  expect(deque.isEmpty()).toBe(true);
28
28
  });
29
29
 
30
30
  it('should handle edge case when removing from an empty deque', () => {
31
- const result = deque.popFirst();
31
+ const result = deque.pollFirst();
32
32
  expect(result).toBeUndefined();
33
33
  });
34
34
 
@@ -40,18 +40,18 @@ describe('Deque Tests', () => {
40
40
 
41
41
  it('should handle adding and removing elements alternately', () => {
42
42
  deque.addFirst(1);
43
- expect(deque.popFirst()).toBe(1);
43
+ expect(deque.pollFirst()).toBe(1);
44
44
  deque.addLast(2);
45
- expect(deque.popLast()).toBe(2);
45
+ expect(deque.pollLast()).toBe(2);
46
46
  expect(deque.isEmpty()).toBe(true);
47
47
  });
48
48
 
49
49
  it('should handle adding and removing elements in a cyclic manner', () => {
50
50
  deque.addFirst(1);
51
51
  deque.addLast(2);
52
- expect(deque.popFirst()).toBe(1);
52
+ expect(deque.pollFirst()).toBe(1);
53
53
  deque.addFirst(3);
54
- expect(deque.popLast()).toBe(2);
54
+ expect(deque.pollLast()).toBe(2);
55
55
  expect(deque.size).toBe(1);
56
56
  });
57
57
  // Add more test cases as needed
@@ -75,13 +75,13 @@ describe('Deque Tests', () => {
75
75
  // it('should delete elements from the beginning and end', () => {
76
76
  // objectDeque.addFirst('one');
77
77
  // objectDeque.addLast('two');
78
- // objectDeque.popFirst();
79
- // objectDeque.popLast();
78
+ // objectDeque.pollFirst();
79
+ // objectDeque.pollLast();
80
80
  // expect(objectDeque.isEmpty()).toBe(true);
81
81
  // });
82
82
  //
83
83
  // it('should handle edge case when removing from an empty deque', () => {
84
- // const result = objectDeque.popFirst();
84
+ // const result = objectDeque.pollFirst();
85
85
  // expect(result).toBeUndefined();
86
86
  // });
87
87
  //
@@ -136,8 +136,8 @@ describe('Deque', () => {
136
136
  deque.addFirst(1);
137
137
  deque.addLast(2);
138
138
 
139
- const firstElement = deque.popFirst();
140
- const lastElement = deque.popLast();
139
+ const firstElement = deque.pollFirst();
140
+ const lastElement = deque.pollLast();
141
141
 
142
142
  expect(deque.size).toBe(0);
143
143
  expect(firstElement).toBe(1);
@@ -166,7 +166,7 @@ describe('Deque', () => {
166
166
  deque.addLast(1);
167
167
  expect(deque.isEmpty()).toBe(false);
168
168
 
169
- deque.popFirst();
169
+ deque.pollFirst();
170
170
  expect(deque.isEmpty()).toBe(true);
171
171
  });
172
172
  });
@@ -200,7 +200,7 @@ describe('Deque', () => {
200
200
  // deque.addLast(1);
201
201
  // deque.addLast(2);
202
202
  //
203
- // const removedElement = deque.popFirst();
203
+ // const removedElement = deque.pollFirst();
204
204
  //
205
205
  // expect(deque.size).toBe(1);
206
206
  // expect(removedElement).toBe(1);
@@ -211,7 +211,7 @@ describe('Deque', () => {
211
211
  // deque.addLast(1);
212
212
  // deque.addLast(2);
213
213
  //
214
- // const removedElement = deque.popFirst();
214
+ // const removedElement = deque.pollFirst();
215
215
  //
216
216
  // expect(deque.size).toBe(1);
217
217
  // expect(removedElement).toBe(1);