data-structure-typed 1.48.1 → 1.48.3
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 +24 -18
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +172 -172
- package/dist/cjs/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/data-structures/base/index.js +18 -0
- package/dist/cjs/data-structures/base/index.js.map +1 -0
- package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/cjs/data-structures/base/iterable-base.js +313 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +16 -16
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +121 -152
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +140 -182
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +28 -47
- package/dist/cjs/data-structures/binary-tree/bst.js +54 -57
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +15 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +7 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +22 -22
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +11 -11
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/cjs/data-structures/hash/hash-map.js +69 -173
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/cjs/data-structures/heap/heap.js +60 -30
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/index.d.ts +1 -0
- package/dist/cjs/data-structures/index.js +1 -0
- package/dist/cjs/data-structures/index.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- 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 +32 -51
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/cjs/data-structures/queue/deque.js +36 -71
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/cjs/data-structures/queue/queue.js +69 -82
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/cjs/data-structures/stack/stack.js +50 -31
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/cjs/data-structures/trie/trie.js +53 -32
- 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 +11 -8
- package/dist/cjs/types/common.js +6 -1
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/cjs/types/data-structures/base/base.js +3 -0
- package/dist/cjs/types/data-structures/base/base.js.map +1 -0
- package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/base/index.js +18 -0
- package/dist/cjs/types/data-structures/base/index.js.map +1 -0
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/cjs/types/data-structures/index.d.ts +1 -0
- package/dist/cjs/types/data-structures/index.js +1 -0
- package/dist/cjs/types/data-structures/index.js.map +1 -1
- package/dist/mjs/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/data-structures/base/index.js +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/mjs/data-structures/base/iterable-base.js +307 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +16 -16
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +7 -7
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +121 -152
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +140 -182
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +28 -47
- package/dist/mjs/data-structures/binary-tree/bst.js +55 -57
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +15 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +7 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +22 -22
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +11 -11
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
- package/dist/mjs/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/mjs/data-structures/hash/hash-map.js +69 -173
- package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
- package/dist/mjs/data-structures/heap/heap.js +60 -30
- package/dist/mjs/data-structures/index.d.ts +1 -0
- package/dist/mjs/data-structures/index.js +1 -0
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
- package/dist/mjs/data-structures/queue/deque.js +36 -71
- package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
- package/dist/mjs/data-structures/queue/queue.js +66 -79
- package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
- package/dist/mjs/data-structures/stack/stack.js +50 -31
- package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
- package/dist/mjs/data-structures/trie/trie.js +53 -32
- package/dist/mjs/interfaces/binary-tree.d.ts +6 -6
- package/dist/mjs/types/common.d.ts +11 -8
- package/dist/mjs/types/common.js +5 -0
- package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
- package/dist/mjs/types/data-structures/base/base.js +1 -0
- package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/base/index.js +1 -0
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
- package/dist/mjs/types/data-structures/index.d.ts +1 -0
- package/dist/mjs/types/data-structures/index.js +1 -0
- package/dist/umd/data-structure-typed.js +991 -848
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- 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 +20 -21
- package/src/data-structures/binary-tree/binary-tree.ts +222 -267
- package/src/data-structures/binary-tree/bst.ts +86 -82
- package/src/data-structures/binary-tree/rb-tree.ts +25 -26
- package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +76 -185
- 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 +50 -79
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
- package/src/data-structures/queue/deque.ts +40 -82
- package/src/data-structures/queue/queue.ts +72 -87
- 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/index.ts +1 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +13 -13
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +16 -16
- package/test/unit/data-structures/binary-tree/bst.test.ts +20 -19
- package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +9 -9
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +15 -15
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
- package/test/unit/data-structures/queue/deque.test.ts +25 -0
- package/test/unit/unrestricted-interconversion.test.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, 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,20 @@ 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
83
|
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
83
84
|
* is valid, otherwise it returns undefined.
|
|
84
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
85
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
85
86
|
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
86
87
|
*/
|
|
87
|
-
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
|
|
88
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>): N | undefined;
|
|
88
89
|
/**
|
|
89
90
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
90
91
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -99,7 +100,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
99
100
|
* @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
|
|
100
101
|
* (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
|
|
101
102
|
*/
|
|
102
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
|
|
103
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | undefined;
|
|
103
104
|
/**
|
|
104
105
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
105
106
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
@@ -120,27 +121,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
120
121
|
* tree instance.
|
|
121
122
|
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
122
123
|
*/
|
|
123
|
-
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
124
|
-
/**
|
|
125
|
-
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
126
|
-
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
127
|
-
*/
|
|
128
|
-
/**
|
|
129
|
-
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
130
|
-
* Space Complexity: O(1) - Constant space is used.
|
|
131
|
-
*
|
|
132
|
-
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
133
|
-
* leftmost node if the comparison result is greater than.
|
|
134
|
-
* @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
135
|
-
* type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
136
|
-
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
137
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
138
|
-
* be performed. It can have one of the following values:
|
|
139
|
-
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
140
|
-
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
141
|
-
* rightmost node otherwise. If no node is found, it returns 0.
|
|
142
|
-
*/
|
|
143
|
-
lastKey(beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): BTNKey;
|
|
124
|
+
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
144
125
|
/**
|
|
145
126
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
146
127
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -151,7 +132,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
151
132
|
*
|
|
152
133
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
153
134
|
* either recursive or iterative methods.
|
|
154
|
-
* @param {
|
|
135
|
+
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
155
136
|
* It is used to identify the node that we want to retrieve.
|
|
156
137
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
157
138
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
@@ -159,7 +140,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
159
140
|
* @returns The function `getNodeByKey` returns a node (`N`) if a node with the specified key is
|
|
160
141
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
161
142
|
*/
|
|
162
|
-
getNodeByKey(key:
|
|
143
|
+
getNodeByKey(key: K, iterationType?: IterationType): N | undefined;
|
|
163
144
|
/**
|
|
164
145
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
165
146
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -167,13 +148,13 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
167
148
|
/**
|
|
168
149
|
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
169
150
|
* otherwise it returns the key itself.
|
|
170
|
-
* @param {
|
|
151
|
+
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
171
152
|
* `undefined`.
|
|
172
153
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
173
154
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
174
155
|
* @returns either a node object (N) or undefined.
|
|
175
156
|
*/
|
|
176
|
-
ensureNode(key: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N | undefined;
|
|
157
|
+
ensureNode(key: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): N | undefined;
|
|
177
158
|
/**
|
|
178
159
|
* 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
160
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -190,14 +171,14 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
190
171
|
* first node that matches the identifier. If set to true, the function will return an array
|
|
191
172
|
* containing only the first matching node. If set to false (default), the function will continue
|
|
192
173
|
* searching for all nodes that match the identifier and return an array containing
|
|
193
|
-
* @param {
|
|
174
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
194
175
|
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
195
176
|
* traversal will start from the root of the tree.
|
|
196
177
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
197
178
|
* performed on the binary tree. It can have two possible values:
|
|
198
179
|
* @returns The method returns an array of nodes (`N[]`).
|
|
199
180
|
*/
|
|
200
|
-
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N[];
|
|
181
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): N[];
|
|
201
182
|
/**
|
|
202
183
|
* 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
184
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -215,7 +196,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
215
196
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
216
197
|
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
217
198
|
* `lesserOrGreater` are
|
|
218
|
-
* @param {
|
|
199
|
+
* @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
219
200
|
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
220
201
|
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
221
202
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -223,7 +204,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
223
204
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
224
205
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
225
206
|
*/
|
|
226
|
-
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): ReturnType<C>[];
|
|
207
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): ReturnType<C>[];
|
|
227
208
|
/**
|
|
228
209
|
* 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
210
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -267,10 +248,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
267
248
|
/**
|
|
268
249
|
* The function compares two values using a comparator function and returns whether the first value
|
|
269
250
|
* is greater than, less than, or equal to the second value.
|
|
270
|
-
* @param {
|
|
271
|
-
* @param {
|
|
251
|
+
* @param {K} a - The parameter "a" is of type K.
|
|
252
|
+
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
272
253
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
273
254
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
274
255
|
*/
|
|
275
|
-
protected _compare(a:
|
|
256
|
+
protected _compare(a: K, b: K): CP;
|
|
276
257
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CP, IterationType } from '../../types';
|
|
1
|
+
import { BSTVariant, CP, IterationType } from '../../types';
|
|
2
2
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
3
3
|
import { Queue } from '../queue';
|
|
4
4
|
export class BSTNode extends BinaryTreeNode {
|
|
@@ -65,9 +65,9 @@ export class BST extends BinaryTree {
|
|
|
65
65
|
constructor(elements, options) {
|
|
66
66
|
super([], options);
|
|
67
67
|
if (options) {
|
|
68
|
-
const {
|
|
69
|
-
if (
|
|
70
|
-
this.
|
|
68
|
+
const { variant } = options;
|
|
69
|
+
if (variant) {
|
|
70
|
+
this._variant = variant;
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
this._root = undefined;
|
|
@@ -78,10 +78,13 @@ export class BST extends BinaryTree {
|
|
|
78
78
|
get root() {
|
|
79
79
|
return this._root;
|
|
80
80
|
}
|
|
81
|
-
|
|
81
|
+
_variant = BSTVariant.MIN;
|
|
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.
|
|
@@ -100,12 +103,12 @@ export class BST extends BinaryTree {
|
|
|
100
103
|
createTree(options) {
|
|
101
104
|
return new BST([], {
|
|
102
105
|
iterationType: this.iterationType,
|
|
103
|
-
|
|
106
|
+
variant: this.variant, ...options
|
|
104
107
|
});
|
|
105
108
|
}
|
|
106
109
|
/**
|
|
107
110
|
* The function checks if an exemplar is an instance of BSTNode.
|
|
108
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
|
|
111
|
+
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
|
|
109
112
|
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
110
113
|
*/
|
|
111
114
|
isNode(exemplar) {
|
|
@@ -114,7 +117,7 @@ export class BST extends BinaryTree {
|
|
|
114
117
|
/**
|
|
115
118
|
* The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
|
|
116
119
|
* is valid, otherwise it returns undefined.
|
|
117
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
120
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
118
121
|
* @returns a variable `node` which is of type `N` or `undefined`.
|
|
119
122
|
*/
|
|
120
123
|
exemplarToNode(exemplar) {
|
|
@@ -134,7 +137,7 @@ export class BST extends BinaryTree {
|
|
|
134
137
|
node = this.createNode(key, value);
|
|
135
138
|
}
|
|
136
139
|
}
|
|
137
|
-
else if (this.
|
|
140
|
+
else if (this.isNotNodeInstance(exemplar)) {
|
|
138
141
|
node = this.createNode(exemplar);
|
|
139
142
|
}
|
|
140
143
|
else {
|
|
@@ -242,17 +245,17 @@ export class BST extends BinaryTree {
|
|
|
242
245
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
243
246
|
let aR, bR;
|
|
244
247
|
if (this.isEntry(a))
|
|
245
|
-
aR = a[0];
|
|
248
|
+
aR = this.extractor(a[0]);
|
|
246
249
|
else if (this.isRealNode(a))
|
|
247
|
-
aR = a.key;
|
|
250
|
+
aR = this.extractor(a.key);
|
|
248
251
|
else
|
|
249
|
-
aR = a;
|
|
252
|
+
aR = this.extractor(a);
|
|
250
253
|
if (this.isEntry(b))
|
|
251
|
-
bR = b[0];
|
|
254
|
+
bR = this.extractor(b[0]);
|
|
252
255
|
else if (this.isRealNode(b))
|
|
253
|
-
bR = b.key;
|
|
256
|
+
bR = this.extractor(b.key);
|
|
254
257
|
else
|
|
255
|
-
bR = b;
|
|
258
|
+
bR = this.extractor(b);
|
|
256
259
|
return aR - bR;
|
|
257
260
|
});
|
|
258
261
|
const _dfs = (arr) => {
|
|
@@ -289,33 +292,31 @@ export class BST extends BinaryTree {
|
|
|
289
292
|
}
|
|
290
293
|
return inserted;
|
|
291
294
|
}
|
|
292
|
-
/**
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
318
|
-
}
|
|
295
|
+
// /**
|
|
296
|
+
// * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
297
|
+
// * Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
298
|
+
// */
|
|
299
|
+
//
|
|
300
|
+
// /**
|
|
301
|
+
// * Time Complexity: O(log n) - Average case for a balanced tree.
|
|
302
|
+
// * Space Complexity: O(1) - Constant space is used.
|
|
303
|
+
// *
|
|
304
|
+
// * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
305
|
+
// * leftmost node if the comparison result is greater than.
|
|
306
|
+
// * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
307
|
+
// * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
308
|
+
// * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
309
|
+
// * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
310
|
+
// * be performed. It can have one of the following values:
|
|
311
|
+
// * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
312
|
+
// * the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
313
|
+
// * rightmost node otherwise. If no node is found, it returns 0.
|
|
314
|
+
// */
|
|
315
|
+
// lastKey(beginRoot: BSTNodeKeyOrNode<K,N> = this.root, iterationType = this.iterationType): K {
|
|
316
|
+
// if (this._compare(0, 1) === CP.lt) return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
317
|
+
// else if (this._compare(0, 1) === CP.gt) return this.getLeftMost(beginRoot, iterationType)?.key ?? 0;
|
|
318
|
+
// else return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
319
|
+
// }
|
|
319
320
|
/**
|
|
320
321
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
321
322
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -326,7 +327,7 @@ export class BST extends BinaryTree {
|
|
|
326
327
|
*
|
|
327
328
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
328
329
|
* either recursive or iterative methods.
|
|
329
|
-
* @param {
|
|
330
|
+
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
330
331
|
* It is used to identify the node that we want to retrieve.
|
|
331
332
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
332
333
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
@@ -372,14 +373,14 @@ export class BST extends BinaryTree {
|
|
|
372
373
|
/**
|
|
373
374
|
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
374
375
|
* otherwise it returns the key itself.
|
|
375
|
-
* @param {
|
|
376
|
+
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
376
377
|
* `undefined`.
|
|
377
378
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
378
379
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
379
380
|
* @returns either a node object (N) or undefined.
|
|
380
381
|
*/
|
|
381
382
|
ensureNode(key, iterationType = IterationType.ITERATIVE) {
|
|
382
|
-
return this.
|
|
383
|
+
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
383
384
|
}
|
|
384
385
|
/**
|
|
385
386
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
@@ -397,7 +398,7 @@ export class BST extends BinaryTree {
|
|
|
397
398
|
* first node that matches the identifier. If set to true, the function will return an array
|
|
398
399
|
* containing only the first matching node. If set to false (default), the function will continue
|
|
399
400
|
* searching for all nodes that match the identifier and return an array containing
|
|
400
|
-
* @param {
|
|
401
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
401
402
|
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
402
403
|
* traversal will start from the root of the tree.
|
|
403
404
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
@@ -477,7 +478,7 @@ export class BST extends BinaryTree {
|
|
|
477
478
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
478
479
|
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
479
480
|
* `lesserOrGreater` are
|
|
480
|
-
* @param {
|
|
481
|
+
* @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
481
482
|
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
482
483
|
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
483
484
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -653,18 +654,15 @@ export class BST extends BinaryTree {
|
|
|
653
654
|
/**
|
|
654
655
|
* The function compares two values using a comparator function and returns whether the first value
|
|
655
656
|
* is greater than, less than, or equal to the second value.
|
|
656
|
-
* @param {
|
|
657
|
-
* @param {
|
|
657
|
+
* @param {K} a - The parameter "a" is of type K.
|
|
658
|
+
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
658
659
|
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
659
660
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
660
661
|
*/
|
|
661
662
|
_compare(a, b) {
|
|
662
|
-
const
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
return CP.lt;
|
|
667
|
-
else
|
|
668
|
-
return CP.eq;
|
|
663
|
+
const extractedA = this.extractor(a);
|
|
664
|
+
const extractedB = this.extractor(b);
|
|
665
|
+
const compared = this.variant === BSTVariant.MIN ? extractedA - extractedB : extractedB - extractedA;
|
|
666
|
+
return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
|
|
669
667
|
}
|
|
670
668
|
}
|
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BiTreeDeleteResult, BTNCallback,
|
|
8
|
+
import { BiTreeDeleteResult, BTNCallback, BTNodeExemplar, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
11
|
+
export declare class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
12
12
|
color: RBTNColor;
|
|
13
|
-
constructor(key:
|
|
13
|
+
constructor(key: K, value?: V, color?: RBTNColor);
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* 1. Each node is either red or black.
|
|
@@ -19,12 +19,12 @@ export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N>
|
|
|
19
19
|
* 4. Red nodes must have black children.
|
|
20
20
|
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
21
21
|
*/
|
|
22
|
-
export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>, TREE extends RedBlackTree<V, N, TREE> = RedBlackTree<V, N, RedBlackTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
22
|
+
export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, N, TREE> = RedBlackTree<K, V, N, RedBlackTreeNested<K, V, N>>> extends BST<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
23
23
|
Sentinel: N;
|
|
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 `BTNodeExemplar<V, N>`
|
|
27
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<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,14 +32,14 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
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<BTNodeExemplar<V, N>>, options?: Partial<RBTreeOptions
|
|
35
|
+
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>);
|
|
36
36
|
protected _root: N;
|
|
37
37
|
get root(): N;
|
|
38
38
|
protected _size: number;
|
|
39
39
|
get size(): number;
|
|
40
40
|
/**
|
|
41
41
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
42
|
-
* @param {
|
|
42
|
+
* @param {K} key - The key parameter is the key value associated with the node. It is used to
|
|
43
43
|
* identify and compare nodes in the Red-Black Tree.
|
|
44
44
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
45
45
|
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
@@ -49,7 +49,7 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
49
49
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
50
50
|
* value, and color.
|
|
51
51
|
*/
|
|
52
|
-
createNode(key:
|
|
52
|
+
createNode(key: K, value?: V, color?: RBTNColor): N;
|
|
53
53
|
/**
|
|
54
54
|
* The function creates a Red-Black Tree with the specified options and returns it.
|
|
55
55
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
@@ -57,23 +57,23 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
57
57
|
* class.
|
|
58
58
|
* @returns a new instance of a RedBlackTree object.
|
|
59
59
|
*/
|
|
60
|
-
createTree(options?: RBTreeOptions): TREE;
|
|
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 `BTNodeExemplar<V, N>`.
|
|
63
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<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: BTNodeExemplar<V, N>): exemplar is N;
|
|
67
|
+
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
68
68
|
/**
|
|
69
69
|
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
70
70
|
* otherwise it returns undefined.
|
|
71
|
-
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
71
|
+
* @param exemplar - BTNodeExemplar<K, V, N> - A generic type representing an exemplar of a binary tree
|
|
72
72
|
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
73
73
|
* that is not a valid exemplar.
|
|
74
74
|
* @returns a variable `node` which is of type `N | undefined`.
|
|
75
75
|
*/
|
|
76
|
-
exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
|
|
76
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>): N | undefined;
|
|
77
77
|
/**
|
|
78
78
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
79
79
|
* Space Complexity: O(1)
|
|
@@ -84,7 +84,7 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
84
84
|
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
85
85
|
* `undefined`.
|
|
86
86
|
*/
|
|
87
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
|
|
87
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | undefined;
|
|
88
88
|
/**
|
|
89
89
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
90
90
|
* Space Complexity: O(1)
|
|
@@ -110,7 +110,7 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
110
110
|
* Space Complexity: O(1)
|
|
111
111
|
*/
|
|
112
112
|
isRealNode(node: N | undefined): node is N;
|
|
113
|
-
getNode<C extends BTNCallback<N,
|
|
113
|
+
getNode<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
114
114
|
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
115
115
|
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
116
116
|
/**
|
|
@@ -27,7 +27,7 @@ export class RedBlackTree extends BST {
|
|
|
27
27
|
/**
|
|
28
28
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
29
29
|
* initializes the tree with optional elements and options.
|
|
30
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
30
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
|
|
31
31
|
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
32
32
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
33
33
|
* elements to the
|
|
@@ -51,7 +51,7 @@ export class RedBlackTree extends BST {
|
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
53
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
54
|
-
* @param {
|
|
54
|
+
* @param {K} key - The key parameter is the key value associated with the node. It is used to
|
|
55
55
|
* identify and compare nodes in the Red-Black Tree.
|
|
56
56
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
57
57
|
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
@@ -74,12 +74,12 @@ export class RedBlackTree extends BST {
|
|
|
74
74
|
createTree(options) {
|
|
75
75
|
return new RedBlackTree([], {
|
|
76
76
|
iterationType: this.iterationType,
|
|
77
|
-
|
|
77
|
+
variant: this.variant, ...options
|
|
78
78
|
});
|
|
79
79
|
}
|
|
80
80
|
/**
|
|
81
81
|
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
|
82
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
|
|
82
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
83
83
|
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
84
84
|
* class.
|
|
85
85
|
*/
|
|
@@ -89,7 +89,7 @@ export class RedBlackTree extends BST {
|
|
|
89
89
|
/**
|
|
90
90
|
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
91
91
|
* otherwise it returns undefined.
|
|
92
|
-
* @param exemplar - BTNodeExemplar<V, N> - A generic type representing an exemplar of a binary tree
|
|
92
|
+
* @param exemplar - BTNodeExemplar<K, V, N> - A generic type representing an exemplar of a binary tree
|
|
93
93
|
* node. It can be either a node itself, an entry (key-value pair), a node key, or any other value
|
|
94
94
|
* that is not a valid exemplar.
|
|
95
95
|
* @returns a variable `node` which is of type `N | undefined`.
|
|
@@ -111,7 +111,7 @@ export class RedBlackTree extends BST {
|
|
|
111
111
|
node = this.createNode(key, value, RBTNColor.RED);
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
|
-
else if (this.
|
|
114
|
+
else if (this.isNotNodeInstance(exemplar)) {
|
|
115
115
|
node = this.createNode(exemplar, undefined, RBTNColor.RED);
|
|
116
116
|
}
|
|
117
117
|
else {
|
|
@@ -277,7 +277,7 @@ export class RedBlackTree extends BST {
|
|
|
277
277
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
278
278
|
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
279
279
|
* function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
|
|
280
|
-
* @param {
|
|
280
|
+
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
281
281
|
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
282
282
|
* provided, the search will start from the root of the binary tree.
|
|
283
283
|
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|