red-black-tree-typed 1.53.6 → 1.54.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/README.md +52 -0
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +28 -0
- package/dist/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -331
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +131 -71
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +309 -208
- package/dist/data-structures/binary-tree/binary-tree.js +382 -300
- package/dist/data-structures/binary-tree/bst.d.ts +245 -127
- package/dist/data-structures/binary-tree/bst.js +366 -163
- package/dist/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/data-structures/binary-tree/index.js +3 -1
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +286 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +181 -108
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
- package/dist/data-structures/binary-tree/tree-multi-map.js +145 -367
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/hash/hash-map.d.ts +31 -1
- package/dist/data-structures/hash/hash-map.js +35 -5
- package/dist/data-structures/heap/heap.d.ts +26 -9
- package/dist/data-structures/heap/heap.js +37 -17
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +64 -19
- package/dist/data-structures/linked-list/doubly-linked-list.js +92 -31
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +48 -12
- package/dist/data-structures/linked-list/singly-linked-list.js +74 -27
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +111 -10
- package/dist/data-structures/trie/trie.js +123 -18
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/index.js +2 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -5
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +25 -0
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +152 -373
- package/src/data-structures/binary-tree/avl-tree.ts +164 -106
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +563 -447
- package/src/data-structures/binary-tree/bst.ts +433 -237
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +224 -146
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/hash/hash-map.ts +37 -7
- package/src/data-structures/heap/heap.ts +72 -49
- package/src/data-structures/linked-list/doubly-linked-list.ts +186 -118
- package/src/data-structures/linked-list/singly-linked-list.ts +81 -28
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +123 -17
- package/src/index.ts +4 -3
- package/src/interfaces/binary-tree.ts +10 -21
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -6
- package/src/types/data-structures/binary-tree/bst.ts +8 -7
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -205
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
|
@@ -5,37 +5,28 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type { BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode, OptNodeOrNull } from '../../types';
|
|
9
9
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
constructor(key: K, value?: V);
|
|
14
|
-
protected _left?: NODE;
|
|
15
|
-
/**
|
|
16
|
-
* The function returns the value of the `_left` property.
|
|
17
|
-
* @returns The `_left` property of the current object is being returned.
|
|
18
|
-
*/
|
|
19
|
-
get left(): OptNode<NODE>;
|
|
20
|
-
/**
|
|
21
|
-
* The function sets the left child of a node and updates the parent reference of the child.
|
|
22
|
-
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
|
|
23
|
-
* instance of the `NODE` class or `undefined`.
|
|
24
|
-
*/
|
|
25
|
-
set left(v: OptNode<NODE>);
|
|
26
|
-
protected _right?: NODE;
|
|
11
|
+
import { Range } from '../../common';
|
|
12
|
+
export declare class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
27
13
|
/**
|
|
28
|
-
*
|
|
29
|
-
* @
|
|
30
|
-
*
|
|
14
|
+
* This TypeScript constructor function initializes an instance with a key and an optional value.
|
|
15
|
+
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
|
|
16
|
+
* within a data structure. It serves as a reference or identifier for accessing or manipulating the
|
|
17
|
+
* associated value.
|
|
18
|
+
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
|
|
19
|
+
* have to be provided when creating an instance of the class. If a value is not provided, it will
|
|
20
|
+
* default to `undefined`.
|
|
31
21
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
22
|
+
constructor(key: K, value?: V);
|
|
23
|
+
parent?: BSTNode<K, V>;
|
|
24
|
+
_left?: OptNodeOrNull<BSTNode<K, V>>;
|
|
25
|
+
get left(): OptNodeOrNull<BSTNode<K, V>>;
|
|
26
|
+
set left(v: OptNodeOrNull<BSTNode<K, V>>);
|
|
27
|
+
_right?: OptNodeOrNull<BSTNode<K, V>>;
|
|
28
|
+
get right(): OptNodeOrNull<BSTNode<K, V>>;
|
|
29
|
+
set right(v: OptNodeOrNull<BSTNode<K, V>>);
|
|
39
30
|
}
|
|
40
31
|
/**
|
|
41
32
|
* 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
|
|
@@ -45,58 +36,113 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
|
|
|
45
36
|
* 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
|
|
46
37
|
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
47
38
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
39
|
+
* @example
|
|
40
|
+
* // Merge 3 sorted datasets
|
|
41
|
+
* const dataset1 = new BST<number, string>([
|
|
42
|
+
* [1, 'A'],
|
|
43
|
+
* [7, 'G']
|
|
44
|
+
* ]);
|
|
45
|
+
* const dataset2 = [
|
|
46
|
+
* [2, 'B'],
|
|
47
|
+
* [6, 'F']
|
|
48
|
+
* ];
|
|
49
|
+
* const dataset3 = new BST<number, string>([
|
|
50
|
+
* [3, 'C'],
|
|
51
|
+
* [5, 'E'],
|
|
52
|
+
* [4, 'D']
|
|
53
|
+
* ]);
|
|
54
|
+
*
|
|
55
|
+
* // Merge datasets into a single BinarySearchTree
|
|
56
|
+
* const merged = new BST<number, string>(dataset1);
|
|
57
|
+
* merged.addMany(dataset2);
|
|
58
|
+
* merged.merge(dataset3);
|
|
59
|
+
*
|
|
60
|
+
* // Verify merged dataset is in sorted order
|
|
61
|
+
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
|
|
62
|
+
* @example
|
|
63
|
+
* // Find elements in a range
|
|
64
|
+
* const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
65
|
+
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7]
|
|
66
|
+
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7']
|
|
67
|
+
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7]
|
|
68
|
+
* console.log(bst.rangeSearch([15, 20])); // [15, 18]
|
|
69
|
+
* console.log(bst.search(new Range(15, 20, false))); // [18]
|
|
70
|
+
* @example
|
|
71
|
+
* // Find lowest common ancestor
|
|
72
|
+
* const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
|
|
73
|
+
*
|
|
74
|
+
* // LCA helper function
|
|
75
|
+
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
76
|
+
* const path1 = bst.getPathToRoot(num1);
|
|
77
|
+
* const path2 = bst.getPathToRoot(num2);
|
|
78
|
+
* // Find the first common ancestor
|
|
79
|
+
* return findFirstCommon(path1, path2);
|
|
80
|
+
* };
|
|
81
|
+
*
|
|
82
|
+
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
|
|
83
|
+
* for (const num of arr1) {
|
|
84
|
+
* if (arr2.indexOf(num) !== -1) {
|
|
85
|
+
* return num;
|
|
86
|
+
* }
|
|
87
|
+
* }
|
|
88
|
+
* return undefined;
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* // Assertions
|
|
92
|
+
* console.log(findLCA(3, 10)); // 7
|
|
93
|
+
* console.log(findLCA(5, 35)); // 15
|
|
94
|
+
* console.log(findLCA(20, 30)); // 25
|
|
48
95
|
*/
|
|
49
|
-
export declare class BST<K = any, V = any, R = object,
|
|
96
|
+
export declare class BST<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends BinaryTree<K, V, R, MK, MV, MR> implements IBinaryTree<K, V, R, MK, MV, MR> {
|
|
50
97
|
/**
|
|
51
|
-
* This
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
98
|
+
* This TypeScript constructor initializes a binary search tree with optional options and adds
|
|
99
|
+
* elements if provided.
|
|
100
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
101
|
+
* iterable that can contain elements of type `BTNRep<K, V, BSTNode<K, V>>` or `R`. It is used to
|
|
102
|
+
* initialize the binary search tree with keys, nodes, entries, or raw data.
|
|
103
|
+
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
104
|
+
* properties:
|
|
57
105
|
*/
|
|
58
|
-
constructor(keysNodesEntriesOrRaws?: Iterable<
|
|
59
|
-
protected _root?:
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
get
|
|
106
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, BSTNode<K, V>> | R>, options?: BSTOptions<K, V, R>);
|
|
107
|
+
protected _root?: BSTNode<K, V>;
|
|
108
|
+
get root(): OptNode<BSTNode<K, V>>;
|
|
109
|
+
protected _isReverse: boolean;
|
|
110
|
+
get isReverse(): boolean;
|
|
111
|
+
protected _comparator: Comparator<K>;
|
|
112
|
+
get comparator(): Comparator<K>;
|
|
113
|
+
protected _specifyComparable?: (key: K) => Comparable;
|
|
114
|
+
get specifyComparable(): ((key: K) => Comparable) | undefined;
|
|
65
115
|
/**
|
|
116
|
+
* Time Complexity: O(1)
|
|
117
|
+
* Space Complexity: O(1)
|
|
118
|
+
*
|
|
66
119
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
67
120
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
68
121
|
* being created.
|
|
69
122
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
70
123
|
* value associated with the key in the node being created.
|
|
71
|
-
* @returns The method is returning a new instance of the BSTNode class, casted as the
|
|
124
|
+
* @returns The method is returning a new instance of the BSTNode class, casted as the BSTNode<K, V> type.
|
|
72
125
|
*/
|
|
73
|
-
createNode(key: K, value?: V):
|
|
126
|
+
createNode(key: K, value?: V): BSTNode<K, V>;
|
|
74
127
|
/**
|
|
128
|
+
* Time Complexity: O(1)
|
|
129
|
+
* Space Complexity: O(1)
|
|
130
|
+
*
|
|
75
131
|
* The function creates a new binary search tree with the specified options.
|
|
76
132
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
77
133
|
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
78
134
|
* following properties:
|
|
79
135
|
* @returns a new instance of the BST class with the provided options.
|
|
80
136
|
*/
|
|
81
|
-
createTree(options?: BSTOptions<K, V, R>):
|
|
82
|
-
/**
|
|
83
|
-
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
84
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
85
|
-
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
86
|
-
* element.
|
|
87
|
-
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
88
|
-
* value associated with a key in a key-value pair.
|
|
89
|
-
* @returns either a NODE object or undefined.
|
|
90
|
-
*/
|
|
91
|
-
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNode<NODE>, V | undefined];
|
|
137
|
+
createTree(options?: BSTOptions<K, V, R>): BST<K, V, R, MK, MV, MR>;
|
|
92
138
|
/**
|
|
93
139
|
* Time Complexity: O(log n)
|
|
94
140
|
* Space Complexity: O(log n)
|
|
95
141
|
*
|
|
96
142
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
97
143
|
* it doesn't exist.
|
|
98
|
-
* @param {BTNRep<K, V,
|
|
99
|
-
* `
|
|
144
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
|
|
145
|
+
* `keyNodeOrEntry` can accept a value of type `R`, which represents the key, node,
|
|
100
146
|
* entry, or raw element that needs to be ensured in the tree.
|
|
101
147
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
102
148
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -104,36 +150,42 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
104
150
|
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
105
151
|
* not be ensured.
|
|
106
152
|
*/
|
|
107
|
-
ensureNode(
|
|
153
|
+
ensureNode(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>;
|
|
108
154
|
/**
|
|
155
|
+
* Time Complexity: O(1)
|
|
156
|
+
* Space Complexity: O(1)
|
|
157
|
+
*
|
|
109
158
|
* The function checks if the input is an instance of the BSTNode class.
|
|
110
|
-
* @param {BTNRep<K, V,
|
|
111
|
-
* `
|
|
112
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
159
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
|
|
160
|
+
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, BSTNode<K, V>>`.
|
|
161
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
113
162
|
* an instance of the `BSTNode` class.
|
|
114
163
|
*/
|
|
115
|
-
isNode(
|
|
164
|
+
isNode(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>): keyNodeOrEntry is BSTNode<K, V>;
|
|
116
165
|
/**
|
|
117
|
-
*
|
|
166
|
+
* Time Complexity: O(1)
|
|
167
|
+
* Space Complexity: O(1)
|
|
168
|
+
*
|
|
169
|
+
* The function "override isValidKey" checks if a key is comparable based on a given comparator.
|
|
118
170
|
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
|
|
119
171
|
* type `K`.
|
|
120
|
-
* @returns The `override
|
|
121
|
-
* the result of the `isComparable` function with the condition `this.
|
|
172
|
+
* @returns The `override isValidKey(key: any): key is K` function is returning a boolean value based on
|
|
173
|
+
* the result of the `isComparable` function with the condition `this._compare !==
|
|
122
174
|
* this._DEFAULT_COMPARATOR`.
|
|
123
175
|
*/
|
|
124
|
-
|
|
176
|
+
isValidKey(key: any): key is K;
|
|
125
177
|
/**
|
|
126
178
|
* Time Complexity: O(log n)
|
|
127
|
-
* Space Complexity: O(
|
|
179
|
+
* Space Complexity: O(log n)
|
|
128
180
|
*
|
|
129
181
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
130
|
-
* @param {BTNRep<K, V,
|
|
131
|
-
* `
|
|
182
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter
|
|
183
|
+
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, BSTNode<K, V>>`.
|
|
132
184
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
133
185
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
134
186
|
* @returns a boolean value.
|
|
135
187
|
*/
|
|
136
|
-
add(
|
|
188
|
+
add(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, value?: V): boolean;
|
|
137
189
|
/**
|
|
138
190
|
* Time Complexity: O(k log n)
|
|
139
191
|
* Space Complexity: O(k + log n)
|
|
@@ -155,39 +207,66 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
155
207
|
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
156
208
|
* successfully inserted into the data structure.
|
|
157
209
|
*/
|
|
158
|
-
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V,
|
|
210
|
+
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
159
211
|
/**
|
|
160
212
|
* Time Complexity: O(log n)
|
|
161
213
|
* Space Complexity: O(k + log n)
|
|
162
214
|
*
|
|
163
|
-
* The function `
|
|
164
|
-
*
|
|
165
|
-
* @param {BTNRep<K, V,
|
|
166
|
-
* parameter in the `
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
215
|
+
* The function `search` in TypeScript overrides the search behavior in a binary tree structure based
|
|
216
|
+
* on specified criteria.
|
|
217
|
+
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The
|
|
218
|
+
* `keyNodeEntryOrPredicate` parameter in the `override search` method can accept one of the
|
|
219
|
+
* following types:
|
|
220
|
+
* @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
|
|
221
|
+
* search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
|
|
222
|
+
* search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
|
|
223
|
+
* @param {C} callback - The `callback` parameter in the `override search` function is a function
|
|
224
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
225
|
+
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its
|
|
226
|
+
* argument and
|
|
227
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search`
|
|
228
|
+
* method represents the node from which the search operation will begin. It is the starting point
|
|
229
|
+
* for searching within the tree data structure. The method ensures that the `startNode` is a valid
|
|
230
|
+
* node before proceeding with the search operation. If the `
|
|
231
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `override search`
|
|
232
|
+
* function determines the type of iteration to be used during the search operation. It can have two
|
|
233
|
+
* possible values:
|
|
234
|
+
* @returns The `override search` method returns an array of values that match the search criteria
|
|
235
|
+
* specified by the input parameters. The method performs a search operation on a binary tree
|
|
236
|
+
* structure based on the provided key, predicate, and other options. The search results are
|
|
237
|
+
* collected in an array and returned as the output of the method.
|
|
181
238
|
*/
|
|
182
|
-
|
|
239
|
+
search<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[];
|
|
183
240
|
/**
|
|
184
241
|
* Time Complexity: O(log n)
|
|
185
|
-
* Space Complexity: O(
|
|
242
|
+
* Space Complexity: O(k + log n)
|
|
186
243
|
*
|
|
187
|
-
*
|
|
188
|
-
* @param {
|
|
189
|
-
*
|
|
190
|
-
* @param {
|
|
244
|
+
* The `rangeSearch` function searches for nodes within a specified range in a binary search tree.
|
|
245
|
+
* @param {Range<K> | [K, K]} range - The `range` parameter in the `rangeSearch` function can be
|
|
246
|
+
* either a `Range` object or an array of two elements representing the range boundaries.
|
|
247
|
+
* @param {C} callback - The `callback` parameter in the `rangeSearch` function is a callback
|
|
248
|
+
* function that is used to process each node that is found within the specified range during the
|
|
249
|
+
* search operation. It is of type `NodeCallback<BSTNode<K, V>>`, where `BSTNode<K, V>` is the type of nodes in the
|
|
250
|
+
* data structure.
|
|
251
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch`
|
|
252
|
+
* function represents the node from which the search for nodes within the specified range will
|
|
253
|
+
* begin. It is the starting point for the range search operation.
|
|
254
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `rangeSearch` function
|
|
255
|
+
* is used to specify the type of iteration to be performed during the search operation. It has a
|
|
256
|
+
* default value of `this.iterationType`, which suggests that it is likely a property of the class or
|
|
257
|
+
* object that the `rangeSearch`
|
|
258
|
+
* @returns The `rangeSearch` function is returning the result of calling the `search` method with
|
|
259
|
+
* the specified parameters.
|
|
260
|
+
*/
|
|
261
|
+
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[];
|
|
262
|
+
/**
|
|
263
|
+
* Time Complexity: O(log n)
|
|
264
|
+
* Space Complexity: O(log n)
|
|
265
|
+
*
|
|
266
|
+
* This function retrieves a node based on a given keyNodeEntryOrPredicate within a binary search tree structure.
|
|
267
|
+
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate`
|
|
268
|
+
* parameter can be of type `BTNRep<K, V, BSTNode<K, V>>`, `R`, or `NodePredicate<BSTNode<K, V>>`.
|
|
269
|
+
* @param {BSTNOptKeyOrNode<K, BSTNode<K, V>>} startNode - The `startNode` parameter in the `getNode` method
|
|
191
270
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
192
271
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
193
272
|
* node of the binary search tree.
|
|
@@ -195,26 +274,12 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
195
274
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
196
275
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
197
276
|
* no value is provided when calling the method.
|
|
198
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<
|
|
199
|
-
* It is using the `getNodes` method to find the node based on the provided
|
|
277
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<BSTNode<K, V>>`).
|
|
278
|
+
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryOrPredicate, beginning at
|
|
200
279
|
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
201
280
|
* returns the first node found or `undefined` if no node is found.
|
|
202
281
|
*/
|
|
203
|
-
getNode(
|
|
204
|
-
/**
|
|
205
|
-
* Time Complexity: O(log n)
|
|
206
|
-
* Space Complexity: O(1)
|
|
207
|
-
*
|
|
208
|
-
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
|
|
209
|
-
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
|
|
210
|
-
* is typically a unique identifier or a value that can be used to determine the position of the node
|
|
211
|
-
* in the tree structure.
|
|
212
|
-
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
213
|
-
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
214
|
-
* It has a default value of `'ITERATIVE'`.
|
|
215
|
-
* @returns The method is returning a NODE object or undefined.
|
|
216
|
-
*/
|
|
217
|
-
getNodeByKey(key: K, iterationType?: IterationType): OptNode<NODE>;
|
|
282
|
+
getNode(keyNodeEntryOrPredicate: BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>, startNode?: BSTNOptKeyOrNode<K, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>;
|
|
218
283
|
/**
|
|
219
284
|
* Time complexity: O(n)
|
|
220
285
|
* Space complexity: O(n)
|
|
@@ -227,7 +292,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
227
292
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
228
293
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
229
294
|
* take one of the following values:
|
|
230
|
-
* @param {BTNRep<K, V,
|
|
295
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
231
296
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
232
297
|
* node entry. If not specified, the default value is the root of the tree.
|
|
233
298
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -235,7 +300,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
235
300
|
* following values:
|
|
236
301
|
* @returns The method is returning an array of the return type of the callback function.
|
|
237
302
|
*/
|
|
238
|
-
dfs<C extends NodeCallback<
|
|
303
|
+
dfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[];
|
|
239
304
|
/**
|
|
240
305
|
* Time complexity: O(n)
|
|
241
306
|
* Space complexity: O(n)
|
|
@@ -245,7 +310,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
245
310
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
246
311
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
247
312
|
* node being visited, and it can return a value of any type.
|
|
248
|
-
* @param {BTNRep<K, V,
|
|
313
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
249
314
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
250
315
|
* object. If no value is provided, the default value is the root of the tree.
|
|
251
316
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -253,7 +318,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
253
318
|
* the following values:
|
|
254
319
|
* @returns an array of the return type of the callback function.
|
|
255
320
|
*/
|
|
256
|
-
bfs<C extends NodeCallback<
|
|
321
|
+
bfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[];
|
|
257
322
|
/**
|
|
258
323
|
* Time complexity: O(n)
|
|
259
324
|
* Space complexity: O(n)
|
|
@@ -261,9 +326,9 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
261
326
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
262
327
|
* containing the results of the callback function applied to each level of the tree.
|
|
263
328
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
264
|
-
* `NodeCallback<
|
|
329
|
+
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the
|
|
265
330
|
* tree during the iteration process.
|
|
266
|
-
* @param {BTNRep<K, V,
|
|
331
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
267
332
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
268
333
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
269
334
|
* value is provided, the root of
|
|
@@ -272,7 +337,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
272
337
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
273
338
|
* function.
|
|
274
339
|
*/
|
|
275
|
-
listLevels<C extends NodeCallback<
|
|
340
|
+
listLevels<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[][];
|
|
276
341
|
/**
|
|
277
342
|
* Time complexity: O(n)
|
|
278
343
|
* Space complexity: O(n)
|
|
@@ -285,7 +350,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
285
350
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
286
351
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
287
352
|
* 0, or 1, where:
|
|
288
|
-
* @param {BTNRep<K, V,
|
|
353
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in
|
|
289
354
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
290
355
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
291
356
|
* `targetNode` is provided,
|
|
@@ -294,7 +359,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
294
359
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
295
360
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
296
361
|
*/
|
|
297
|
-
lesserOrGreaterTraverse<C extends NodeCallback<
|
|
362
|
+
lesserOrGreaterTraverse<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[];
|
|
298
363
|
/**
|
|
299
364
|
* Time complexity: O(n)
|
|
300
365
|
* Space complexity: O(n)
|
|
@@ -321,17 +386,70 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
321
386
|
* @returns a boolean value.
|
|
322
387
|
*/
|
|
323
388
|
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
324
|
-
protected _DEFAULT_COMPARATOR: (a: K, b: K) => number;
|
|
325
|
-
protected _comparator: Comparator<K>;
|
|
326
389
|
/**
|
|
327
|
-
*
|
|
328
|
-
*
|
|
390
|
+
* Time complexity: O(n)
|
|
391
|
+
* Space complexity: O(n)
|
|
392
|
+
*
|
|
393
|
+
* The `map` function in TypeScript overrides the default map behavior for a binary search tree by
|
|
394
|
+
* applying a callback function to each entry and creating a new tree with the results.
|
|
395
|
+
* @param callback - A function that will be called for each entry in the BST. It takes four
|
|
396
|
+
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to
|
|
397
|
+
* the BST itself.
|
|
398
|
+
* @param [options] - The `options` parameter in the `override map` method is of type `BSTOptions<MK,
|
|
399
|
+
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the Binary
|
|
400
|
+
* Search Tree (BST) being created in the `map` method. These options could include configuration
|
|
401
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` method is used to specify
|
|
402
|
+
* the value of `this` that should be used when executing the `callback` function. It allows you to
|
|
403
|
+
* set the context or scope in which the callback function will be called. This can be useful when
|
|
404
|
+
* you want
|
|
405
|
+
* @returns The `map` method is returning a new Binary Search Tree (`BST`) instance with the entries
|
|
406
|
+
* transformed by the provided callback function.
|
|
329
407
|
*/
|
|
330
|
-
|
|
408
|
+
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BSTOptions<MK, MV, MR>, thisArg?: any): BST<MK, MV, MR>;
|
|
331
409
|
/**
|
|
410
|
+
* Time complexity: O(n)
|
|
411
|
+
* Space complexity: O(n)
|
|
412
|
+
*
|
|
413
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
414
|
+
* structure.
|
|
415
|
+
* @returns The `cloned` object is being returned.
|
|
416
|
+
*/
|
|
417
|
+
clone(): BST<K, V, R, MK, MV, MR>;
|
|
418
|
+
/**
|
|
419
|
+
* Time Complexity: O(1)
|
|
420
|
+
* Space Complexity: O(1)
|
|
421
|
+
*
|
|
422
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
423
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - A variable that can be of
|
|
424
|
+
* type R or BTNRep<K, V, BSTNode<K, V>>. It represents either a key, a node, an entry, or a raw
|
|
425
|
+
* element.
|
|
426
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
427
|
+
* value associated with a key in a key-value pair.
|
|
428
|
+
* @returns either a BSTNode<K, V> object or undefined.
|
|
429
|
+
*/
|
|
430
|
+
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, value?: V): [OptNode<BSTNode<K, V>>, V | undefined];
|
|
431
|
+
/**
|
|
432
|
+
* Time Complexity: O(1)
|
|
433
|
+
* Space Complexity: O(1)
|
|
434
|
+
*
|
|
332
435
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
333
436
|
* root.
|
|
334
|
-
* @param {OptNode<
|
|
437
|
+
* @param {OptNode<BSTNode<K, V>>} v - v is a parameter of type BSTNode<K, V> or undefined.
|
|
438
|
+
*/
|
|
439
|
+
protected _setRoot(v: OptNode<BSTNode<K, V>>): void;
|
|
440
|
+
/**
|
|
441
|
+
* Time Complexity: O(1)
|
|
442
|
+
* Space Complexity: O(1)
|
|
443
|
+
*
|
|
444
|
+
* The _compare function compares two values using a specified comparator function and optionally
|
|
445
|
+
* reverses the result.
|
|
446
|
+
* @param {K} a - The parameter `a` is of type `K`, which is used as an input for comparison in the
|
|
447
|
+
* `_compare` method.
|
|
448
|
+
* @param {K} b - The parameter `b` in the `_compare` function is of type `K`.
|
|
449
|
+
* @returns The `_compare` method is returning the result of the ternary expression. If `_isReverse`
|
|
450
|
+
* is true, it returns the negation of the result of calling the `_comparator` function with
|
|
451
|
+
* arguments `a` and `b`. If `_isReverse` is false, it returns the result of calling the
|
|
452
|
+
* `_comparator` function with arguments `a` and `b`.
|
|
335
453
|
*/
|
|
336
|
-
protected
|
|
454
|
+
protected _compare(a: K, b: K): number;
|
|
337
455
|
}
|