directed-graph-typed 1.48.0 → 1.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +28 -19
- package/dist/data-structures/binary-tree/avl-tree.js +22 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -152
- package/dist/data-structures/binary-tree/binary-tree.js +241 -215
- package/dist/data-structures/binary-tree/bst.d.ts +64 -48
- package/dist/data-structures/binary-tree/bst.js +94 -65
- package/dist/data-structures/binary-tree/rb-tree.d.ts +39 -39
- package/dist/data-structures/binary-tree/rb-tree.js +42 -49
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +60 -34
- package/dist/data-structures/binary-tree/tree-multimap.js +59 -27
- package/dist/data-structures/graph/abstract-graph.d.ts +92 -53
- package/dist/data-structures/graph/abstract-graph.js +130 -103
- package/dist/data-structures/graph/directed-graph.d.ts +70 -52
- package/dist/data-structures/graph/directed-graph.js +111 -65
- package/dist/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/data-structures/graph/map-graph.js +8 -8
- package/dist/data-structures/graph/undirected-graph.d.ts +51 -32
- package/dist/data-structures/graph/undirected-graph.js +117 -54
- package/dist/data-structures/hash/hash-map.d.ts +160 -44
- package/dist/data-structures/hash/hash-map.js +314 -82
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +42 -55
- package/dist/data-structures/linked-list/doubly-linked-list.js +50 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +36 -55
- package/dist/data-structures/linked-list/singly-linked-list.js +44 -77
- package/dist/data-structures/queue/deque.d.ts +35 -167
- package/dist/data-structures/queue/deque.js +43 -249
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +11 -8
- package/dist/types/common.js +6 -1
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -0
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/avl-tree.ts +37 -25
- package/src/data-structures/binary-tree/binary-tree.ts +336 -296
- package/src/data-structures/binary-tree/bst.ts +135 -89
- package/src/data-structures/binary-tree/rb-tree.ts +60 -69
- package/src/data-structures/binary-tree/tree-multimap.ts +86 -49
- package/src/data-structures/graph/abstract-graph.ts +136 -104
- package/src/data-structures/graph/directed-graph.ts +114 -65
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +124 -56
- package/src/data-structures/hash/hash-map.ts +335 -84
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +54 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -84
- package/src/data-structures/queue/deque.ts +43 -275
- package/src/data-structures/queue/queue.ts +71 -86
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/interfaces/binary-tree.ts +5 -6
- package/src/types/common.ts +11 -8
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
- package/src/types/data-structures/hash/hash-map.ts +2 -0
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/index.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BSTNested, BSTNodeKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback,
|
|
9
|
-
import { CP, IterationType } from '../../types';
|
|
8
|
+
import type { BSTNested, BSTNodeKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNodeExemplar } from '../../types';
|
|
9
|
+
import { BSTVariant, BTNodeKeyOrNode, CP, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
|
-
export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
12
|
+
export declare class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, N> {
|
|
13
13
|
parent?: N;
|
|
14
|
-
constructor(key:
|
|
14
|
+
constructor(key: K, value?: V);
|
|
15
15
|
protected _left?: N;
|
|
16
16
|
/**
|
|
17
17
|
* Get the left child node.
|
|
@@ -42,7 +42,7 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
|
|
|
42
42
|
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
43
43
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
44
44
|
*/
|
|
45
|
-
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>, TREE extends BST<V, N, TREE> = BST<V, N, BSTNested<V, N>>> extends BinaryTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
45
|
+
export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>> extends BinaryTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
46
46
|
/**
|
|
47
47
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
48
48
|
* the tree with optional elements and options.
|
|
@@ -51,19 +51,20 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
51
51
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
52
52
|
* configuration options for the binary search tree. It can have the following properties:
|
|
53
53
|
*/
|
|
54
|
-
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<BSTOptions
|
|
54
|
+
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>);
|
|
55
55
|
protected _root?: N;
|
|
56
56
|
get root(): N | undefined;
|
|
57
|
-
|
|
57
|
+
protected _variant: BSTVariant;
|
|
58
|
+
get variant(): BSTVariant;
|
|
58
59
|
/**
|
|
59
60
|
* The function creates a new binary search tree node with the given key and value.
|
|
60
|
-
* @param {
|
|
61
|
+
* @param {K} key - The key parameter is the key value that will be associated with
|
|
61
62
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
62
63
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
|
|
63
64
|
* represents the value associated with the node in a binary search tree.
|
|
64
65
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
65
66
|
*/
|
|
66
|
-
createNode(key:
|
|
67
|
+
createNode(key: K, value?: V): N;
|
|
67
68
|
/**
|
|
68
69
|
* The function creates a new binary search tree with the specified options.
|
|
69
70
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
@@ -71,20 +72,22 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
71
72
|
* that defines various options for creating a binary search tree.
|
|
72
73
|
* @returns a new instance of the BST class with the specified options.
|
|
73
74
|
*/
|
|
74
|
-
createTree(options?: Partial<BSTOptions
|
|
75
|
+
createTree(options?: Partial<BSTOptions<K>>): TREE;
|
|
75
76
|
/**
|
|
76
77
|
* The function checks if an exemplar is an instance of BSTNode.
|
|
77
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
78
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
|
|
78
79
|
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
79
80
|
*/
|
|
80
|
-
isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
|
|
81
|
+
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
81
82
|
/**
|
|
82
|
-
* The function `exemplarToNode` takes an exemplar and returns a
|
|
83
|
-
*
|
|
84
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N
|
|
85
|
-
* @
|
|
83
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
84
|
+
* otherwise it returns undefined.
|
|
85
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
86
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
87
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node.
|
|
88
|
+
* @returns a node of type N or undefined.
|
|
86
89
|
*/
|
|
87
|
-
exemplarToNode(exemplar: BTNodeExemplar<V, N
|
|
90
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
|
|
88
91
|
/**
|
|
89
92
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
90
93
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -93,13 +96,15 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
93
96
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
94
97
|
* Space Complexity: O(1) - Constant space is used.
|
|
95
98
|
*
|
|
96
|
-
* The `add` function adds a new node to a binary
|
|
97
|
-
*
|
|
98
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can
|
|
99
|
-
* @
|
|
100
|
-
*
|
|
99
|
+
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
100
|
+
* or inserting a new node if the key is unique.
|
|
101
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
|
|
102
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
103
|
+
* being added to the binary tree.
|
|
104
|
+
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
105
|
+
* node was not added.
|
|
101
106
|
*/
|
|
102
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<V, N
|
|
107
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
|
|
103
108
|
/**
|
|
104
109
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
105
110
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
@@ -108,19 +113,23 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
108
113
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
109
114
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
110
115
|
*
|
|
111
|
-
* The `addMany` function in TypeScript adds multiple nodes to a binary tree,
|
|
112
|
-
*
|
|
113
|
-
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to
|
|
114
|
-
* binary tree.
|
|
115
|
-
* @param [
|
|
116
|
-
*
|
|
116
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
117
|
+
* balancing the tree after each addition.
|
|
118
|
+
* @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
|
|
119
|
+
* the binary tree.
|
|
120
|
+
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
121
|
+
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
122
|
+
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
123
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
124
|
+
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
125
|
+
* algorithm. If set to false, the add operation will not be balanced and the elements will be added
|
|
126
|
+
* in the order they appear in the input.
|
|
117
127
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
118
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
128
|
+
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
129
|
+
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
130
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
122
131
|
*/
|
|
123
|
-
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
132
|
+
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
124
133
|
/**
|
|
125
134
|
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
126
135
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
@@ -131,8 +140,8 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
131
140
|
*
|
|
132
141
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
133
142
|
* leftmost node if the comparison result is greater than.
|
|
134
|
-
* @param {
|
|
135
|
-
* type `
|
|
143
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
144
|
+
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
136
145
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
137
146
|
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
138
147
|
* be performed. It can have one of the following values:
|
|
@@ -140,7 +149,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
140
149
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
141
150
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
142
151
|
*/
|
|
143
|
-
lastKey(beginRoot?: BSTNodeKeyOrNode<N
|
|
152
|
+
lastKey(beginRoot?: BSTNodeKeyOrNode<K, N>): K | undefined;
|
|
144
153
|
/**
|
|
145
154
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
146
155
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -151,7 +160,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
151
160
|
*
|
|
152
161
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
153
162
|
* either recursive or iterative methods.
|
|
154
|
-
* @param {
|
|
163
|
+
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
155
164
|
* It is used to identify the node that we want to retrieve.
|
|
156
165
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
157
166
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
@@ -159,7 +168,14 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
159
168
|
* @returns The function `getNodeByKey` returns a node (`N`) if a node with the specified key is
|
|
160
169
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
161
170
|
*/
|
|
162
|
-
getNodeByKey(key:
|
|
171
|
+
getNodeByKey(key: K, iterationType?: IterationType): N | undefined;
|
|
172
|
+
/**
|
|
173
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
174
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
175
|
+
* data type.
|
|
176
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
177
|
+
*/
|
|
178
|
+
isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K;
|
|
163
179
|
/**
|
|
164
180
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
165
181
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -167,13 +183,13 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
167
183
|
/**
|
|
168
184
|
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
169
185
|
* otherwise it returns the key itself.
|
|
170
|
-
* @param {
|
|
186
|
+
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
171
187
|
* `undefined`.
|
|
172
188
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
173
189
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
174
190
|
* @returns either a node object (N) or undefined.
|
|
175
191
|
*/
|
|
176
|
-
ensureNode(key: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N | undefined;
|
|
192
|
+
ensureNode(key: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): N | undefined;
|
|
177
193
|
/**
|
|
178
194
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
179
195
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -190,14 +206,14 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
190
206
|
* first node that matches the identifier. If set to true, the function will return an array
|
|
191
207
|
* containing only the first matching node. If set to false (default), the function will continue
|
|
192
208
|
* searching for all nodes that match the identifier and return an array containing
|
|
193
|
-
* @param {
|
|
209
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
194
210
|
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
195
211
|
* traversal will start from the root of the tree.
|
|
196
212
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
197
213
|
* performed on the binary tree. It can have two possible values:
|
|
198
214
|
* @returns The method returns an array of nodes (`N[]`).
|
|
199
215
|
*/
|
|
200
|
-
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N[];
|
|
216
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): N[];
|
|
201
217
|
/**
|
|
202
218
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
203
219
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -215,7 +231,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
215
231
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
216
232
|
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
217
233
|
* `lesserOrGreater` are
|
|
218
|
-
* @param {
|
|
234
|
+
* @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
219
235
|
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
220
236
|
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
221
237
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -223,7 +239,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
223
239
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
224
240
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
225
241
|
*/
|
|
226
|
-
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): ReturnType<C>[];
|
|
242
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): ReturnType<C>[];
|
|
227
243
|
/**
|
|
228
244
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
229
245
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -267,10 +283,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
267
283
|
/**
|
|
268
284
|
* The function compares two values using a comparator function and returns whether the first value
|
|
269
285
|
* is greater than, less than, or equal to the second value.
|
|
270
|
-
* @param {
|
|
271
|
-
* @param {
|
|
286
|
+
* @param {K} a - The parameter "a" is of type K.
|
|
287
|
+
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
272
288
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
273
289
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
274
290
|
*/
|
|
275
|
-
protected _compare(a:
|
|
291
|
+
protected _compare(a: K, b: K): CP;
|
|
276
292
|
}
|
|
@@ -65,11 +65,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
65
65
|
*/
|
|
66
66
|
constructor(elements, options) {
|
|
67
67
|
super([], options);
|
|
68
|
-
this.
|
|
68
|
+
this._variant = types_1.BSTVariant.MIN;
|
|
69
69
|
if (options) {
|
|
70
|
-
const {
|
|
71
|
-
if (
|
|
72
|
-
this.
|
|
70
|
+
const { variant } = options;
|
|
71
|
+
if (variant) {
|
|
72
|
+
this._variant = variant;
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
this._root = undefined;
|
|
@@ -79,9 +79,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
79
79
|
get root() {
|
|
80
80
|
return this._root;
|
|
81
81
|
}
|
|
82
|
+
get variant() {
|
|
83
|
+
return this._variant;
|
|
84
|
+
}
|
|
82
85
|
/**
|
|
83
86
|
* The function creates a new binary search tree node with the given key and value.
|
|
84
|
-
* @param {
|
|
87
|
+
* @param {K} key - The key parameter is the key value that will be associated with
|
|
85
88
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
86
89
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
|
|
87
90
|
* represents the value associated with the node in a binary search tree.
|
|
@@ -98,23 +101,25 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
98
101
|
* @returns a new instance of the BST class with the specified options.
|
|
99
102
|
*/
|
|
100
103
|
createTree(options) {
|
|
101
|
-
return new BST([], Object.assign({ iterationType: this.iterationType,
|
|
104
|
+
return new BST([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
102
105
|
}
|
|
103
106
|
/**
|
|
104
107
|
* The function checks if an exemplar is an instance of BSTNode.
|
|
105
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
108
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
|
|
106
109
|
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
107
110
|
*/
|
|
108
111
|
isNode(exemplar) {
|
|
109
112
|
return exemplar instanceof BSTNode;
|
|
110
113
|
}
|
|
111
114
|
/**
|
|
112
|
-
* The function `exemplarToNode` takes an exemplar and returns a
|
|
113
|
-
*
|
|
114
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N
|
|
115
|
-
* @
|
|
115
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
116
|
+
* otherwise it returns undefined.
|
|
117
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
118
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
119
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node.
|
|
120
|
+
* @returns a node of type N or undefined.
|
|
116
121
|
*/
|
|
117
|
-
exemplarToNode(exemplar) {
|
|
122
|
+
exemplarToNode(exemplar, value) {
|
|
118
123
|
let node;
|
|
119
124
|
if (exemplar === null || exemplar === undefined) {
|
|
120
125
|
return;
|
|
@@ -131,8 +136,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
131
136
|
node = this.createNode(key, value);
|
|
132
137
|
}
|
|
133
138
|
}
|
|
134
|
-
else if (this.
|
|
135
|
-
node = this.createNode(exemplar);
|
|
139
|
+
else if (this.isNotNodeInstance(exemplar)) {
|
|
140
|
+
node = this.createNode(exemplar, value);
|
|
136
141
|
}
|
|
137
142
|
else {
|
|
138
143
|
return;
|
|
@@ -147,14 +152,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
147
152
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
148
153
|
* Space Complexity: O(1) - Constant space is used.
|
|
149
154
|
*
|
|
150
|
-
* The `add` function adds a new node to a binary
|
|
151
|
-
*
|
|
152
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can
|
|
153
|
-
* @
|
|
154
|
-
*
|
|
155
|
+
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
156
|
+
* or inserting a new node if the key is unique.
|
|
157
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
|
|
158
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
159
|
+
* being added to the binary tree.
|
|
160
|
+
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
161
|
+
* node was not added.
|
|
155
162
|
*/
|
|
156
|
-
add(keyOrNodeOrEntry) {
|
|
157
|
-
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
163
|
+
add(keyOrNodeOrEntry, value) {
|
|
164
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
158
165
|
if (newNode === undefined)
|
|
159
166
|
return;
|
|
160
167
|
if (this.root === undefined) {
|
|
@@ -204,23 +211,32 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
204
211
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
205
212
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
206
213
|
*
|
|
207
|
-
* The `addMany` function in TypeScript adds multiple nodes to a binary tree,
|
|
208
|
-
*
|
|
209
|
-
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to
|
|
210
|
-
* binary tree.
|
|
211
|
-
* @param [
|
|
212
|
-
*
|
|
214
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
215
|
+
* balancing the tree after each addition.
|
|
216
|
+
* @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
|
|
217
|
+
* the binary tree.
|
|
218
|
+
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
219
|
+
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
220
|
+
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
221
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
222
|
+
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
223
|
+
* algorithm. If set to false, the add operation will not be balanced and the elements will be added
|
|
224
|
+
* in the order they appear in the input.
|
|
213
225
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
214
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
226
|
+
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
227
|
+
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
228
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
218
229
|
*/
|
|
219
|
-
addMany(keysOrNodesOrEntries, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
230
|
+
addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
220
231
|
const inserted = [];
|
|
232
|
+
let valuesIterator;
|
|
233
|
+
if (values) {
|
|
234
|
+
valuesIterator = values[Symbol.iterator]();
|
|
235
|
+
}
|
|
221
236
|
if (!isBalanceAdd) {
|
|
222
237
|
for (const kve of keysOrNodesOrEntries) {
|
|
223
|
-
const
|
|
238
|
+
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
239
|
+
const nn = this.add(kve, value);
|
|
224
240
|
inserted.push(nn);
|
|
225
241
|
}
|
|
226
242
|
return inserted;
|
|
@@ -234,22 +250,21 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
234
250
|
for (const kve of keysOrNodesOrEntries) {
|
|
235
251
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
236
252
|
}
|
|
237
|
-
// TODO this addMany function is inefficient, it should be optimized
|
|
238
253
|
let sorted = [];
|
|
239
254
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
240
255
|
let aR, bR;
|
|
241
256
|
if (this.isEntry(a))
|
|
242
|
-
aR = a[0];
|
|
257
|
+
aR = this.extractor(a[0]);
|
|
243
258
|
else if (this.isRealNode(a))
|
|
244
|
-
aR = a.key;
|
|
259
|
+
aR = this.extractor(a.key);
|
|
245
260
|
else
|
|
246
|
-
aR = a;
|
|
261
|
+
aR = this.extractor(a);
|
|
247
262
|
if (this.isEntry(b))
|
|
248
|
-
bR = b[0];
|
|
263
|
+
bR = this.extractor(b[0]);
|
|
249
264
|
else if (this.isRealNode(b))
|
|
250
|
-
bR = b.key;
|
|
265
|
+
bR = this.extractor(b.key);
|
|
251
266
|
else
|
|
252
|
-
bR = b;
|
|
267
|
+
bR = this.extractor(b);
|
|
253
268
|
return aR - bR;
|
|
254
269
|
});
|
|
255
270
|
const _dfs = (arr) => {
|
|
@@ -296,8 +311,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
296
311
|
*
|
|
297
312
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
298
313
|
* leftmost node if the comparison result is greater than.
|
|
299
|
-
* @param {
|
|
300
|
-
* type `
|
|
314
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
315
|
+
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
301
316
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
302
317
|
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
303
318
|
* be performed. It can have one of the following values:
|
|
@@ -305,14 +320,23 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
305
320
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
306
321
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
307
322
|
*/
|
|
308
|
-
lastKey(beginRoot = this.root
|
|
309
|
-
|
|
310
|
-
if (
|
|
311
|
-
return
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
323
|
+
lastKey(beginRoot = this.root) {
|
|
324
|
+
let current = this.ensureNode(beginRoot);
|
|
325
|
+
if (!current)
|
|
326
|
+
return undefined;
|
|
327
|
+
if (this._variant === types_1.BSTVariant.MIN) {
|
|
328
|
+
// For BSTVariant.MIN, find the rightmost node
|
|
329
|
+
while (current.right !== undefined) {
|
|
330
|
+
current = current.right;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
// For BSTVariant.MAX, find the leftmost node
|
|
335
|
+
while (current.left !== undefined) {
|
|
336
|
+
current = current.left;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return current.key;
|
|
316
340
|
}
|
|
317
341
|
/**
|
|
318
342
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
@@ -324,7 +348,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
324
348
|
*
|
|
325
349
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
326
350
|
* either recursive or iterative methods.
|
|
327
|
-
* @param {
|
|
351
|
+
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
328
352
|
* It is used to identify the node that we want to retrieve.
|
|
329
353
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
330
354
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
@@ -363,6 +387,15 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
363
387
|
}
|
|
364
388
|
}
|
|
365
389
|
}
|
|
390
|
+
/**
|
|
391
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
392
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
393
|
+
* data type.
|
|
394
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
395
|
+
*/
|
|
396
|
+
isNotNodeInstance(potentialKey) {
|
|
397
|
+
return !(potentialKey instanceof BSTNode);
|
|
398
|
+
}
|
|
366
399
|
/**
|
|
367
400
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
368
401
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -370,14 +403,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
370
403
|
/**
|
|
371
404
|
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
372
405
|
* otherwise it returns the key itself.
|
|
373
|
-
* @param {
|
|
406
|
+
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
374
407
|
* `undefined`.
|
|
375
408
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
376
409
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
377
410
|
* @returns either a node object (N) or undefined.
|
|
378
411
|
*/
|
|
379
412
|
ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
380
|
-
return this.
|
|
413
|
+
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
381
414
|
}
|
|
382
415
|
/**
|
|
383
416
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
@@ -395,7 +428,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
395
428
|
* first node that matches the identifier. If set to true, the function will return an array
|
|
396
429
|
* containing only the first matching node. If set to false (default), the function will continue
|
|
397
430
|
* searching for all nodes that match the identifier and return an array containing
|
|
398
|
-
* @param {
|
|
431
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
399
432
|
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
400
433
|
* traversal will start from the root of the tree.
|
|
401
434
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
@@ -475,7 +508,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
475
508
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
476
509
|
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
477
510
|
* `lesserOrGreater` are
|
|
478
|
-
* @param {
|
|
511
|
+
* @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
479
512
|
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
480
513
|
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
481
514
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -565,7 +598,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
565
598
|
if (l <= r) {
|
|
566
599
|
const m = l + Math.floor((r - l) / 2);
|
|
567
600
|
const midNode = sorted[m];
|
|
568
|
-
debugger;
|
|
569
601
|
this.add([midNode.key, midNode.value]);
|
|
570
602
|
stack.push([m + 1, r]);
|
|
571
603
|
stack.push([l, m - 1]);
|
|
@@ -652,19 +684,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
652
684
|
/**
|
|
653
685
|
* The function compares two values using a comparator function and returns whether the first value
|
|
654
686
|
* is greater than, less than, or equal to the second value.
|
|
655
|
-
* @param {
|
|
656
|
-
* @param {
|
|
687
|
+
* @param {K} a - The parameter "a" is of type K.
|
|
688
|
+
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
657
689
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
658
690
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
659
691
|
*/
|
|
660
692
|
_compare(a, b) {
|
|
661
|
-
const
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
return types_1.CP.lt;
|
|
666
|
-
else
|
|
667
|
-
return types_1.CP.eq;
|
|
693
|
+
const extractedA = this.extractor(a);
|
|
694
|
+
const extractedB = this.extractor(b);
|
|
695
|
+
const compared = this.variant === types_1.BSTVariant.MIN ? extractedA - extractedB : extractedB - extractedA;
|
|
696
|
+
return compared > 0 ? types_1.CP.gt : compared < 0 ? types_1.CP.lt : types_1.CP.eq;
|
|
668
697
|
}
|
|
669
698
|
}
|
|
670
699
|
exports.BST = BST;
|