red-black-tree-typed 1.53.7 → 1.54.2
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/LICENSE +2 -2
- package/README.md +52 -0
- package/dist/common/index.js +5 -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 -328
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +130 -70
- 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 +268 -202
- package/dist/data-structures/binary-tree/binary-tree.js +311 -263
- package/dist/data-structures/binary-tree/bst.d.ts +193 -139
- package/dist/data-structures/binary-tree/bst.js +248 -164
- 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} +176 -107
- 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 +20 -3
- package/dist/data-structures/heap/heap.js +31 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
- package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +47 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +73 -26
- 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 +8 -7
- package/dist/data-structures/trie/trie.js +8 -7
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- 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-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 +0 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/types/data-structures/binary-tree/index.js +3 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.js +2 -0
- 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/package.json +3 -3
- package/src/common/index.ts +7 -1
- 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 +151 -370
- package/src/data-structures/binary-tree/avl-tree.ts +162 -105
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +488 -416
- package/src/data-structures/binary-tree/bst.ts +326 -251
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +219 -145
- 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 +33 -10
- package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
- package/src/data-structures/linked-list/singly-linked-list.ts +80 -27
- 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 +8 -7
- package/src/index.ts +4 -4
- 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 +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -6
- 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/dist/data-structures/binary-tree/rb-tree.d.ts +0 -209
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
- /package/dist/types/data-structures/binary-tree/{rb-tree.js → avl-tree-counter.js} +0 -0
|
@@ -5,38 +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
11
|
import { Range } from '../../common';
|
|
12
|
-
export declare class BSTNode<K = any, V = any
|
|
13
|
-
parent?: NODE;
|
|
14
|
-
constructor(key: K, value?: V);
|
|
15
|
-
protected _left?: NODE;
|
|
16
|
-
/**
|
|
17
|
-
* The function returns the value of the `_left` property.
|
|
18
|
-
* @returns The `_left` property of the current object is being returned.
|
|
19
|
-
*/
|
|
20
|
-
get left(): OptNode<NODE>;
|
|
12
|
+
export declare class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
21
13
|
/**
|
|
22
|
-
*
|
|
23
|
-
* @param {
|
|
24
|
-
*
|
|
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`.
|
|
25
21
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* The function sets the right child of a node and updates the parent reference of the child.
|
|
36
|
-
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
|
|
37
|
-
* `NODE` object or `undefined`.
|
|
38
|
-
*/
|
|
39
|
-
set right(v: OptNode<NODE>);
|
|
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>>);
|
|
40
30
|
}
|
|
41
31
|
/**
|
|
42
32
|
* 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
|
|
@@ -47,32 +37,48 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
|
|
|
47
37
|
* 6. Balance Variability: Can become unbalanced; special types maintain balance.
|
|
48
38
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
49
39
|
* @example
|
|
50
|
-
* //
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
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
54
|
*
|
|
55
|
-
* //
|
|
56
|
-
* const
|
|
57
|
-
*
|
|
58
|
-
*
|
|
55
|
+
* // Merge datasets into a single BinarySearchTree
|
|
56
|
+
* const merged = new BST<number, string>(dataset1);
|
|
57
|
+
* merged.addMany(dataset2);
|
|
58
|
+
* merged.merge(dataset3);
|
|
59
59
|
*
|
|
60
|
-
* //
|
|
61
|
-
* console.log(
|
|
62
|
-
* console.log(findKthSmallest(3)); // 4
|
|
63
|
-
* console.log(findKthSmallest(7)); // 8
|
|
60
|
+
* // Verify merged dataset is in sorted order
|
|
61
|
+
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
|
|
64
62
|
* @example
|
|
65
63
|
* // Find elements in a range
|
|
66
64
|
* const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
67
65
|
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7]
|
|
68
|
-
* console.log(bst.
|
|
66
|
+
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7']
|
|
69
67
|
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7]
|
|
70
|
-
* console.log(bst.
|
|
68
|
+
* console.log(bst.rangeSearch([15, 20])); // [15, 18]
|
|
71
69
|
* console.log(bst.search(new Range(15, 20, false))); // [18]
|
|
72
70
|
* @example
|
|
73
71
|
* // Find lowest common ancestor
|
|
74
72
|
* const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
|
|
75
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
|
+
*
|
|
76
82
|
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
|
|
77
83
|
* for (const num of arr1) {
|
|
78
84
|
* if (arr2.indexOf(num) !== -1) {
|
|
@@ -82,77 +88,61 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
|
|
|
82
88
|
* return undefined;
|
|
83
89
|
* }
|
|
84
90
|
*
|
|
85
|
-
* // LCA helper function
|
|
86
|
-
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
87
|
-
* const path1 = bst.getPathToRoot(num1);
|
|
88
|
-
* const path2 = bst.getPathToRoot(num2);
|
|
89
|
-
* // Find the first common ancestor
|
|
90
|
-
* return findFirstCommon(path1, path2);
|
|
91
|
-
* };
|
|
92
|
-
*
|
|
93
91
|
* // Assertions
|
|
94
92
|
* console.log(findLCA(3, 10)); // 7
|
|
95
93
|
* console.log(findLCA(5, 35)); // 15
|
|
96
94
|
* console.log(findLCA(20, 30)); // 25
|
|
97
95
|
*/
|
|
98
|
-
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> {
|
|
99
97
|
/**
|
|
100
|
-
* This
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
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:
|
|
106
105
|
*/
|
|
107
|
-
constructor(keysNodesEntriesOrRaws?: Iterable<
|
|
108
|
-
protected _root?:
|
|
109
|
-
|
|
110
|
-
* The function returns the root node of a tree structure.
|
|
111
|
-
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
112
|
-
*/
|
|
113
|
-
get root(): OptNode<NODE>;
|
|
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>>;
|
|
114
109
|
protected _isReverse: boolean;
|
|
115
|
-
/**
|
|
116
|
-
* The above function is a getter method in TypeScript that returns the value of the private property
|
|
117
|
-
* `_isReverse`.
|
|
118
|
-
* @returns The `isReverse` property of the object, which is a boolean value.
|
|
119
|
-
*/
|
|
120
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;
|
|
121
115
|
/**
|
|
116
|
+
* Time Complexity: O(1)
|
|
117
|
+
* Space Complexity: O(1)
|
|
118
|
+
*
|
|
122
119
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
123
120
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
124
121
|
* being created.
|
|
125
122
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
126
123
|
* value associated with the key in the node being created.
|
|
127
|
-
* @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.
|
|
128
125
|
*/
|
|
129
|
-
createNode(key: K, value?: V):
|
|
126
|
+
createNode(key: K, value?: V): BSTNode<K, V>;
|
|
130
127
|
/**
|
|
128
|
+
* Time Complexity: O(1)
|
|
129
|
+
* Space Complexity: O(1)
|
|
130
|
+
*
|
|
131
131
|
* The function creates a new binary search tree with the specified options.
|
|
132
132
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
133
133
|
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
134
134
|
* following properties:
|
|
135
135
|
* @returns a new instance of the BST class with the provided options.
|
|
136
136
|
*/
|
|
137
|
-
createTree(options?: BSTOptions<K, V, R>):
|
|
138
|
-
/**
|
|
139
|
-
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
140
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
141
|
-
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
142
|
-
* element.
|
|
143
|
-
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
144
|
-
* value associated with a key in a key-value pair.
|
|
145
|
-
* @returns either a NODE object or undefined.
|
|
146
|
-
*/
|
|
147
|
-
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>;
|
|
148
138
|
/**
|
|
149
139
|
* Time Complexity: O(log n)
|
|
150
140
|
* Space Complexity: O(log n)
|
|
151
141
|
*
|
|
152
142
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
153
143
|
* it doesn't exist.
|
|
154
|
-
* @param {BTNRep<K, V,
|
|
155
|
-
* `
|
|
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,
|
|
156
146
|
* entry, or raw element that needs to be ensured in the tree.
|
|
157
147
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
158
148
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -160,36 +150,42 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
160
150
|
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
161
151
|
* not be ensured.
|
|
162
152
|
*/
|
|
163
|
-
ensureNode(
|
|
153
|
+
ensureNode(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>;
|
|
164
154
|
/**
|
|
155
|
+
* Time Complexity: O(1)
|
|
156
|
+
* Space Complexity: O(1)
|
|
157
|
+
*
|
|
165
158
|
* The function checks if the input is an instance of the BSTNode class.
|
|
166
|
-
* @param {BTNRep<K, V,
|
|
167
|
-
* `
|
|
168
|
-
* @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
|
|
169
162
|
* an instance of the `BSTNode` class.
|
|
170
163
|
*/
|
|
171
|
-
isNode(
|
|
164
|
+
isNode(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>): keyNodeOrEntry is BSTNode<K, V>;
|
|
172
165
|
/**
|
|
173
|
-
*
|
|
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.
|
|
174
170
|
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
|
|
175
171
|
* type `K`.
|
|
176
|
-
* @returns The `override
|
|
172
|
+
* @returns The `override isValidKey(key: any): key is K` function is returning a boolean value based on
|
|
177
173
|
* the result of the `isComparable` function with the condition `this._compare !==
|
|
178
174
|
* this._DEFAULT_COMPARATOR`.
|
|
179
175
|
*/
|
|
180
|
-
|
|
176
|
+
isValidKey(key: any): key is K;
|
|
181
177
|
/**
|
|
182
178
|
* Time Complexity: O(log n)
|
|
183
|
-
* Space Complexity: O(
|
|
179
|
+
* Space Complexity: O(log n)
|
|
184
180
|
*
|
|
185
181
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
186
|
-
* @param {BTNRep<K, V,
|
|
187
|
-
* `
|
|
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>>`.
|
|
188
184
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
189
185
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
190
186
|
* @returns a boolean value.
|
|
191
187
|
*/
|
|
192
|
-
add(
|
|
188
|
+
add(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, value?: V): boolean;
|
|
193
189
|
/**
|
|
194
190
|
* Time Complexity: O(k log n)
|
|
195
191
|
* Space Complexity: O(k + log n)
|
|
@@ -211,34 +207,24 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
211
207
|
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
212
208
|
* successfully inserted into the data structure.
|
|
213
209
|
*/
|
|
214
|
-
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V,
|
|
215
|
-
/**
|
|
216
|
-
* Time Complexity: O(n)
|
|
217
|
-
* Space Complexity: O(1)
|
|
218
|
-
*
|
|
219
|
-
* The `merge` function overrides the base class method by adding elements from another
|
|
220
|
-
* binary search tree.
|
|
221
|
-
* @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
|
|
222
|
-
* value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
|
|
223
|
-
*/
|
|
224
|
-
merge(anotherTree: BST<K, V, R, NODE, TREE>): void;
|
|
210
|
+
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
225
211
|
/**
|
|
226
212
|
* Time Complexity: O(log n)
|
|
227
213
|
* Space Complexity: O(k + log n)
|
|
228
214
|
*
|
|
229
215
|
* The function `search` in TypeScript overrides the search behavior in a binary tree structure based
|
|
230
216
|
* on specified criteria.
|
|
231
|
-
* @param {BTNRep<K, V,
|
|
232
|
-
* `
|
|
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
|
|
233
219
|
* following types:
|
|
234
220
|
* @param [onlyOne=false] - The `onlyOne` parameter is a boolean flag that determines whether the
|
|
235
221
|
* search should stop after finding the first matching node. If `onlyOne` is set to `true`, the
|
|
236
222
|
* search will return as soon as a matching node is found. If `onlyOne` is set to `false`, the
|
|
237
223
|
* @param {C} callback - The `callback` parameter in the `override search` function is a function
|
|
238
224
|
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
239
|
-
* extends `NodeCallback<
|
|
225
|
+
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its
|
|
240
226
|
* argument and
|
|
241
|
-
* @param {BTNRep<K, V,
|
|
227
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search`
|
|
242
228
|
* method represents the node from which the search operation will begin. It is the starting point
|
|
243
229
|
* for searching within the tree data structure. The method ensures that the `startNode` is a valid
|
|
244
230
|
* node before proceeding with the search operation. If the `
|
|
@@ -250,15 +236,37 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
250
236
|
* structure based on the provided key, predicate, and other options. The search results are
|
|
251
237
|
* collected in an array and returned as the output of the method.
|
|
252
238
|
*/
|
|
253
|
-
search<C extends NodeCallback<
|
|
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>[];
|
|
254
240
|
/**
|
|
255
241
|
* Time Complexity: O(log n)
|
|
256
|
-
* Space Complexity: O(
|
|
242
|
+
* Space Complexity: O(k + log n)
|
|
243
|
+
*
|
|
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)
|
|
257
265
|
*
|
|
258
|
-
* This function retrieves a node based on a given
|
|
259
|
-
* @param {BTNRep<K, V,
|
|
260
|
-
* parameter can be of type `BTNRep<K, V,
|
|
261
|
-
* @param {
|
|
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
|
|
262
270
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
263
271
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
264
272
|
* node of the binary search tree.
|
|
@@ -266,12 +274,12 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
266
274
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
267
275
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
268
276
|
* no value is provided when calling the method.
|
|
269
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<
|
|
270
|
-
* 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
|
|
271
279
|
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
272
280
|
* returns the first node found or `undefined` if no node is found.
|
|
273
281
|
*/
|
|
274
|
-
getNode(
|
|
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>>;
|
|
275
283
|
/**
|
|
276
284
|
* Time complexity: O(n)
|
|
277
285
|
* Space complexity: O(n)
|
|
@@ -284,7 +292,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
284
292
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
285
293
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
286
294
|
* take one of the following values:
|
|
287
|
-
* @param {BTNRep<K, V,
|
|
295
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
288
296
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
289
297
|
* node entry. If not specified, the default value is the root of the tree.
|
|
290
298
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -292,7 +300,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
292
300
|
* following values:
|
|
293
301
|
* @returns The method is returning an array of the return type of the callback function.
|
|
294
302
|
*/
|
|
295
|
-
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>[];
|
|
296
304
|
/**
|
|
297
305
|
* Time complexity: O(n)
|
|
298
306
|
* Space complexity: O(n)
|
|
@@ -302,7 +310,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
302
310
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
303
311
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
304
312
|
* node being visited, and it can return a value of any type.
|
|
305
|
-
* @param {BTNRep<K, V,
|
|
313
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
306
314
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
307
315
|
* object. If no value is provided, the default value is the root of the tree.
|
|
308
316
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -310,7 +318,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
310
318
|
* the following values:
|
|
311
319
|
* @returns an array of the return type of the callback function.
|
|
312
320
|
*/
|
|
313
|
-
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>[];
|
|
314
322
|
/**
|
|
315
323
|
* Time complexity: O(n)
|
|
316
324
|
* Space complexity: O(n)
|
|
@@ -318,9 +326,9 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
318
326
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
319
327
|
* containing the results of the callback function applied to each level of the tree.
|
|
320
328
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
321
|
-
* `NodeCallback<
|
|
329
|
+
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the
|
|
322
330
|
* tree during the iteration process.
|
|
323
|
-
* @param {BTNRep<K, V,
|
|
331
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting
|
|
324
332
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
325
333
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
326
334
|
* value is provided, the root of
|
|
@@ -329,7 +337,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
329
337
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
330
338
|
* function.
|
|
331
339
|
*/
|
|
332
|
-
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>[][];
|
|
333
341
|
/**
|
|
334
342
|
* Time complexity: O(n)
|
|
335
343
|
* Space complexity: O(n)
|
|
@@ -342,7 +350,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
342
350
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
343
351
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
344
352
|
* 0, or 1, where:
|
|
345
|
-
* @param {BTNRep<K, V,
|
|
353
|
+
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in
|
|
346
354
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
347
355
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
348
356
|
* `targetNode` is provided,
|
|
@@ -351,7 +359,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
351
359
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
352
360
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
353
361
|
*/
|
|
354
|
-
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>[];
|
|
355
363
|
/**
|
|
356
364
|
* Time complexity: O(n)
|
|
357
365
|
* Space complexity: O(n)
|
|
@@ -378,24 +386,70 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
378
386
|
* @returns a boolean value.
|
|
379
387
|
*/
|
|
380
388
|
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
381
|
-
protected _comparator: Comparator<K>;
|
|
382
389
|
/**
|
|
383
|
-
*
|
|
384
|
-
*
|
|
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.
|
|
385
407
|
*/
|
|
386
|
-
|
|
387
|
-
|
|
408
|
+
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BSTOptions<MK, MV, MR>, thisArg?: any): BST<MK, MV, MR>;
|
|
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>;
|
|
388
418
|
/**
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
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.
|
|
392
429
|
*/
|
|
393
|
-
|
|
430
|
+
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, value?: V): [OptNode<BSTNode<K, V>>, V | undefined];
|
|
394
431
|
/**
|
|
432
|
+
* Time Complexity: O(1)
|
|
433
|
+
* Space Complexity: O(1)
|
|
434
|
+
*
|
|
395
435
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
396
436
|
* root.
|
|
397
|
-
* @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`.
|
|
398
453
|
*/
|
|
399
|
-
protected _setRoot(v: OptNode<NODE>): void;
|
|
400
454
|
protected _compare(a: K, b: K): number;
|
|
401
455
|
}
|