data-structure-typed 1.51.8 → 1.51.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 +13 -13
- package/benchmark/report.html +1 -37
- package/benchmark/report.json +13 -385
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +80 -60
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +316 -224
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +471 -361
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +198 -200
- package/dist/cjs/data-structures/binary-tree/bst.js +215 -249
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +71 -72
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/cjs/data-structures/hash/hash-map.js +40 -55
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -3
- package/dist/cjs/data-structures/queue/deque.js +2 -3
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +1 -1
- package/dist/cjs/data-structures/trie/trie.js +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +6 -6
- package/dist/cjs/types/common.d.ts +1 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/cjs/utils/utils.js +3 -5
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +80 -60
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +316 -224
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +472 -362
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +198 -200
- package/dist/mjs/data-structures/binary-tree/bst.js +219 -255
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +71 -72
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/mjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/mjs/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/mjs/data-structures/hash/hash-map.js +41 -55
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -3
- package/dist/mjs/data-structures/queue/deque.js +2 -3
- package/dist/mjs/data-structures/trie/trie.d.ts +1 -1
- package/dist/mjs/data-structures/trie/trie.js +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +6 -6
- package/dist/mjs/types/common.d.ts +1 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/mjs/utils/utils.js +3 -5
- package/dist/umd/data-structure-typed.js +1340 -1181
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
- package/src/data-structures/binary-tree/avl-tree.ts +94 -66
- package/src/data-structures/binary-tree/binary-tree.ts +530 -398
- package/src/data-structures/binary-tree/bst.ts +251 -270
- package/src/data-structures/binary-tree/rb-tree.ts +121 -100
- package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +8 -7
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/utils/utils.ts +3 -3
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +3 -3
- package/test/integration/index.html +25 -11
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +68 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
- package/test/utils/big-o.ts +12 -6
- package/tsconfig-base.json +1 -0
|
@@ -358,7 +358,7 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
358
358
|
* @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
|
|
359
359
|
* values as the original `LinkedListQueue`.
|
|
360
360
|
*/
|
|
361
|
-
clone(): LinkedListQueue<E> {
|
|
361
|
+
override clone(): LinkedListQueue<E> {
|
|
362
362
|
return new LinkedListQueue<E>(this.values());
|
|
363
363
|
}
|
|
364
364
|
}
|
|
@@ -91,7 +91,7 @@ export class TrieNode {
|
|
|
91
91
|
* 8. Autocomplete: Providing recommended words or phrases as a user types.
|
|
92
92
|
* 9. Spell Check: Checking the spelling of words.
|
|
93
93
|
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
|
94
|
-
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
|
|
94
|
+
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
|
|
95
95
|
*/
|
|
96
96
|
export class Trie extends IterableElementBase<string, Trie> {
|
|
97
97
|
/**
|
|
@@ -187,7 +187,7 @@ export class Trie extends IterableElementBase<string, Trie> {
|
|
|
187
187
|
* @param {string} word - The word to check for.
|
|
188
188
|
* @returns {boolean} True if the word is present in the Trie.
|
|
189
189
|
*/
|
|
190
|
-
has(word: string): boolean {
|
|
190
|
+
override has(word: string): boolean {
|
|
191
191
|
word = this._caseProcess(word);
|
|
192
192
|
let cur = this.root;
|
|
193
193
|
for (const c of word) {
|
|
@@ -12,16 +12,17 @@ import type {
|
|
|
12
12
|
export interface IBinaryTree<
|
|
13
13
|
K extends Comparable,
|
|
14
14
|
V = any,
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
R = [K, V],
|
|
16
|
+
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>,
|
|
17
|
+
TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTreeNested<K, V, R, NODE>
|
|
17
18
|
> {
|
|
18
|
-
createNode(key: K, value?:
|
|
19
|
+
createNode(key: K, value?: NODE['value']): NODE;
|
|
19
20
|
|
|
20
|
-
createTree(options?: Partial<BinaryTreeOptions
|
|
21
|
+
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE;
|
|
21
22
|
|
|
22
|
-
add(
|
|
23
|
+
add(keyOrNodeOrEntryOrRawElement: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
|
|
23
24
|
|
|
24
|
-
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V,
|
|
25
|
+
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
|
|
25
26
|
|
|
26
|
-
delete<C extends BTNCallback<
|
|
27
|
+
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<NODE>[];
|
|
27
28
|
}
|
package/src/types/common.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export type BSTVariant = 'STANDARD' | 'INVERSE';
|
|
2
1
|
export type CP = 1 | -1 | 0;
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -39,7 +38,7 @@ export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
|
|
|
39
38
|
|
|
40
39
|
export type BTNodePureKeyOrNode<K, N> = K | N;
|
|
41
40
|
|
|
42
|
-
export type
|
|
41
|
+
export type BTNPureKeyOrNodeOrEntry<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
|
|
43
42
|
|
|
44
43
|
export type BSTNKeyOrNode<K, N> = K | undefined | N;
|
|
45
44
|
|
|
@@ -4,6 +4,6 @@ import { Comparable } from "../../utils";
|
|
|
4
4
|
|
|
5
5
|
export type AVLTreeMultiMapNodeNested<K extends Comparable, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
6
6
|
|
|
7
|
-
export type AVLTreeMultiMapNested<K extends Comparable, V,
|
|
7
|
+
export type AVLTreeMultiMapNested<K extends Comparable, V, R, NODE extends AVLTreeMultiMapNode<K, V, NODE>> = AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMap<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
8
8
|
|
|
9
|
-
export type AVLTreeMultiMapOptions<K> = AVLTreeOptions<K> & {}
|
|
9
|
+
export type AVLTreeMultiMapOptions<K, V, R> = AVLTreeOptions<K, V, R> & {}
|
|
@@ -4,6 +4,6 @@ import { Comparable } from "../../utils";
|
|
|
4
4
|
|
|
5
5
|
export type AVLTreeNodeNested<K extends Comparable, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
6
6
|
|
|
7
|
-
export type AVLTreeNested<K extends Comparable, V,
|
|
7
|
+
export type AVLTreeNested<K extends Comparable, V, R, NODE extends AVLTreeNode<K, V, NODE>> = AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, AVLTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
8
8
|
|
|
9
|
-
export type AVLTreeOptions<K> = BSTOptions<K> & {};
|
|
9
|
+
export type AVLTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
|
|
2
|
-
import { IterationType } from
|
|
3
|
-
import { Comparable } from
|
|
2
|
+
import { BTNEntry, IterationType } from '../../common';
|
|
3
|
+
import { Comparable } from '../../utils';
|
|
4
4
|
|
|
5
5
|
export type BinaryTreeNodeNested<K extends Comparable, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
6
6
|
|
|
7
|
-
export type BinaryTreeNested<K extends Comparable, V, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
7
|
+
export type BinaryTreeNested<K extends Comparable, V, R, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, BinaryTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
8
8
|
|
|
9
|
-
export type BinaryTreeOptions = {
|
|
9
|
+
export type BinaryTreeOptions<K, V, R> = {
|
|
10
10
|
iterationType?: IterationType
|
|
11
|
+
toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
|
|
11
12
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { BST, BSTNode } from '../../../data-structures';
|
|
2
2
|
import type { BinaryTreeOptions } from './binary-tree';
|
|
3
|
-
import { Comparator } from
|
|
4
|
-
import { Comparable } from
|
|
3
|
+
import { Comparator } from '../../common';
|
|
4
|
+
import { Comparable } from '../../utils';
|
|
5
5
|
|
|
6
6
|
export type BSTNodeNested<K extends Comparable, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
7
7
|
|
|
8
|
-
export type BSTNested<K extends Comparable, V,
|
|
8
|
+
export type BSTNested<K extends Comparable, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
9
9
|
|
|
10
|
-
export type BSTOptions<K> = BinaryTreeOptions & {
|
|
10
|
+
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
|
|
11
11
|
comparator?: Comparator<K>
|
|
12
12
|
}
|
|
@@ -6,6 +6,6 @@ export type RBTNColor = 'RED' | 'BLACK';
|
|
|
6
6
|
|
|
7
7
|
export type RedBlackTreeNodeNested<K extends Comparable, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
8
8
|
|
|
9
|
-
export type RedBlackTreeNested<K extends Comparable, V,
|
|
9
|
+
export type RedBlackTreeNested<K extends Comparable, V, R, NODE extends RedBlackTreeNode<K, V, NODE>> = RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
10
10
|
|
|
11
|
-
export type RBTreeOptions<K> = BSTOptions<K> & {};
|
|
11
|
+
export type RBTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { TreeMultiMap, TreeMultiMapNode } from '../../../data-structures';
|
|
2
2
|
import type { RBTreeOptions } from './rb-tree';
|
|
3
|
-
import { Comparable } from
|
|
3
|
+
import { Comparable } from '../../utils';
|
|
4
4
|
|
|
5
5
|
export type TreeMultiMapNodeNested<K extends Comparable, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
6
6
|
|
|
7
|
-
export type TreeMultiMapNested<K extends Comparable, V,
|
|
7
|
+
export type TreeMultiMapNested<K extends Comparable, V, R, NODE extends TreeMultiMapNode<K, V, NODE>> = TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
8
8
|
|
|
9
|
-
export type TreeMultiMapOptions<K> = RBTreeOptions<K> & {}
|
|
9
|
+
export type TreeMultiMapOptions<K, V, R> = RBTreeOptions<K, V, R> & {}
|
package/src/utils/utils.ts
CHANGED
|
@@ -108,7 +108,7 @@ export const roundFixed = (num: number, digit: number = 10) => {
|
|
|
108
108
|
|
|
109
109
|
export function isComparable(key: any): key is Comparable {
|
|
110
110
|
const keyType = typeof key;
|
|
111
|
-
if (keyType === 'number') return isNaN(key);
|
|
111
|
+
if (keyType === 'number') return !isNaN(key);
|
|
112
112
|
if (keyType === 'string') return true;
|
|
113
113
|
if (keyType === 'bigint') return true;
|
|
114
114
|
if (keyType === 'boolean') return true;
|
|
@@ -117,8 +117,8 @@ export function isComparable(key: any): key is Comparable {
|
|
|
117
117
|
if (keyType === 'function') return isComparable(key());
|
|
118
118
|
if (keyType === 'object') {
|
|
119
119
|
if (key === null) return true;
|
|
120
|
-
if (typeof key.valueOf === 'function') return isComparable(key.valueOf());
|
|
121
|
-
if (typeof key.toString === 'function') return isComparable(key.toString());
|
|
120
|
+
// if (typeof key.valueOf === 'function') return isComparable(key.valueOf()); // This will keep recursing because every object has a valueOf method.
|
|
121
|
+
// if (typeof key.toString === 'function') return isComparable(key.toString()); // This will also keep recursing because every string type has a toString method.
|
|
122
122
|
return false;
|
|
123
123
|
}
|
|
124
124
|
|
|
@@ -20,7 +20,7 @@ describe('AVL Tree Test', () => {
|
|
|
20
20
|
[10, 10],
|
|
21
21
|
[5, 5]
|
|
22
22
|
];
|
|
23
|
-
const tree = new AVLTree();
|
|
23
|
+
const tree = new AVLTree<number, number>();
|
|
24
24
|
|
|
25
25
|
for (const i of arr) tree.add(i);
|
|
26
26
|
|
|
@@ -44,7 +44,7 @@ describe('AVL Tree Test', () => {
|
|
|
44
44
|
expect(subTreeSum).toBe(70);
|
|
45
45
|
|
|
46
46
|
let lesserSum = 0;
|
|
47
|
-
tree.lesserOrGreaterTraverse(node => (lesserSum += node.key),
|
|
47
|
+
tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
|
|
48
48
|
expect(lesserSum).toBe(45);
|
|
49
49
|
|
|
50
50
|
// node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
|
|
@@ -2,7 +2,7 @@ import { BST, BSTNode } from 'bst-typed';
|
|
|
2
2
|
|
|
3
3
|
describe('Individual package BST operations test', () => {
|
|
4
4
|
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
5
|
-
const bst = new BST();
|
|
5
|
+
const bst = new BST<number>();
|
|
6
6
|
expect(bst).toBeInstanceOf(BST);
|
|
7
7
|
bst.add([11, 11]);
|
|
8
8
|
bst.add([3, 3]);
|
|
@@ -38,7 +38,7 @@ describe('Individual package BST operations test', () => {
|
|
|
38
38
|
expect(subTreeSum).toBe(70);
|
|
39
39
|
|
|
40
40
|
let lesserSum = 0;
|
|
41
|
-
bst.lesserOrGreaterTraverse(node => (lesserSum += node.key),
|
|
41
|
+
bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
|
|
42
42
|
expect(lesserSum).toBe(45);
|
|
43
43
|
|
|
44
44
|
expect(node15).toBeInstanceOf(BSTNode);
|
|
@@ -235,7 +235,7 @@ describe('Individual package BST operations test', () => {
|
|
|
235
235
|
expect(subTreeSum).toBe(70);
|
|
236
236
|
|
|
237
237
|
let lesserSum = 0;
|
|
238
|
-
objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key),
|
|
238
|
+
objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
|
|
239
239
|
expect(lesserSum).toBe(45);
|
|
240
240
|
|
|
241
241
|
expect(node15).toBeInstanceOf(BSTNode);
|
|
@@ -11,6 +11,20 @@
|
|
|
11
11
|
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.44.0/dist/umd/data-structure-typed.min.js'></script>-->
|
|
12
12
|
|
|
13
13
|
<script src='https://unpkg.com/js-sdsl@4.4.2/dist/umd/js-sdsl.js'></script>
|
|
14
|
+
<script>
|
|
15
|
+
const logPerf = function (label = 'function running cost', fn, args, thisArg) {
|
|
16
|
+
const start = performance.now();
|
|
17
|
+
let result;
|
|
18
|
+
if (thisArg) {
|
|
19
|
+
if (args && args.length > 0) result = fn.apply(thisArg, args);
|
|
20
|
+
else result = fn.apply(thisArg);
|
|
21
|
+
} else {
|
|
22
|
+
if (args && args.length > 0) result = fn(...args);
|
|
23
|
+
else result = fn();
|
|
24
|
+
}
|
|
25
|
+
console.log(`${(performance.now() - start).toFixed(2)} ms, ${label}, ${result}`);
|
|
26
|
+
}
|
|
27
|
+
</script>
|
|
14
28
|
</head>
|
|
15
29
|
<body>
|
|
16
30
|
<div id='app'>
|
|
@@ -26,19 +40,19 @@
|
|
|
26
40
|
const { Queue } = dataStructureTyped;
|
|
27
41
|
const queue = new Queue();
|
|
28
42
|
const n = 1000000;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
let last = 0;
|
|
35
|
-
const startTime = performance.now();
|
|
43
|
+
logPerf(`Queue ${n.toLocaleString()} enqueue`, () => {
|
|
44
|
+
for (let i = 0; i < n; i++) {
|
|
45
|
+
queue.push(i);
|
|
46
|
+
}
|
|
47
|
+
})
|
|
36
48
|
|
|
37
|
-
|
|
38
|
-
last = queue.shift();
|
|
39
|
-
}
|
|
49
|
+
let last = 0;
|
|
40
50
|
|
|
41
|
-
|
|
51
|
+
logPerf(`Queue ${n.toLocaleString()} dequeue `, () => {
|
|
52
|
+
for (let i = 0; i < n; i++) {
|
|
53
|
+
last = queue.shift();
|
|
54
|
+
}
|
|
55
|
+
})
|
|
42
56
|
|
|
43
57
|
} catch (e) {
|
|
44
58
|
console.error(e);
|
|
@@ -689,3 +689,71 @@ describe('AVLTreeMultiMap iterative methods test', () => {
|
|
|
689
689
|
expect([...values]).toEqual(['a', 'b', 'c']);
|
|
690
690
|
});
|
|
691
691
|
});
|
|
692
|
+
|
|
693
|
+
describe('AVLTree toEntryFn', () => {
|
|
694
|
+
it('should toEntryFn 1', () => {
|
|
695
|
+
const tree = new AVLTreeMultiMap<number, number, { obj: { id: number } }>([], {
|
|
696
|
+
toEntryFn: ele => [ele.obj.id, ele.obj.id]
|
|
697
|
+
});
|
|
698
|
+
tree.add({ obj: { id: 1 } });
|
|
699
|
+
tree.add({ obj: { id: 2 } });
|
|
700
|
+
tree.add({ obj: { id: 3 } });
|
|
701
|
+
tree.add({ obj: { id: 4 } });
|
|
702
|
+
tree.add({ obj: { id: 5 } });
|
|
703
|
+
|
|
704
|
+
const expected = [1, 2, 3, 4, 5];
|
|
705
|
+
|
|
706
|
+
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
|
|
707
|
+
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
|
|
708
|
+
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
it('should toEntryFn 2', () => {
|
|
712
|
+
const tree = new AVLTreeMultiMap<number, number, { obj: { id: number } }>(
|
|
713
|
+
[{ obj: { id: 1 } }, { obj: { id: 2 } }, { obj: { id: 3 } }, { obj: { id: 4 } }, { obj: { id: 5 } }],
|
|
714
|
+
{
|
|
715
|
+
toEntryFn: ele => [ele.obj.id, ele.obj.id]
|
|
716
|
+
}
|
|
717
|
+
);
|
|
718
|
+
|
|
719
|
+
const expected = [1, 2, 3, 4, 5];
|
|
720
|
+
|
|
721
|
+
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
|
|
722
|
+
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
|
|
723
|
+
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
it('should toEntryFn throw error', () => {
|
|
727
|
+
expect(
|
|
728
|
+
() =>
|
|
729
|
+
new AVLTreeMultiMap<{ obj: { id: number } }, number>([
|
|
730
|
+
{ obj: { id: 1 } },
|
|
731
|
+
{ obj: { id: 2 } },
|
|
732
|
+
{ obj: { id: 3 } },
|
|
733
|
+
{ obj: { id: 4 } },
|
|
734
|
+
{ obj: { id: 5 } }
|
|
735
|
+
])
|
|
736
|
+
).toThrowError(
|
|
737
|
+
'When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.'
|
|
738
|
+
);
|
|
739
|
+
});
|
|
740
|
+
|
|
741
|
+
it('should toEntryFn 3', () => {
|
|
742
|
+
const tree = new AVLTreeMultiMap<{ obj: { id: number } }, number>(
|
|
743
|
+
[{ obj: { id: 1 } }, { obj: { id: 2 } }, { obj: { id: 3 } }, { obj: { id: 4 } }, { obj: { id: 5 } }],
|
|
744
|
+
{ comparator: (a, b) => a.obj.id - b.obj.id }
|
|
745
|
+
);
|
|
746
|
+
|
|
747
|
+
const expected = [
|
|
748
|
+
{ obj: { id: 1 } },
|
|
749
|
+
{ obj: { id: 2 } },
|
|
750
|
+
{ obj: { id: 3 } },
|
|
751
|
+
{ obj: { id: 4 } },
|
|
752
|
+
{ obj: { id: 5 } }
|
|
753
|
+
];
|
|
754
|
+
|
|
755
|
+
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
|
|
756
|
+
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
|
|
757
|
+
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
|
|
758
|
+
});
|
|
759
|
+
});
|
|
@@ -403,6 +403,62 @@ describe('BinaryTree Morris Traversal', () => {
|
|
|
403
403
|
});
|
|
404
404
|
});
|
|
405
405
|
|
|
406
|
+
describe('BinaryTree toEntryFn', () => {
|
|
407
|
+
it('should toEntryFn 1', () => {
|
|
408
|
+
const tree = new BinaryTree<number, number, { obj: { id: number } }>([], {
|
|
409
|
+
toEntryFn: ele => [ele.obj.id, ele.obj.id]
|
|
410
|
+
});
|
|
411
|
+
tree.add({ obj: { id: 1 } });
|
|
412
|
+
tree.add({ obj: { id: 2 } });
|
|
413
|
+
tree.add({ obj: { id: 3 } });
|
|
414
|
+
tree.add({ obj: { id: 4 } });
|
|
415
|
+
tree.add({ obj: { id: 5 } });
|
|
416
|
+
|
|
417
|
+
const expected = [4, 2, 5, 1, 3];
|
|
418
|
+
|
|
419
|
+
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
|
|
420
|
+
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
|
|
421
|
+
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it('should toEntryFn 2', () => {
|
|
425
|
+
const tree = new BinaryTree<number, number, { obj: { id: number } }>(
|
|
426
|
+
[{ obj: { id: 1 } }, { obj: { id: 2 } }, { obj: { id: 3 } }, { obj: { id: 4 } }, { obj: { id: 5 } }],
|
|
427
|
+
{
|
|
428
|
+
toEntryFn: ele => [ele.obj.id, ele.obj.id]
|
|
429
|
+
}
|
|
430
|
+
);
|
|
431
|
+
|
|
432
|
+
const expected = [4, 2, 5, 1, 3];
|
|
433
|
+
|
|
434
|
+
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
|
|
435
|
+
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
|
|
436
|
+
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it('should toEntryFn 3', () => {
|
|
440
|
+
const tree = new BinaryTree<{ obj: { id: number } }, number>([
|
|
441
|
+
{ obj: { id: 1 } },
|
|
442
|
+
{ obj: { id: 2 } },
|
|
443
|
+
{ obj: { id: 3 } },
|
|
444
|
+
{ obj: { id: 4 } },
|
|
445
|
+
{ obj: { id: 5 } }
|
|
446
|
+
]);
|
|
447
|
+
|
|
448
|
+
const expected = [
|
|
449
|
+
{ obj: { id: 4 } },
|
|
450
|
+
{ obj: { id: 2 } },
|
|
451
|
+
{ obj: { id: 5 } },
|
|
452
|
+
{ obj: { id: 1 } },
|
|
453
|
+
{ obj: { id: 3 } }
|
|
454
|
+
];
|
|
455
|
+
|
|
456
|
+
expect(tree.morris(node => node.key, 'IN')).toEqual(expected);
|
|
457
|
+
expect(tree.dfs(node => node.key, 'IN')).toEqual(expected);
|
|
458
|
+
expect(tree.dfs(node => node.key, 'IN', tree.root, 'RECURSIVE')).toEqual(expected);
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
|
|
406
462
|
describe('BinaryTree traversals', () => {
|
|
407
463
|
const tree = new BinaryTree<number>();
|
|
408
464
|
|
|
@@ -132,11 +132,11 @@ class MyDirectedGraph<
|
|
|
132
132
|
VO extends MyVertex<V> = MyVertex<V>,
|
|
133
133
|
EO extends MyEdge<E> = MyEdge<E>
|
|
134
134
|
> extends DirectedGraph<V, E, VO, EO> {
|
|
135
|
-
createVertex(key: VertexKey, value: V): VO {
|
|
135
|
+
override createVertex(key: VertexKey, value: V): VO {
|
|
136
136
|
return new MyVertex(key, value) as VO;
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
|
139
|
+
override createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO {
|
|
140
140
|
return new MyEdge(src, dest, weight ?? 1, value) as EO;
|
|
141
141
|
}
|
|
142
142
|
|
package/test/utils/big-o.ts
CHANGED
|
@@ -206,14 +206,20 @@ export function logBigOMetrics(target: any, propertyKey: string, descriptor: Pro
|
|
|
206
206
|
return descriptor;
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
-
export const logPerf = function (
|
|
209
|
+
export const logPerf = function (
|
|
210
|
+
label: string = 'function running cost',
|
|
211
|
+
fn: (...args: any[]) => any,
|
|
212
|
+
args: any[],
|
|
213
|
+
thisArg?: any
|
|
214
|
+
) {
|
|
210
215
|
const start = performance.now();
|
|
216
|
+
let result: any;
|
|
211
217
|
if (thisArg) {
|
|
212
|
-
if (args && args.length > 0) fn.apply(thisArg, args);
|
|
213
|
-
else fn.apply(thisArg);
|
|
218
|
+
if (args && args.length > 0) result = fn.apply(thisArg, args);
|
|
219
|
+
else result = fn.apply(thisArg);
|
|
214
220
|
} else {
|
|
215
|
-
if (args && args.length > 0) fn(...args);
|
|
216
|
-
else fn();
|
|
221
|
+
if (args && args.length > 0) result = fn(...args);
|
|
222
|
+
else result = fn();
|
|
217
223
|
}
|
|
218
|
-
console.log(
|
|
224
|
+
console.log(`${(performance.now() - start).toFixed(2)} ms, ${label}, ${result}`);
|
|
219
225
|
};
|
package/tsconfig-base.json
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"strict": true, // Enable strict type checking, including enabling all strict options.
|
|
17
17
|
"traceResolution": false, // Whether to trace the module resolution process.
|
|
18
18
|
"types": ["node", "jest"], // Specifies the name of the type declaration file to include.
|
|
19
|
+
"noImplicitOverride": true,
|
|
19
20
|
"typeRoots": ["node_modules/@types", "src/types", "test/types"]
|
|
20
21
|
},
|
|
21
22
|
"compileOnSave": false, // Whether to automatically compile when saving.
|