data-structure-typed 1.49.4 → 1.49.6
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 +15 -26
- package/README_zh-CN.md +1 -1
- package/benchmark/report.html +14 -23
- package/benchmark/report.json +163 -256
- package/dist/cjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +154 -143
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +211 -198
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/cjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -55
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +59 -94
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js +2 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/cjs/data-structures/matrix/index.js +0 -2
- package/dist/cjs/data-structures/matrix/index.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/cjs/data-structures/matrix/matrix.js +400 -15
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/cjs/data-structures/queue/deque.js +5 -7
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +3 -3
- package/dist/cjs/types/common.js +2 -2
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/cjs/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.js +6 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +57 -50
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +154 -143
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +212 -199
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/mjs/data-structures/binary-tree/bst.js +115 -90
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +64 -60
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -55
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +61 -95
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.js +4 -3
- package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/mjs/data-structures/hash/hash-map.js +2 -2
- package/dist/mjs/data-structures/heap/heap.js +2 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/mjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/mjs/data-structures/matrix/index.js +0 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/mjs/data-structures/matrix/matrix.js +399 -16
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/mjs/data-structures/queue/deque.js +7 -9
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +3 -3
- package/dist/mjs/types/common.js +2 -2
- package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/mjs/utils/utils.d.ts +1 -0
- package/dist/mjs/utils/utils.js +4 -0
- package/dist/umd/data-structure-typed.js +853 -943
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +4 -3
- package/src/data-structures/base/index.ts +1 -1
- package/src/data-structures/base/iterable-base.ts +7 -10
- package/src/data-structures/binary-tree/avl-tree.ts +73 -61
- package/src/data-structures/binary-tree/binary-tree.ts +301 -270
- package/src/data-structures/binary-tree/bst.ts +139 -115
- package/src/data-structures/binary-tree/rb-tree.ts +81 -73
- package/src/data-structures/binary-tree/tree-multimap.ts +72 -103
- package/src/data-structures/graph/abstract-graph.ts +13 -11
- package/src/data-structures/graph/directed-graph.ts +1 -3
- package/src/data-structures/graph/map-graph.ts +6 -1
- package/src/data-structures/graph/undirected-graph.ts +3 -6
- package/src/data-structures/hash/hash-map.ts +18 -16
- package/src/data-structures/heap/heap.ts +7 -10
- package/src/data-structures/heap/max-heap.ts +2 -1
- package/src/data-structures/heap/min-heap.ts +2 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/matrix/index.ts +0 -2
- package/src/data-structures/matrix/matrix.ts +442 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
- package/src/data-structures/queue/deque.ts +18 -39
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +9 -4
- package/src/types/common.ts +5 -5
- package/src/types/data-structures/base/base.ts +14 -3
- package/src/types/data-structures/base/index.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +4 -2
- package/src/types/data-structures/hash/hash-map.ts +3 -3
- package/src/types/data-structures/heap/heap.ts +2 -2
- package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/utils/utils.ts +7 -1
- package/test/integration/avl-tree.test.ts +18 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +6 -16
- package/test/performance/data-structures/comparison/comparison.test.ts +19 -47
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
- package/test/performance/data-structures/hash/hash-map.test.ts +14 -38
- package/test/performance/data-structures/heap/heap.test.ts +5 -18
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
- package/test/performance/data-structures/queue/deque.test.ts +6 -7
- package/test/performance/data-structures/queue/queue.test.ts +13 -29
- package/test/performance/data-structures/stack/stack.test.ts +6 -18
- package/test/performance/data-structures/trie/trie.test.ts +2 -6
- package/test/performance/reportor.ts +14 -15
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +65 -29
- package/test/unit/data-structures/binary-tree/bst.test.ts +69 -27
- package/test/unit/data-structures/binary-tree/overall.test.ts +16 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -6
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -21
- package/test/unit/data-structures/graph/directed-graph.test.ts +104 -45
- package/test/unit/data-structures/graph/undirected-graph.test.ts +57 -40
- package/test/unit/data-structures/hash/hash-map.test.ts +48 -28
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -4
- package/test/unit/data-structures/heap/heap.test.ts +0 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +10 -11
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -4
- package/test/unit/data-structures/matrix/matrix.test.ts +345 -52
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
- package/test/unit/data-structures/queue/deque.test.ts +0 -6
- package/test/unit/data-structures/queue/queue.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +3 -4
- package/test/unit/unrestricted-interconversion.test.ts +50 -44
- package/test/utils/performanc.ts +1 -1
- package/typedoc.json +30 -0
- package/dist/cjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/cjs/data-structures/matrix/matrix2d.js +0 -200
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/cjs/data-structures/matrix/vector2d.js +0 -291
- package/dist/cjs/data-structures/matrix/vector2d.js.map +0 -1
- package/dist/mjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/mjs/data-structures/matrix/matrix2d.js +0 -196
- package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/mjs/data-structures/matrix/vector2d.js +0 -289
- package/src/data-structures/matrix/matrix2d.ts +0 -211
- package/src/data-structures/matrix/vector2d.ts +0 -315
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
- package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -345
- package/test/unit/data-structures/matrix/vector2d.test.ts +0 -171
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BSTNested,
|
|
8
|
+
import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
9
|
import { BSTVariant, CP, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -45,13 +45,13 @@ export declare class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTN
|
|
|
45
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
|
-
* the tree with optional
|
|
49
|
-
* @param [
|
|
48
|
+
* the tree with optional nodes and options.
|
|
49
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
50
50
|
* binary search tree.
|
|
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(
|
|
54
|
+
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BSTOptions<K>>);
|
|
55
55
|
protected _root?: N;
|
|
56
56
|
get root(): N | undefined;
|
|
57
57
|
protected _variant: BSTVariant;
|
|
@@ -74,27 +74,53 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
74
74
|
*/
|
|
75
75
|
createTree(options?: Partial<BSTOptions<K>>): TREE;
|
|
76
76
|
/**
|
|
77
|
-
* The function
|
|
78
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
79
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
80
|
-
*/
|
|
81
|
-
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N;
|
|
82
|
-
/**
|
|
83
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
77
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
84
78
|
* otherwise it returns undefined.
|
|
85
|
-
* @param
|
|
79
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
86
80
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
87
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
81
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
88
82
|
* @returns a node of type N or undefined.
|
|
89
83
|
*/
|
|
90
|
-
exemplarToNode(
|
|
84
|
+
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined;
|
|
85
|
+
/**
|
|
86
|
+
* Time Complexity: O(log n)
|
|
87
|
+
* Space Complexity: O(log n)
|
|
88
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
89
|
+
*/
|
|
90
|
+
/**
|
|
91
|
+
* Time Complexity: O(log n)
|
|
92
|
+
* Space Complexity: O(log n)
|
|
93
|
+
*
|
|
94
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
95
|
+
* otherwise it returns the key itself.
|
|
96
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
97
|
+
* `undefined`.
|
|
98
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
99
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
100
|
+
* @returns either a node object (N) or undefined.
|
|
101
|
+
*/
|
|
102
|
+
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
105
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
106
|
+
* data type.
|
|
107
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
108
|
+
*/
|
|
109
|
+
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
110
|
+
/**
|
|
111
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
112
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
113
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
114
|
+
*/
|
|
115
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
|
|
91
116
|
/**
|
|
92
|
-
* Time Complexity: O(log n)
|
|
93
|
-
* Space Complexity: O(1)
|
|
117
|
+
* Time Complexity: O(log n)
|
|
118
|
+
* Space Complexity: O(1)
|
|
119
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
94
120
|
*/
|
|
95
121
|
/**
|
|
96
|
-
* Time Complexity: O(log n)
|
|
97
|
-
* Space Complexity: O(1)
|
|
122
|
+
* Time Complexity: O(log n)
|
|
123
|
+
* Space Complexity: O(1)
|
|
98
124
|
*
|
|
99
125
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
100
126
|
* or inserting a new node if the key is unique.
|
|
@@ -104,14 +130,15 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
104
130
|
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
105
131
|
* node was not added.
|
|
106
132
|
*/
|
|
107
|
-
add(keyOrNodeOrEntry:
|
|
133
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean;
|
|
108
134
|
/**
|
|
109
|
-
* Time Complexity: O(k log n)
|
|
110
|
-
* Space Complexity: O(k)
|
|
135
|
+
* Time Complexity: O(k log n)
|
|
136
|
+
* Space Complexity: O(k)
|
|
137
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
111
138
|
*/
|
|
112
139
|
/**
|
|
113
|
-
* Time Complexity: O(k log n)
|
|
114
|
-
* Space Complexity: O(k)
|
|
140
|
+
* Time Complexity: O(k log n)
|
|
141
|
+
* Space Complexity: O(k)
|
|
115
142
|
*
|
|
116
143
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
117
144
|
* balancing the tree after each addition.
|
|
@@ -122,41 +149,40 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
122
149
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
123
150
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
124
151
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
125
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
152
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
126
153
|
* in the order they appear in the input.
|
|
127
154
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
128
155
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
129
156
|
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
130
157
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
131
158
|
*/
|
|
132
|
-
addMany(keysOrNodesOrEntries: Iterable<
|
|
159
|
+
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
133
160
|
/**
|
|
134
|
-
* Time Complexity: O(n log n)
|
|
135
|
-
* Space Complexity: O(n)
|
|
161
|
+
* Time Complexity: O(n log n)
|
|
162
|
+
* Space Complexity: O(n)
|
|
163
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
136
164
|
*/
|
|
137
165
|
/**
|
|
138
|
-
* Time Complexity: O(log n)
|
|
139
|
-
* Space Complexity: O(
|
|
166
|
+
* Time Complexity: O(n log n)
|
|
167
|
+
* Space Complexity: O(n)
|
|
140
168
|
*
|
|
141
169
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
142
170
|
* leftmost node if the comparison result is greater than.
|
|
143
171
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
144
172
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
145
173
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
146
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
147
|
-
* be performed. It can have one of the following values:
|
|
148
174
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
149
175
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
150
176
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
151
177
|
*/
|
|
152
|
-
lastKey(beginRoot?:
|
|
178
|
+
lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, N>): K | undefined;
|
|
153
179
|
/**
|
|
154
|
-
* Time Complexity: O(log n)
|
|
155
|
-
* Space Complexity: O(1)
|
|
180
|
+
* Time Complexity: O(log n)
|
|
181
|
+
* Space Complexity: O(1)
|
|
156
182
|
*/
|
|
157
183
|
/**
|
|
158
|
-
* Time Complexity: O(log n)
|
|
159
|
-
* Space Complexity: O(
|
|
184
|
+
* Time Complexity: O(log n)
|
|
185
|
+
* Space Complexity: O(1)
|
|
160
186
|
*
|
|
161
187
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
162
188
|
* either recursive or iterative methods.
|
|
@@ -170,29 +196,14 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
170
196
|
*/
|
|
171
197
|
getNodeByKey(key: K, iterationType?: IterationType): N | undefined;
|
|
172
198
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
*
|
|
181
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
182
|
-
*/
|
|
183
|
-
/**
|
|
184
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
185
|
-
* otherwise it returns the key itself.
|
|
186
|
-
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
187
|
-
* `undefined`.
|
|
188
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
189
|
-
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
190
|
-
* @returns either a node object (N) or undefined.
|
|
191
|
-
*/
|
|
192
|
-
ensureNode(key: BSTNKeyOrNode<K, N>, iterationType?: IterationType): N | undefined;
|
|
193
|
-
/**
|
|
194
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
195
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
199
|
+
* Time Complexity: O(log n)
|
|
200
|
+
* Space Complexity: O(log n)
|
|
201
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
202
|
+
* /
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Time Complexity: O(log n)
|
|
206
|
+
* Space Complexity: O(log n)
|
|
196
207
|
*
|
|
197
208
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
198
209
|
* recursive or iterative approach.
|
|
@@ -213,14 +224,15 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
213
224
|
* performed on the binary tree. It can have two possible values:
|
|
214
225
|
* @returns The method returns an array of nodes (`N[]`).
|
|
215
226
|
*/
|
|
216
|
-
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?:
|
|
227
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N[];
|
|
217
228
|
/**
|
|
218
|
-
* Time Complexity: O(log n)
|
|
219
|
-
* Space Complexity: O(log n)
|
|
229
|
+
* Time Complexity: O(log n)
|
|
230
|
+
* Space Complexity: O(log n)
|
|
231
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
220
232
|
*/
|
|
221
233
|
/**
|
|
222
|
-
* Time Complexity: O(log n)
|
|
223
|
-
* Space Complexity: O(log n)
|
|
234
|
+
* Time Complexity: O(log n)
|
|
235
|
+
* Space Complexity: O(log n)
|
|
224
236
|
*
|
|
225
237
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
226
238
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -239,14 +251,14 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
239
251
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
240
252
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
241
253
|
*/
|
|
242
|
-
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?:
|
|
254
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): ReturnType<C>[];
|
|
243
255
|
/**
|
|
244
|
-
* Time Complexity: O(log n)
|
|
245
|
-
* Space Complexity: O(log n)
|
|
256
|
+
* Time Complexity: O(log n)
|
|
257
|
+
* Space Complexity: O(log n)
|
|
246
258
|
*/
|
|
247
259
|
/**
|
|
248
|
-
* Time Complexity: O(n)
|
|
249
|
-
* Space Complexity: O(n)
|
|
260
|
+
* Time Complexity: O(log n)
|
|
261
|
+
* Space Complexity: O(log n)
|
|
250
262
|
*
|
|
251
263
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
252
264
|
* ensures the tree is perfectly balanced.
|
|
@@ -270,8 +282,8 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
270
282
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
271
283
|
*/
|
|
272
284
|
/**
|
|
273
|
-
* Time Complexity: O(n)
|
|
274
|
-
* Space Complexity: O(log n)
|
|
285
|
+
* Time Complexity: O(n)
|
|
286
|
+
* Space Complexity: O(log n)
|
|
275
287
|
*
|
|
276
288
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
277
289
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -56,13 +56,13 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
56
56
|
export class BST extends BinaryTree {
|
|
57
57
|
/**
|
|
58
58
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
59
|
-
* the tree with optional
|
|
60
|
-
* @param [
|
|
59
|
+
* the tree with optional nodes and options.
|
|
60
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
61
61
|
* binary search tree.
|
|
62
62
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
63
63
|
* configuration options for the binary search tree. It can have the following properties:
|
|
64
64
|
*/
|
|
65
|
-
constructor(
|
|
65
|
+
constructor(nodes, options) {
|
|
66
66
|
super([], options);
|
|
67
67
|
if (options) {
|
|
68
68
|
const { variant } = options;
|
|
@@ -71,14 +71,14 @@ export class BST extends BinaryTree {
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
this._root = undefined;
|
|
74
|
-
if (
|
|
75
|
-
this.addMany(
|
|
74
|
+
if (nodes)
|
|
75
|
+
this.addMany(nodes);
|
|
76
76
|
}
|
|
77
77
|
_root;
|
|
78
78
|
get root() {
|
|
79
79
|
return this._root;
|
|
80
80
|
}
|
|
81
|
-
_variant = BSTVariant.
|
|
81
|
+
_variant = BSTVariant.STANDARD;
|
|
82
82
|
get variant() {
|
|
83
83
|
return this._variant;
|
|
84
84
|
}
|
|
@@ -103,35 +103,28 @@ export class BST extends BinaryTree {
|
|
|
103
103
|
createTree(options) {
|
|
104
104
|
return new BST([], {
|
|
105
105
|
iterationType: this.iterationType,
|
|
106
|
-
variant: this.variant,
|
|
106
|
+
variant: this.variant,
|
|
107
|
+
...options
|
|
107
108
|
});
|
|
108
109
|
}
|
|
109
110
|
/**
|
|
110
|
-
* The function
|
|
111
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
112
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
113
|
-
*/
|
|
114
|
-
isNode(exemplar) {
|
|
115
|
-
return exemplar instanceof BSTNode;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
111
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
119
112
|
* otherwise it returns undefined.
|
|
120
|
-
* @param
|
|
113
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
121
114
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
122
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
115
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
123
116
|
* @returns a node of type N or undefined.
|
|
124
117
|
*/
|
|
125
|
-
exemplarToNode(
|
|
118
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
126
119
|
let node;
|
|
127
|
-
if (
|
|
120
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
128
121
|
return;
|
|
129
122
|
}
|
|
130
|
-
else if (this.isNode(
|
|
131
|
-
node =
|
|
123
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
124
|
+
node = keyOrNodeOrEntry;
|
|
132
125
|
}
|
|
133
|
-
else if (this.isEntry(
|
|
134
|
-
const [key, value] =
|
|
126
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
127
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
135
128
|
if (key === undefined || key === null) {
|
|
136
129
|
return;
|
|
137
130
|
}
|
|
@@ -139,8 +132,8 @@ export class BST extends BinaryTree {
|
|
|
139
132
|
node = this.createNode(key, value);
|
|
140
133
|
}
|
|
141
134
|
}
|
|
142
|
-
else if (this.isNotNodeInstance(
|
|
143
|
-
node = this.createNode(
|
|
135
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
136
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
144
137
|
}
|
|
145
138
|
else {
|
|
146
139
|
return;
|
|
@@ -148,12 +141,62 @@ export class BST extends BinaryTree {
|
|
|
148
141
|
return node;
|
|
149
142
|
}
|
|
150
143
|
/**
|
|
151
|
-
* Time Complexity: O(log n)
|
|
152
|
-
* Space Complexity: O(
|
|
144
|
+
* Time Complexity: O(log n)
|
|
145
|
+
* Space Complexity: O(log n)
|
|
146
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
153
147
|
*/
|
|
154
148
|
/**
|
|
155
|
-
* Time Complexity: O(log n)
|
|
156
|
-
* Space Complexity: O(
|
|
149
|
+
* Time Complexity: O(log n)
|
|
150
|
+
* Space Complexity: O(log n)
|
|
151
|
+
*
|
|
152
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
153
|
+
* otherwise it returns the key itself.
|
|
154
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
155
|
+
* `undefined`.
|
|
156
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
157
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
158
|
+
* @returns either a node object (N) or undefined.
|
|
159
|
+
*/
|
|
160
|
+
ensureNode(keyOrNodeOrEntry, iterationType = IterationType.ITERATIVE) {
|
|
161
|
+
let res;
|
|
162
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
163
|
+
res = keyOrNodeOrEntry;
|
|
164
|
+
}
|
|
165
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
166
|
+
if (keyOrNodeOrEntry[0])
|
|
167
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
if (keyOrNodeOrEntry)
|
|
171
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
172
|
+
}
|
|
173
|
+
return res;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
177
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
178
|
+
* data type.
|
|
179
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
180
|
+
*/
|
|
181
|
+
isNotNodeInstance(potentialKey) {
|
|
182
|
+
return !(potentialKey instanceof BSTNode);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
186
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
187
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
188
|
+
*/
|
|
189
|
+
isNode(keyOrNodeOrEntry) {
|
|
190
|
+
return keyOrNodeOrEntry instanceof BSTNode;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Time Complexity: O(log n)
|
|
194
|
+
* Space Complexity: O(1)
|
|
195
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
196
|
+
*/
|
|
197
|
+
/**
|
|
198
|
+
* Time Complexity: O(log n)
|
|
199
|
+
* Space Complexity: O(1)
|
|
157
200
|
*
|
|
158
201
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
159
202
|
* or inserting a new node if the key is unique.
|
|
@@ -166,11 +209,11 @@ export class BST extends BinaryTree {
|
|
|
166
209
|
add(keyOrNodeOrEntry, value) {
|
|
167
210
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
168
211
|
if (newNode === undefined)
|
|
169
|
-
return;
|
|
212
|
+
return false;
|
|
170
213
|
if (this.root === undefined) {
|
|
171
214
|
this._setRoot(newNode);
|
|
172
215
|
this._size++;
|
|
173
|
-
return
|
|
216
|
+
return true;
|
|
174
217
|
}
|
|
175
218
|
let current = this.root;
|
|
176
219
|
while (current !== undefined) {
|
|
@@ -178,7 +221,7 @@ export class BST extends BinaryTree {
|
|
|
178
221
|
// if (current !== newNode) {
|
|
179
222
|
// The key value is the same but the reference is different, update the value of the existing node
|
|
180
223
|
this._replaceNode(current, newNode);
|
|
181
|
-
return
|
|
224
|
+
return true;
|
|
182
225
|
// } else {
|
|
183
226
|
// The key value is the same and the reference is the same, replace the entire node
|
|
184
227
|
// this._replaceNode(current, newNode);
|
|
@@ -190,7 +233,7 @@ export class BST extends BinaryTree {
|
|
|
190
233
|
current.left = newNode;
|
|
191
234
|
newNode.parent = current;
|
|
192
235
|
this._size++;
|
|
193
|
-
return
|
|
236
|
+
return true;
|
|
194
237
|
}
|
|
195
238
|
current = current.left;
|
|
196
239
|
}
|
|
@@ -199,20 +242,21 @@ export class BST extends BinaryTree {
|
|
|
199
242
|
current.right = newNode;
|
|
200
243
|
newNode.parent = current;
|
|
201
244
|
this._size++;
|
|
202
|
-
return
|
|
245
|
+
return true;
|
|
203
246
|
}
|
|
204
247
|
current = current.right;
|
|
205
248
|
}
|
|
206
249
|
}
|
|
207
|
-
return
|
|
250
|
+
return false;
|
|
208
251
|
}
|
|
209
252
|
/**
|
|
210
|
-
* Time Complexity: O(k log n)
|
|
211
|
-
* Space Complexity: O(k)
|
|
253
|
+
* Time Complexity: O(k log n)
|
|
254
|
+
* Space Complexity: O(k)
|
|
255
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
212
256
|
*/
|
|
213
257
|
/**
|
|
214
|
-
* Time Complexity: O(k log n)
|
|
215
|
-
* Space Complexity: O(k)
|
|
258
|
+
* Time Complexity: O(k log n)
|
|
259
|
+
* Space Complexity: O(k)
|
|
216
260
|
*
|
|
217
261
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
218
262
|
* balancing the tree after each addition.
|
|
@@ -223,7 +267,7 @@ export class BST extends BinaryTree {
|
|
|
223
267
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
224
268
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
225
269
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
226
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
270
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
227
271
|
* in the order they appear in the input.
|
|
228
272
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
229
273
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
@@ -305,20 +349,19 @@ export class BST extends BinaryTree {
|
|
|
305
349
|
return inserted;
|
|
306
350
|
}
|
|
307
351
|
/**
|
|
308
|
-
* Time Complexity: O(n log n)
|
|
309
|
-
* Space Complexity: O(n)
|
|
352
|
+
* Time Complexity: O(n log n)
|
|
353
|
+
* Space Complexity: O(n)
|
|
354
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
310
355
|
*/
|
|
311
356
|
/**
|
|
312
|
-
* Time Complexity: O(log n)
|
|
313
|
-
* Space Complexity: O(
|
|
357
|
+
* Time Complexity: O(n log n)
|
|
358
|
+
* Space Complexity: O(n)
|
|
314
359
|
*
|
|
315
360
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
316
361
|
* leftmost node if the comparison result is greater than.
|
|
317
362
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
318
363
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
319
364
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
320
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
321
|
-
* be performed. It can have one of the following values:
|
|
322
365
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
323
366
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
324
367
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
@@ -327,7 +370,7 @@ export class BST extends BinaryTree {
|
|
|
327
370
|
let current = this.ensureNode(beginRoot);
|
|
328
371
|
if (!current)
|
|
329
372
|
return undefined;
|
|
330
|
-
if (this._variant === BSTVariant.
|
|
373
|
+
if (this._variant === BSTVariant.STANDARD) {
|
|
331
374
|
// For BSTVariant.MIN, find the rightmost node
|
|
332
375
|
while (current.right !== undefined) {
|
|
333
376
|
current = current.right;
|
|
@@ -342,12 +385,12 @@ export class BST extends BinaryTree {
|
|
|
342
385
|
return current.key;
|
|
343
386
|
}
|
|
344
387
|
/**
|
|
345
|
-
* Time Complexity: O(log n)
|
|
346
|
-
* Space Complexity: O(1)
|
|
388
|
+
* Time Complexity: O(log n)
|
|
389
|
+
* Space Complexity: O(1)
|
|
347
390
|
*/
|
|
348
391
|
/**
|
|
349
|
-
* Time Complexity: O(log n)
|
|
350
|
-
* Space Complexity: O(
|
|
392
|
+
* Time Complexity: O(log n)
|
|
393
|
+
* Space Complexity: O(1)
|
|
351
394
|
*
|
|
352
395
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
353
396
|
* either recursive or iterative methods.
|
|
@@ -391,33 +434,14 @@ export class BST extends BinaryTree {
|
|
|
391
434
|
}
|
|
392
435
|
}
|
|
393
436
|
/**
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
404
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
405
|
-
*/
|
|
406
|
-
/**
|
|
407
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
408
|
-
* otherwise it returns the key itself.
|
|
409
|
-
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
410
|
-
* `undefined`.
|
|
411
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
412
|
-
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
413
|
-
* @returns either a node object (N) or undefined.
|
|
414
|
-
*/
|
|
415
|
-
ensureNode(key, iterationType = IterationType.ITERATIVE) {
|
|
416
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
417
|
-
}
|
|
418
|
-
/**
|
|
419
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
420
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
437
|
+
* Time Complexity: O(log n)
|
|
438
|
+
* Space Complexity: O(log n)
|
|
439
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
440
|
+
* /
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Time Complexity: O(log n)
|
|
444
|
+
* Space Complexity: O(log n)
|
|
421
445
|
*
|
|
422
446
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
423
447
|
* recursive or iterative approach.
|
|
@@ -495,12 +519,13 @@ export class BST extends BinaryTree {
|
|
|
495
519
|
return ans;
|
|
496
520
|
}
|
|
497
521
|
/**
|
|
498
|
-
* Time Complexity: O(log n)
|
|
499
|
-
* Space Complexity: O(log n)
|
|
522
|
+
* Time Complexity: O(log n)
|
|
523
|
+
* Space Complexity: O(log n)
|
|
524
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
500
525
|
*/
|
|
501
526
|
/**
|
|
502
|
-
* Time Complexity: O(log n)
|
|
503
|
-
* Space Complexity: O(log n)
|
|
527
|
+
* Time Complexity: O(log n)
|
|
528
|
+
* Space Complexity: O(log n)
|
|
504
529
|
*
|
|
505
530
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
506
531
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -560,12 +585,12 @@ export class BST extends BinaryTree {
|
|
|
560
585
|
}
|
|
561
586
|
}
|
|
562
587
|
/**
|
|
563
|
-
* Time Complexity: O(log n)
|
|
564
|
-
* Space Complexity: O(log n)
|
|
588
|
+
* Time Complexity: O(log n)
|
|
589
|
+
* Space Complexity: O(log n)
|
|
565
590
|
*/
|
|
566
591
|
/**
|
|
567
|
-
* Time Complexity: O(n)
|
|
568
|
-
* Space Complexity: O(n)
|
|
592
|
+
* Time Complexity: O(log n)
|
|
593
|
+
* Space Complexity: O(log n)
|
|
569
594
|
*
|
|
570
595
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
571
596
|
* ensures the tree is perfectly balanced.
|
|
@@ -624,8 +649,8 @@ export class BST extends BinaryTree {
|
|
|
624
649
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
625
650
|
*/
|
|
626
651
|
/**
|
|
627
|
-
* Time Complexity: O(n)
|
|
628
|
-
* Space Complexity: O(log n)
|
|
652
|
+
* Time Complexity: O(n)
|
|
653
|
+
* Space Complexity: O(log n)
|
|
629
654
|
*
|
|
630
655
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
631
656
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -694,7 +719,7 @@ export class BST extends BinaryTree {
|
|
|
694
719
|
_compare(a, b) {
|
|
695
720
|
const extractedA = this.extractor(a);
|
|
696
721
|
const extractedB = this.extractor(b);
|
|
697
|
-
const compared = this.variant === BSTVariant.
|
|
722
|
+
const compared = this.variant === BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA;
|
|
698
723
|
return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
|
|
699
724
|
}
|
|
700
725
|
}
|