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