data-structure-typed 1.48.9 → 1.49.1
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/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +16 -11
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +12 -4
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +70 -61
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +25 -21
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +20 -13
- package/dist/cjs/data-structures/binary-tree/bst.js +12 -3
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +19 -25
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +21 -35
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +20 -13
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +12 -3
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +9 -3
- package/dist/cjs/data-structures/graph/abstract-graph.js +27 -31
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +8 -1
- package/dist/cjs/data-structures/graph/directed-graph.js +1 -8
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +8 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js +1 -8
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +14 -2
- package/dist/cjs/data-structures/hash/hash-map.js +19 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +14 -3
- package/dist/cjs/data-structures/heap/heap.js +12 -0
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +11 -1
- package/dist/cjs/data-structures/heap/max-heap.js +10 -7
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +11 -1
- package/dist/cjs/data-structures/heap/min-heap.js +10 -7
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +8 -2
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +6 -7
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +0 -7
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +0 -7
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +0 -7
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +9 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -7
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +6 -5
- package/dist/cjs/data-structures/queue/deque.js +6 -12
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +18 -3
- package/dist/cjs/data-structures/queue/queue.js +16 -6
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +15 -5
- package/dist/cjs/data-structures/stack/stack.js +7 -4
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +13 -3
- package/dist/cjs/data-structures/trie/trie.js +11 -8
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/cjs/types/common.d.ts +32 -8
- package/dist/cjs/types/common.js +22 -1
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -22
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +16 -11
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +12 -4
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +70 -61
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +25 -21
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +20 -13
- package/dist/mjs/data-structures/binary-tree/bst.js +12 -3
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +19 -25
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +21 -34
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +20 -13
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +12 -3
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +9 -3
- package/dist/mjs/data-structures/graph/abstract-graph.js +27 -31
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +8 -1
- package/dist/mjs/data-structures/graph/directed-graph.js +1 -8
- package/dist/mjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +8 -1
- package/dist/mjs/data-structures/graph/undirected-graph.js +1 -8
- package/dist/mjs/data-structures/hash/hash-map.d.ts +14 -2
- package/dist/mjs/data-structures/hash/hash-map.js +18 -7
- package/dist/mjs/data-structures/hash/hash-table.d.ts +2 -2
- package/dist/mjs/data-structures/heap/heap.d.ts +14 -3
- package/dist/mjs/data-structures/heap/heap.js +13 -1
- package/dist/mjs/data-structures/heap/max-heap.d.ts +11 -1
- package/dist/mjs/data-structures/heap/max-heap.js +9 -6
- package/dist/mjs/data-structures/heap/min-heap.d.ts +11 -1
- package/dist/mjs/data-structures/heap/min-heap.js +9 -6
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +8 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +7 -8
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +0 -7
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +0 -7
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +0 -7
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +9 -1
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +7 -6
- package/dist/mjs/data-structures/queue/deque.d.ts +6 -5
- package/dist/mjs/data-structures/queue/deque.js +6 -12
- package/dist/mjs/data-structures/queue/queue.d.ts +18 -3
- package/dist/mjs/data-structures/queue/queue.js +16 -6
- package/dist/mjs/data-structures/stack/stack.d.ts +15 -5
- package/dist/mjs/data-structures/stack/stack.js +8 -5
- package/dist/mjs/data-structures/trie/trie.d.ts +13 -3
- package/dist/mjs/data-structures/trie/trie.js +12 -9
- package/dist/mjs/interfaces/binary-tree.d.ts +4 -4
- package/dist/mjs/types/common.d.ts +32 -8
- package/dist/mjs/types/common.js +21 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -21
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/umd/data-structure-typed.js +241 -216
- 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/avl-tree.ts +23 -13
- package/src/data-structures/binary-tree/binary-tree.ts +93 -97
- package/src/data-structures/binary-tree/bst.ts +26 -15
- package/src/data-structures/binary-tree/rb-tree.ts +33 -48
- package/src/data-structures/binary-tree/tree-multimap.ts +32 -14
- package/src/data-structures/graph/abstract-graph.ts +35 -25
- package/src/data-structures/graph/directed-graph.ts +2 -2
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +20 -3
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +14 -3
- package/src/data-structures/heap/max-heap.ts +11 -2
- package/src/data-structures/heap/min-heap.ts +11 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +9 -2
- package/src/data-structures/queue/deque.ts +7 -9
- package/src/data-structures/queue/queue.ts +18 -3
- package/src/data-structures/stack/stack.ts +16 -6
- package/src/data-structures/trie/trie.ts +13 -4
- package/src/interfaces/binary-tree.ts +5 -5
- package/src/types/common.ts +37 -12
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
- package/src/types/data-structures/binary-tree/bst.ts +0 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +21 -1
- package/test/unit/data-structures/graph/undirected-graph.test.ts +39 -1
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { BinaryTreeDeleteResult, BTNCallback, BTNExemplar, BTNKeyOrNode, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
@@ -24,7 +24,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
24
24
|
/**
|
|
25
25
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
26
26
|
* initializes the tree with optional elements and options.
|
|
27
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `
|
|
27
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
|
|
28
28
|
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
29
29
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
30
30
|
* elements to the
|
|
@@ -32,7 +32,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
32
32
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
33
33
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
34
34
|
*/
|
|
35
|
-
constructor(elements?: Iterable<
|
|
35
|
+
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>);
|
|
36
36
|
protected _root: N;
|
|
37
37
|
get root(): N;
|
|
38
38
|
protected _size: number;
|
|
@@ -60,20 +60,27 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
60
60
|
createTree(options?: RBTreeOptions<K>): TREE;
|
|
61
61
|
/**
|
|
62
62
|
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
63
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
63
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
64
64
|
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
65
65
|
* class.
|
|
66
66
|
*/
|
|
67
|
-
isNode(exemplar:
|
|
67
|
+
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N;
|
|
68
|
+
/**
|
|
69
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
70
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
71
|
+
* data type.
|
|
72
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
73
|
+
*/
|
|
74
|
+
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K;
|
|
68
75
|
/**
|
|
69
76
|
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
70
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
77
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
71
78
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
72
79
|
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
|
73
80
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
74
81
|
* @returns a node of type N or undefined.
|
|
75
82
|
*/
|
|
76
|
-
exemplarToNode(exemplar:
|
|
83
|
+
exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined;
|
|
77
84
|
/**
|
|
78
85
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
79
86
|
* Space Complexity: O(1)
|
|
@@ -90,7 +97,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
90
97
|
* being added to the binary search tree.
|
|
91
98
|
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
|
92
99
|
*/
|
|
93
|
-
add(keyOrNodeOrEntry:
|
|
100
|
+
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined;
|
|
94
101
|
/**
|
|
95
102
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
96
103
|
* Space Complexity: O(1)
|
|
@@ -108,9 +115,9 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
108
115
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` and
|
|
109
116
|
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
110
117
|
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
111
|
-
* @returns an array of `
|
|
118
|
+
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
|
112
119
|
*/
|
|
113
|
-
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C):
|
|
120
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeleteResult<N>[];
|
|
114
121
|
/**
|
|
115
122
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
116
123
|
* Space Complexity: O(1)
|
|
@@ -120,24 +127,11 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
120
127
|
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
121
128
|
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
122
129
|
/**
|
|
123
|
-
* Time Complexity: O(log n)
|
|
130
|
+
* Time Complexity: O(log n)
|
|
124
131
|
* Space Complexity: O(1)
|
|
125
132
|
*/
|
|
126
133
|
/**
|
|
127
|
-
* Time Complexity: O(log n)
|
|
128
|
-
* Space Complexity: O(1)
|
|
129
|
-
*
|
|
130
|
-
* The function returns the successor of a given node in a red-black tree.
|
|
131
|
-
* @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
|
|
132
|
-
* @returns the successor of the given RedBlackTreeNode.
|
|
133
|
-
*/
|
|
134
|
-
getSuccessor(x: N): N | undefined;
|
|
135
|
-
/**
|
|
136
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
137
|
-
* Space Complexity: O(1)
|
|
138
|
-
*/
|
|
139
|
-
/**
|
|
140
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
134
|
+
* Time Complexity: O(log n)
|
|
141
135
|
* Space Complexity: O(1)
|
|
142
136
|
*
|
|
143
137
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { RBTNColor } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
|
-
import { BinaryTreeNode } from './binary-tree';
|
|
11
10
|
export class RedBlackTreeNode extends BSTNode {
|
|
12
11
|
color;
|
|
13
12
|
constructor(key, value, color = RBTNColor.BLACK) {
|
|
@@ -27,7 +26,7 @@ export class RedBlackTree extends BST {
|
|
|
27
26
|
/**
|
|
28
27
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
29
28
|
* initializes the tree with optional elements and options.
|
|
30
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `
|
|
29
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
|
|
31
30
|
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
32
31
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
33
32
|
* elements to the
|
|
@@ -79,16 +78,25 @@ export class RedBlackTree extends BST {
|
|
|
79
78
|
}
|
|
80
79
|
/**
|
|
81
80
|
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
82
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
81
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
83
82
|
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
84
83
|
* class.
|
|
85
84
|
*/
|
|
86
85
|
isNode(exemplar) {
|
|
87
86
|
return exemplar instanceof RedBlackTreeNode;
|
|
88
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
90
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
91
|
+
* data type.
|
|
92
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
93
|
+
*/
|
|
94
|
+
isNotNodeInstance(potentialKey) {
|
|
95
|
+
return !(potentialKey instanceof RedBlackTreeNode);
|
|
96
|
+
}
|
|
89
97
|
/**
|
|
90
98
|
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
91
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
99
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
92
100
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
93
101
|
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
|
94
102
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
@@ -199,7 +207,7 @@ export class RedBlackTree extends BST {
|
|
|
199
207
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` and
|
|
200
208
|
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
201
209
|
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
202
|
-
* @returns an array of `
|
|
210
|
+
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
|
203
211
|
*/
|
|
204
212
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
205
213
|
const ans = [];
|
|
@@ -264,7 +272,9 @@ export class RedBlackTree extends BST {
|
|
|
264
272
|
* Space Complexity: O(1)
|
|
265
273
|
*/
|
|
266
274
|
isRealNode(node) {
|
|
267
|
-
|
|
275
|
+
if (node === this.Sentinel || node === undefined)
|
|
276
|
+
return false;
|
|
277
|
+
return node instanceof RedBlackTreeNode;
|
|
268
278
|
}
|
|
269
279
|
/**
|
|
270
280
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
@@ -292,40 +302,17 @@ export class RedBlackTree extends BST {
|
|
|
292
302
|
* @returns a value of type `N`, `null`, or `undefined`.
|
|
293
303
|
*/
|
|
294
304
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
295
|
-
if (identifier instanceof
|
|
305
|
+
if (identifier instanceof RedBlackTreeNode)
|
|
296
306
|
callback = (node => node);
|
|
297
307
|
beginRoot = this.ensureNode(beginRoot);
|
|
298
308
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
299
309
|
}
|
|
300
310
|
/**
|
|
301
|
-
* Time Complexity: O(log n)
|
|
311
|
+
* Time Complexity: O(log n)
|
|
302
312
|
* Space Complexity: O(1)
|
|
303
313
|
*/
|
|
304
314
|
/**
|
|
305
|
-
* Time Complexity: O(log n)
|
|
306
|
-
* Space Complexity: O(1)
|
|
307
|
-
*
|
|
308
|
-
* The function returns the successor of a given node in a red-black tree.
|
|
309
|
-
* @param {RedBlackTreeNode} x - RedBlackTreeNode - The node for which we want to find the successor.
|
|
310
|
-
* @returns the successor of the given RedBlackTreeNode.
|
|
311
|
-
*/
|
|
312
|
-
getSuccessor(x) {
|
|
313
|
-
if (x.right !== this.Sentinel) {
|
|
314
|
-
return this.getLeftMost(x.right) ?? undefined;
|
|
315
|
-
}
|
|
316
|
-
let y = x.parent;
|
|
317
|
-
while (y !== this.Sentinel && y !== undefined && x === y.right) {
|
|
318
|
-
x = y;
|
|
319
|
-
y = y.parent;
|
|
320
|
-
}
|
|
321
|
-
return y;
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
325
|
-
* Space Complexity: O(1)
|
|
326
|
-
*/
|
|
327
|
-
/**
|
|
328
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
315
|
+
* Time Complexity: O(log n)
|
|
329
316
|
* Space Complexity: O(1)
|
|
330
317
|
*
|
|
331
318
|
* The function returns the predecessor of a given node in a red-black tree.
|
|
@@ -334,11 +321,11 @@ export class RedBlackTree extends BST {
|
|
|
334
321
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
335
322
|
*/
|
|
336
323
|
getPredecessor(x) {
|
|
337
|
-
if (x.left
|
|
324
|
+
if (this.isRealNode(x.left)) {
|
|
338
325
|
return this.getRightMost(x.left);
|
|
339
326
|
}
|
|
340
327
|
let y = x.parent;
|
|
341
|
-
while (
|
|
328
|
+
while (this.isRealNode(y) && x === y.left) {
|
|
342
329
|
x = y;
|
|
343
330
|
y = y.parent;
|
|
344
331
|
}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
import {
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, BTNExemplar, BTNKeyOrNode, TreeMultimapNested, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
|
+
import { IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNodeNested<K, V>> extends AVLTreeNode<K, V, N> {
|
|
@@ -27,7 +27,7 @@ export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNo
|
|
|
27
27
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
28
28
|
*/
|
|
29
29
|
export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
30
|
-
constructor(elements?: Iterable<
|
|
30
|
+
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>);
|
|
31
31
|
private _count;
|
|
32
32
|
get count(): number;
|
|
33
33
|
/**
|
|
@@ -43,14 +43,21 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
43
43
|
createTree(options?: TreeMultimapOptions<K>): TREE;
|
|
44
44
|
/**
|
|
45
45
|
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
46
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
46
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
47
47
|
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
48
48
|
* class.
|
|
49
49
|
*/
|
|
50
|
-
isNode(exemplar:
|
|
50
|
+
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N;
|
|
51
|
+
/**
|
|
52
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
53
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
54
|
+
* data type.
|
|
55
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
56
|
+
*/
|
|
57
|
+
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K;
|
|
51
58
|
/**
|
|
52
59
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
53
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
60
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
54
61
|
* can be one of the following:
|
|
55
62
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
56
63
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -59,7 +66,7 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
59
66
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
60
67
|
* @returns a node of type `N` or `undefined`.
|
|
61
68
|
*/
|
|
62
|
-
exemplarToNode(exemplar:
|
|
69
|
+
exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V, count?: number): N | undefined;
|
|
63
70
|
/**
|
|
64
71
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
65
72
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -80,7 +87,7 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
80
87
|
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
|
|
81
88
|
* was not successful.
|
|
82
89
|
*/
|
|
83
|
-
add(keyOrNodeOrEntry:
|
|
90
|
+
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count?: number): N | undefined;
|
|
84
91
|
/**
|
|
85
92
|
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
86
93
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -95,7 +102,7 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
95
102
|
* either keys, nodes, or entries.
|
|
96
103
|
* @returns The method is returning an array of type `N | undefined`.
|
|
97
104
|
*/
|
|
98
|
-
addMany(keysOrNodesOrEntries: Iterable<
|
|
105
|
+
addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>): (N | undefined)[];
|
|
99
106
|
/**
|
|
100
107
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
101
108
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -133,9 +140,9 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
133
140
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
134
141
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
135
142
|
* decremented by 1 and
|
|
136
|
-
* @returns an array of `
|
|
143
|
+
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
|
137
144
|
*/
|
|
138
|
-
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean):
|
|
145
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<N>[];
|
|
139
146
|
/**
|
|
140
147
|
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
141
148
|
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
@@ -171,7 +178,7 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
171
178
|
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
172
179
|
* added, or `undefined` if no node was added.
|
|
173
180
|
*/
|
|
174
|
-
protected _addTo(newNode: N | undefined, parent:
|
|
181
|
+
protected _addTo(newNode: N | undefined, parent: BSTNKeyOrNode<K, N>): N | undefined;
|
|
175
182
|
/**
|
|
176
183
|
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
177
184
|
* @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
@@ -181,6 +188,6 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
181
188
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
182
189
|
* if either `srcNode` or `destNode` is undefined.
|
|
183
190
|
*/
|
|
184
|
-
protected _swapProperties(srcNode:
|
|
191
|
+
protected _swapProperties(srcNode: BSTNKeyOrNode<K, N>, destNode: BSTNKeyOrNode<K, N>): N | undefined;
|
|
185
192
|
protected _replaceNode(oldNode: N, newNode: N): N;
|
|
186
193
|
}
|
|
@@ -53,16 +53,25 @@ export class TreeMultimap extends AVLTree {
|
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
|
56
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
56
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
57
57
|
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
58
58
|
* class.
|
|
59
59
|
*/
|
|
60
60
|
isNode(exemplar) {
|
|
61
61
|
return exemplar instanceof TreeMultimapNode;
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
65
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
66
|
+
* data type.
|
|
67
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
68
|
+
*/
|
|
69
|
+
isNotNodeInstance(potentialKey) {
|
|
70
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
71
|
+
}
|
|
63
72
|
/**
|
|
64
73
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
65
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
74
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
66
75
|
* can be one of the following:
|
|
67
76
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
68
77
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -216,7 +225,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
216
225
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
217
226
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
218
227
|
* decremented by 1 and
|
|
219
|
-
* @returns an array of `
|
|
228
|
+
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
|
220
229
|
*/
|
|
221
230
|
delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
|
|
222
231
|
const deletedResult = [];
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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 { DijkstraResult, EntryCallback, VertexKey } from '../../types';
|
|
4
9
|
import { IterableEntryBase } from "../base";
|
|
10
|
+
import { IGraph } from '../../interfaces';
|
|
5
11
|
export declare abstract class AbstractVertex<V = any> {
|
|
6
12
|
key: VertexKey;
|
|
7
13
|
value: V | undefined;
|
|
@@ -1,14 +1,7 @@
|
|
|
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
1
|
import { uuidV4 } from '../../utils';
|
|
9
|
-
import { PriorityQueue } from '../priority-queue';
|
|
10
|
-
import { Queue } from '../queue';
|
|
11
2
|
import { IterableEntryBase } from "../base";
|
|
3
|
+
import { Heap } from '../heap';
|
|
4
|
+
import { Queue } from '../queue';
|
|
12
5
|
export class AbstractVertex {
|
|
13
6
|
key;
|
|
14
7
|
value;
|
|
@@ -438,13 +431,7 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
438
431
|
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
439
432
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
440
433
|
*/
|
|
441
|
-
dijkstraWithoutHeap(src, dest, getMinDist, genPaths) {
|
|
442
|
-
if (getMinDist === undefined)
|
|
443
|
-
getMinDist = false;
|
|
444
|
-
if (genPaths === undefined)
|
|
445
|
-
genPaths = false;
|
|
446
|
-
if (dest === undefined)
|
|
447
|
-
dest = undefined;
|
|
434
|
+
dijkstraWithoutHeap(src, dest = undefined, getMinDist = false, genPaths = false) {
|
|
448
435
|
let minDist = Infinity;
|
|
449
436
|
let minDest = undefined;
|
|
450
437
|
let minPath = [];
|
|
@@ -573,13 +560,7 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
573
560
|
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
574
561
|
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
575
562
|
*/
|
|
576
|
-
dijkstra(src, dest, getMinDist, genPaths) {
|
|
577
|
-
if (getMinDist === undefined)
|
|
578
|
-
getMinDist = false;
|
|
579
|
-
if (genPaths === undefined)
|
|
580
|
-
genPaths = false;
|
|
581
|
-
if (dest === undefined)
|
|
582
|
-
dest = undefined;
|
|
563
|
+
dijkstra(src, dest = undefined, getMinDist = false, genPaths = false) {
|
|
583
564
|
let minDist = Infinity;
|
|
584
565
|
let minDest = undefined;
|
|
585
566
|
let minPath = [];
|
|
@@ -597,7 +578,7 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
597
578
|
if (vertexOrKey instanceof AbstractVertex)
|
|
598
579
|
distMap.set(vertexOrKey, Infinity);
|
|
599
580
|
}
|
|
600
|
-
const heap = new
|
|
581
|
+
const heap = new Heap([], { comparator: (a, b) => a.key - b.key });
|
|
601
582
|
heap.add({ key: 0, value: srcVertex });
|
|
602
583
|
distMap.set(srcVertex, 0);
|
|
603
584
|
preMap.set(srcVertex, undefined);
|
|
@@ -964,13 +945,28 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
964
945
|
}
|
|
965
946
|
const cycles = new Map();
|
|
966
947
|
if (needCycles) {
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
948
|
+
const visitedMap = new Map();
|
|
949
|
+
const stack = [];
|
|
950
|
+
const findCyclesDFS = (cur, parent) => {
|
|
951
|
+
visitedMap.set(cur, true);
|
|
952
|
+
stack.push(cur);
|
|
953
|
+
const neighbors = this.getNeighbors(cur);
|
|
954
|
+
for (const neighbor of neighbors) {
|
|
955
|
+
if (!visitedMap.get(neighbor)) {
|
|
956
|
+
findCyclesDFS(neighbor, cur);
|
|
957
|
+
}
|
|
958
|
+
else if (stack.includes(neighbor) && neighbor !== parent) {
|
|
959
|
+
const cycleStartIndex = stack.indexOf(neighbor);
|
|
960
|
+
const cycle = stack.slice(cycleStartIndex);
|
|
961
|
+
const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity));
|
|
962
|
+
cycles.set(cycleLow, cycle);
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
stack.pop();
|
|
966
|
+
};
|
|
967
|
+
vertexMap.forEach(v => {
|
|
968
|
+
if (!visitedMap.get(v)) {
|
|
969
|
+
findCyclesDFS(v, undefined);
|
|
974
970
|
}
|
|
975
971
|
});
|
|
976
972
|
}
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
|
|
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
|
+
*/
|
|
2
8
|
import type { VertexKey } from '../../types';
|
|
9
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
3
10
|
import { IGraph } from '../../interfaces';
|
|
4
11
|
export declare class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
5
12
|
/**
|
|
@@ -1,12 +1,5 @@
|
|
|
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 { arrayRemove } from '../../utils';
|
|
9
1
|
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
2
|
+
import { arrayRemove } from '../../utils';
|
|
10
3
|
export class DirectedVertex extends AbstractVertex {
|
|
11
4
|
/**
|
|
12
5
|
* The constructor function initializes a vertex with an optional value.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MapGraphCoordinate, VertexKey } from '../../types';
|
|
1
|
+
import type { MapGraphCoordinate, VertexKey } from '../../types';
|
|
2
2
|
import { DirectedEdge, DirectedGraph, DirectedVertex } from './directed-graph';
|
|
3
3
|
export declare class MapVertex<V = any> extends DirectedVertex<V> {
|
|
4
4
|
lat: number;
|
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
|
|
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
|
+
*/
|
|
2
8
|
import type { VertexKey } from '../../types';
|
|
3
9
|
import { IGraph } from '../../interfaces';
|
|
10
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
4
11
|
export declare class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
5
12
|
/**
|
|
6
13
|
* The constructor function initializes a vertex with an optional value.
|
|
@@ -1,12 +1,5 @@
|
|
|
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 { arrayRemove } from '../../utils';
|
|
9
1
|
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
2
|
+
import { arrayRemove } from '../../utils';
|
|
10
3
|
export class UndirectedVertex extends AbstractVertex {
|
|
11
4
|
/**
|
|
12
5
|
* The constructor function initializes a vertex with an optional value.
|
|
@@ -5,8 +5,14 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
9
|
-
import { IterableEntryBase } from
|
|
8
|
+
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
|
9
|
+
import { IterableEntryBase } from '../base';
|
|
10
|
+
/**
|
|
11
|
+
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
12
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
|
|
13
|
+
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
14
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
|
|
15
|
+
*/
|
|
10
16
|
export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
11
17
|
protected _store: {
|
|
12
18
|
[key: string]: HashMapStoreItem<K, V>;
|
|
@@ -117,6 +123,11 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
117
123
|
protected _isObjKey(key: any): key is (object | ((...args: any[]) => any));
|
|
118
124
|
protected _getNoObjKey(key: K): string;
|
|
119
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which elements are inserted. Therefore, when you traverse it, elements will be returned in the order they were inserted into the map.
|
|
128
|
+
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of elements through the linked list.
|
|
129
|
+
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
130
|
+
*/
|
|
120
131
|
export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
121
132
|
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>;
|
|
122
133
|
protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>;
|
|
@@ -169,6 +180,7 @@ export declare class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K
|
|
|
169
180
|
*/
|
|
170
181
|
set(key: K, value?: V): number;
|
|
171
182
|
has(key: K): boolean;
|
|
183
|
+
hasValue(value: V): boolean;
|
|
172
184
|
setMany(entries: Iterable<[K, V]>): void;
|
|
173
185
|
/**
|
|
174
186
|
* Time Complexity: O(1)
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
import { IterableEntryBase } from '../base';
|
|
2
|
+
import { isWeakKey, rangeCheck } from '../../utils';
|
|
1
3
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @license MIT License
|
|
4
|
+
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
|
5
|
+
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
|
|
6
|
+
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
7
|
+
* 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
|
|
7
8
|
*/
|
|
8
|
-
import { isWeakKey, rangeCheck } from '../../utils';
|
|
9
|
-
import { IterableEntryBase } from "../base";
|
|
10
9
|
export class HashMap extends IterableEntryBase {
|
|
11
10
|
_store = {};
|
|
12
11
|
_objMap = new Map();
|
|
@@ -225,6 +224,11 @@ export class HashMap extends IterableEntryBase {
|
|
|
225
224
|
return strKey;
|
|
226
225
|
}
|
|
227
226
|
}
|
|
227
|
+
/**
|
|
228
|
+
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which elements are inserted. Therefore, when you traverse it, elements will be returned in the order they were inserted into the map.
|
|
229
|
+
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of elements through the linked list.
|
|
230
|
+
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
|
231
|
+
*/
|
|
228
232
|
export class LinkedHashMap extends IterableEntryBase {
|
|
229
233
|
_noObjMap = {};
|
|
230
234
|
_objMap = new WeakMap();
|
|
@@ -365,6 +369,13 @@ export class LinkedHashMap extends IterableEntryBase {
|
|
|
365
369
|
return hash in this._noObjMap;
|
|
366
370
|
}
|
|
367
371
|
}
|
|
372
|
+
hasValue(value) {
|
|
373
|
+
for (const [, elementValue] of this) {
|
|
374
|
+
if (elementValue === value)
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
368
379
|
setMany(entries) {
|
|
369
380
|
for (const entry of entries) {
|
|
370
381
|
const [key, value] = entry;
|
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
import type { HashFunction } from '../../types';
|
|
9
|
+
export declare class HashTableNode<K = any, V = any> {
|
|
9
10
|
key: K;
|
|
10
11
|
value: V;
|
|
11
12
|
next: HashTableNode<K, V> | undefined;
|
|
12
13
|
constructor(key: K, value: V);
|
|
13
14
|
}
|
|
14
|
-
import { HashFunction } from '../../types';
|
|
15
15
|
export declare class HashTable<K = any, V = any> {
|
|
16
16
|
protected static readonly DEFAULT_CAPACITY = 16;
|
|
17
17
|
protected static readonly LOAD_FACTOR = 0.75;
|