data-structure-typed 1.51.8 → 1.52.0
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 +13 -13
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +152 -152
- package/dist/cjs/data-structures/base/index.d.ts +2 -1
- package/dist/cjs/data-structures/base/index.js +2 -1
- package/dist/cjs/data-structures/base/index.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/cjs/data-structures/base/iterable-element-base.js +226 -0
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -0
- package/dist/cjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/cjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +13 -190
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +318 -226
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +475 -363
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +192 -202
- package/dist/cjs/data-structures/binary-tree/bst.js +207 -249
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +33 -40
- package/dist/cjs/data-structures/hash/hash-map.js +40 -55
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +43 -114
- package/dist/cjs/data-structures/heap/heap.js +59 -127
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/cjs/data-structures/heap/max-heap.js +76 -10
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/cjs/data-structures/heap/min-heap.js +68 -11
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +21 -20
- package/dist/cjs/data-structures/queue/deque.js +29 -23
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +8 -28
- package/dist/cjs/data-structures/queue/queue.js +15 -31
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +17 -22
- package/dist/cjs/data-structures/stack/stack.js +25 -24
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +19 -14
- package/dist/cjs/data-structures/trie/trie.js +27 -16
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +7 -7
- package/dist/cjs/types/common.d.ts +1 -2
- package/dist/cjs/types/data-structures/base/base.d.ts +5 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +4 -5
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/cjs/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/cjs/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/cjs/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/cjs/utils/utils.js +3 -5
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/index.d.ts +2 -1
- package/dist/mjs/data-structures/base/index.js +2 -1
- package/dist/mjs/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/mjs/data-structures/base/iterable-element-base.js +222 -0
- package/dist/mjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/mjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +10 -186
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +318 -226
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +476 -364
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +192 -202
- package/dist/mjs/data-structures/binary-tree/bst.js +211 -255
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/mjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/mjs/data-structures/hash/hash-map.d.ts +33 -40
- package/dist/mjs/data-structures/hash/hash-map.js +41 -55
- package/dist/mjs/data-structures/heap/heap.d.ts +43 -114
- package/dist/mjs/data-structures/heap/heap.js +60 -128
- package/dist/mjs/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/mjs/data-structures/heap/max-heap.js +79 -10
- package/dist/mjs/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/mjs/data-structures/heap/min-heap.js +68 -11
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +82 -10
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +21 -20
- package/dist/mjs/data-structures/queue/deque.js +29 -23
- package/dist/mjs/data-structures/queue/queue.d.ts +8 -28
- package/dist/mjs/data-structures/queue/queue.js +15 -31
- package/dist/mjs/data-structures/stack/stack.d.ts +17 -22
- package/dist/mjs/data-structures/stack/stack.js +25 -24
- package/dist/mjs/data-structures/trie/trie.d.ts +19 -14
- package/dist/mjs/data-structures/trie/trie.js +27 -16
- package/dist/mjs/interfaces/binary-tree.d.ts +7 -7
- package/dist/mjs/types/common.d.ts +1 -2
- package/dist/mjs/types/data-structures/base/base.d.ts +5 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +4 -5
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/mjs/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/mjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/mjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/mjs/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/mjs/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/mjs/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/mjs/utils/utils.js +3 -5
- package/dist/umd/data-structure-typed.js +1949 -1481
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +144 -95
- package/src/data-structures/binary-tree/avl-tree.ts +96 -69
- package/src/data-structures/binary-tree/binary-tree.ts +535 -403
- package/src/data-structures/binary-tree/bst.ts +247 -277
- package/src/data-structures/binary-tree/rb-tree.ts +123 -103
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +46 -53
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +37 -26
- package/src/data-structures/queue/queue.ts +23 -36
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +35 -20
- package/src/interfaces/binary-tree.ts +9 -9
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/bst.ts +4 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +3 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/utils/utils.ts +3 -3
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +3 -3
- package/test/integration/index.html +25 -11
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +68 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +80 -0
- package/test/unit/data-structures/heap/max-heap.test.ts +44 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +18 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +42 -0
- package/test/unit/data-structures/queue/deque.test.ts +30 -0
- package/test/unit/data-structures/queue/queue.test.ts +56 -0
- package/test/utils/big-o.ts +12 -6
- package/tsconfig-base.json +1 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +0 -1
|
@@ -97,24 +97,27 @@ export class BinaryTreeNode {
|
|
|
97
97
|
export class BinaryTree extends IterableEntryBase {
|
|
98
98
|
iterationType = 'ITERATIVE';
|
|
99
99
|
/**
|
|
100
|
-
* The constructor function initializes a binary tree object with optional
|
|
101
|
-
* @param [
|
|
100
|
+
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
|
|
101
|
+
* @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of KeyOrNodeOrEntry objects. These objects represent the
|
|
102
102
|
* nodes to be added to the binary tree.
|
|
103
103
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
104
104
|
* configuration options for the binary tree. In this case, it is of type
|
|
105
105
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
106
106
|
* required.
|
|
107
107
|
*/
|
|
108
|
-
constructor(
|
|
108
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
109
109
|
super();
|
|
110
110
|
if (options) {
|
|
111
|
-
const { iterationType } = options;
|
|
111
|
+
const { iterationType, toEntryFn } = options;
|
|
112
112
|
if (iterationType)
|
|
113
113
|
this.iterationType = iterationType;
|
|
114
|
+
if (typeof toEntryFn === 'function')
|
|
115
|
+
this._toEntryFn = toEntryFn;
|
|
116
|
+
else if (toEntryFn)
|
|
117
|
+
throw TypeError('toEntryFn must be a function type');
|
|
114
118
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
this.addMany(keysOrNodesOrEntries);
|
|
119
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
120
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
118
121
|
}
|
|
119
122
|
_root;
|
|
120
123
|
/**
|
|
@@ -125,7 +128,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
125
128
|
get root() {
|
|
126
129
|
return this._root;
|
|
127
130
|
}
|
|
128
|
-
_size;
|
|
131
|
+
_size = 0;
|
|
129
132
|
/**
|
|
130
133
|
* The function returns the size of an object.
|
|
131
134
|
* @returns The size of the object, which is a number.
|
|
@@ -141,6 +144,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
141
144
|
get NIL() {
|
|
142
145
|
return this._NIL;
|
|
143
146
|
}
|
|
147
|
+
_toEntryFn;
|
|
148
|
+
/**
|
|
149
|
+
* The function returns the value of the _toEntryFn property.
|
|
150
|
+
* @returns The function being returned is `this._toEntryFn`.
|
|
151
|
+
*/
|
|
152
|
+
get toEntryFn() {
|
|
153
|
+
return this._toEntryFn;
|
|
154
|
+
}
|
|
144
155
|
/**
|
|
145
156
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
146
157
|
* @param {K} key - The key for the new node.
|
|
@@ -161,42 +172,42 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
161
172
|
return new BinaryTree([], { iterationType: this.iterationType, ...options });
|
|
162
173
|
}
|
|
163
174
|
/**
|
|
164
|
-
* The function `
|
|
165
|
-
*
|
|
175
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
|
|
176
|
+
* into a node object.
|
|
177
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
178
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
166
179
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
167
|
-
* `
|
|
168
|
-
*
|
|
169
|
-
* @returns
|
|
180
|
+
* `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
|
|
181
|
+
* key-value pair. If provided, it will be used to create a node with the specified key and value.
|
|
182
|
+
* @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
|
|
183
|
+
* or `undefined`.
|
|
170
184
|
*/
|
|
171
|
-
|
|
172
|
-
if (
|
|
185
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
186
|
+
if (keyOrNodeOrEntryOrRawElement === undefined)
|
|
173
187
|
return;
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const [key,
|
|
180
|
-
if (key
|
|
188
|
+
if (keyOrNodeOrEntryOrRawElement === null)
|
|
189
|
+
return null;
|
|
190
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
191
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
192
|
+
if (this.toEntryFn) {
|
|
193
|
+
const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
194
|
+
if (key)
|
|
195
|
+
return this.createNode(key, entryValue ?? value);
|
|
196
|
+
else
|
|
181
197
|
return;
|
|
182
|
-
}
|
|
183
|
-
else if (key === null) {
|
|
184
|
-
node = null;
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
node = this.createNode(key, value);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
191
|
-
node = keyOrNodeOrEntry;
|
|
192
|
-
}
|
|
193
|
-
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
194
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
195
198
|
}
|
|
196
|
-
|
|
197
|
-
|
|
199
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
200
|
+
const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
201
|
+
if (key === undefined)
|
|
202
|
+
return;
|
|
203
|
+
else if (key === null)
|
|
204
|
+
return null;
|
|
205
|
+
else
|
|
206
|
+
return this.createNode(key, value);
|
|
198
207
|
}
|
|
199
|
-
|
|
208
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
209
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value);
|
|
210
|
+
return;
|
|
200
211
|
}
|
|
201
212
|
/**
|
|
202
213
|
* Time Complexity: O(n)
|
|
@@ -206,56 +217,56 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
206
217
|
* Time Complexity: O(n)
|
|
207
218
|
* Space Complexity: O(log n)
|
|
208
219
|
*
|
|
209
|
-
* The
|
|
210
|
-
*
|
|
211
|
-
* @param {
|
|
212
|
-
* `
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*/
|
|
219
|
-
ensureNode(
|
|
220
|
-
if (
|
|
220
|
+
* The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
|
|
221
|
+
* node if it is a key or entry.
|
|
222
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
223
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
|
|
224
|
+
* a raw element.
|
|
225
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
226
|
+
* parameter that specifies the type of iteration to be used when searching for a node. It has a
|
|
227
|
+
* default value of `'ITERATIVE'`.
|
|
228
|
+
* @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
|
|
229
|
+
*/
|
|
230
|
+
ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
|
|
231
|
+
if (keyOrNodeOrEntryOrRawElement === null)
|
|
232
|
+
return null;
|
|
233
|
+
if (keyOrNodeOrEntryOrRawElement === undefined)
|
|
234
|
+
return;
|
|
235
|
+
if (keyOrNodeOrEntryOrRawElement === this.NIL)
|
|
221
236
|
return;
|
|
222
|
-
if (this.
|
|
223
|
-
return
|
|
237
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
238
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
239
|
+
if (this.toEntryFn) {
|
|
240
|
+
const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
241
|
+
if (key)
|
|
242
|
+
return this.getNodeByKey(key);
|
|
224
243
|
}
|
|
225
|
-
if (this.isEntry(
|
|
226
|
-
const key =
|
|
244
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
245
|
+
const key = keyOrNodeOrEntryOrRawElement[0];
|
|
227
246
|
if (key === null)
|
|
228
247
|
return null;
|
|
229
248
|
if (key === undefined)
|
|
230
249
|
return;
|
|
231
250
|
return this.getNodeByKey(key, iterationType);
|
|
232
251
|
}
|
|
233
|
-
if (
|
|
234
|
-
return
|
|
235
|
-
|
|
236
|
-
return;
|
|
237
|
-
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
252
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
253
|
+
return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
|
|
254
|
+
return;
|
|
238
255
|
}
|
|
239
256
|
/**
|
|
240
|
-
* The function checks if
|
|
241
|
-
* @param {
|
|
242
|
-
*
|
|
257
|
+
* The function checks if the input is an instance of the BinaryTreeNode class.
|
|
258
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
259
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
260
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
261
|
+
* an instance of the `BinaryTreeNode` class.
|
|
243
262
|
*/
|
|
244
|
-
|
|
245
|
-
return
|
|
263
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
264
|
+
return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
|
|
246
265
|
}
|
|
247
266
|
/**
|
|
248
|
-
* The function
|
|
249
|
-
* @param
|
|
250
|
-
*
|
|
251
|
-
*/
|
|
252
|
-
isNode(keyOrNodeOrEntry) {
|
|
253
|
-
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
257
|
-
* BinaryTreeNode and its key is not NaN.
|
|
258
|
-
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
267
|
+
* The function checks if a given node is a valid node in a binary search tree.
|
|
268
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
269
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
259
270
|
* @returns a boolean value.
|
|
260
271
|
*/
|
|
261
272
|
isRealNode(node) {
|
|
@@ -264,21 +275,64 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
264
275
|
return this.isNode(node);
|
|
265
276
|
}
|
|
266
277
|
/**
|
|
267
|
-
* The function checks if a given node is a
|
|
268
|
-
* @param {
|
|
278
|
+
* The function checks if a given node is a real node or null.
|
|
279
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
280
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
281
|
+
* @returns a boolean value.
|
|
282
|
+
*/
|
|
283
|
+
isNodeOrNull(node) {
|
|
284
|
+
return this.isRealNode(node) || node === null;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* The function checks if a given node is equal to the NIL value.
|
|
288
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
289
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
269
290
|
* @returns a boolean value.
|
|
270
291
|
*/
|
|
271
292
|
isNIL(node) {
|
|
272
293
|
return node === this.NIL;
|
|
273
294
|
}
|
|
274
295
|
/**
|
|
275
|
-
* The function checks if
|
|
276
|
-
*
|
|
277
|
-
*
|
|
296
|
+
* The function checks if the input is an array with two elements, indicating it is a binary tree
|
|
297
|
+
* node entry.
|
|
298
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
299
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
278
300
|
* @returns a boolean value.
|
|
279
301
|
*/
|
|
280
|
-
isEntry(
|
|
281
|
-
return Array.isArray(
|
|
302
|
+
isEntry(keyOrNodeOrEntryOrRawElement) {
|
|
303
|
+
return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* The function checks if a given value is a valid key by evaluating its type and value.
|
|
307
|
+
* @param {any} key - The `key` parameter can be of any type. It is the value that we want to check
|
|
308
|
+
* if it is a valid key.
|
|
309
|
+
* @param [isCheckValueOf=true] - The `isCheckValueOf` parameter is a boolean flag that determines
|
|
310
|
+
* whether the function should check the valueOf() method of an object when the key is of type
|
|
311
|
+
* 'object'. If `isCheckValueOf` is true, the function will recursively call itself with the value
|
|
312
|
+
* returned by key.valueOf().
|
|
313
|
+
* @returns a boolean value.
|
|
314
|
+
*/
|
|
315
|
+
isKey(key, isCheckValueOf = true) {
|
|
316
|
+
if (key === null)
|
|
317
|
+
return true;
|
|
318
|
+
const keyType = typeof key;
|
|
319
|
+
if (keyType === 'string' || keyType === 'bigint' || keyType === 'boolean')
|
|
320
|
+
return true;
|
|
321
|
+
if (keyType === 'number')
|
|
322
|
+
return !isNaN(key);
|
|
323
|
+
if (keyType === 'symbol' || keyType === 'undefined')
|
|
324
|
+
return false;
|
|
325
|
+
if (keyType === 'function')
|
|
326
|
+
return this.isKey(key());
|
|
327
|
+
if (keyType === 'object') {
|
|
328
|
+
if (typeof key.toString === 'function')
|
|
329
|
+
return true;
|
|
330
|
+
if (isCheckValueOf && typeof key.valueOf === 'function') {
|
|
331
|
+
this.isKey(key.valueOf(), false);
|
|
332
|
+
}
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
return false;
|
|
282
336
|
}
|
|
283
337
|
/**
|
|
284
338
|
* Time Complexity O(n)
|
|
@@ -288,14 +342,20 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
288
342
|
* Time Complexity O(n)
|
|
289
343
|
* Space Complexity O(1)
|
|
290
344
|
*
|
|
291
|
-
* The `add` function
|
|
292
|
-
*
|
|
293
|
-
* @param
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
345
|
+
* The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
|
|
346
|
+
* and finding the appropriate insertion position.
|
|
347
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
348
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
|
|
349
|
+
* node, entry, or raw element to be added to the tree. It can also accept a value of type
|
|
350
|
+
* `KeyOrNodeOrEntry<K, V, NODE>
|
|
351
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
352
|
+
* key being added to the tree. It represents the value that will be stored in the tree for the given
|
|
353
|
+
* key.
|
|
354
|
+
* @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
355
|
+
* insertion position cannot be found or if there are duplicate keys.
|
|
356
|
+
*/
|
|
357
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
358
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
299
359
|
if (newNode === undefined)
|
|
300
360
|
return false;
|
|
301
361
|
// If the tree is empty, directly set the new node as the root node
|
|
@@ -349,20 +409,24 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
349
409
|
* Time Complexity: O(k * n)
|
|
350
410
|
* Space Complexity: O(1)
|
|
351
411
|
*
|
|
352
|
-
* The `addMany` function takes in
|
|
353
|
-
* adds each node with its corresponding value to
|
|
354
|
-
*
|
|
355
|
-
* @param
|
|
356
|
-
*
|
|
357
|
-
|
|
358
|
-
|
|
412
|
+
* The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
|
|
413
|
+
* optional iterable of values, and adds each key or node or entry with its corresponding value to a
|
|
414
|
+
* data structure, returning an array of booleans indicating whether each insertion was successful.
|
|
415
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
416
|
+
* elements. These elements will be added to the data structure.
|
|
417
|
+
* @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
|
|
418
|
+
* in the `keysOrNodesOrEntriesOrRawElements` parameter.
|
|
419
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
420
|
+
* successfully added to the data structure.
|
|
421
|
+
*/
|
|
422
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values) {
|
|
359
423
|
// TODO not sure addMany not be run multi times
|
|
360
424
|
const inserted = [];
|
|
361
425
|
let valuesIterator;
|
|
362
426
|
if (values) {
|
|
363
427
|
valuesIterator = values[Symbol.iterator]();
|
|
364
428
|
}
|
|
365
|
-
for (const
|
|
429
|
+
for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
|
|
366
430
|
let value = undefined;
|
|
367
431
|
if (valuesIterator) {
|
|
368
432
|
const valueResult = valuesIterator.next();
|
|
@@ -370,7 +434,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
370
434
|
value = valueResult.value;
|
|
371
435
|
}
|
|
372
436
|
}
|
|
373
|
-
inserted.push(this.add(
|
|
437
|
+
inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
|
|
374
438
|
}
|
|
375
439
|
return inserted;
|
|
376
440
|
}
|
|
@@ -383,36 +447,34 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
383
447
|
* Time Complexity: O(k * n)
|
|
384
448
|
* Space Complexity: O(1)
|
|
385
449
|
*
|
|
386
|
-
* The `refill` function clears the current data and adds new
|
|
387
|
-
* @param
|
|
388
|
-
* KeyOrNodeOrEntry<K, V, NODE
|
|
389
|
-
* @param [values] - The `values` parameter is an optional iterable
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
refill(keysOrNodesOrEntries, values) {
|
|
450
|
+
* The `refill` function clears the current data and adds new data to the collection.
|
|
451
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
|
|
452
|
+
* elements. These can be of any type (R) or a specific type (KeyOrNodeOrEntry<K, V, NODE>).
|
|
453
|
+
* @param [values] - The `values` parameter is an optional iterable of values that will be associated
|
|
454
|
+
* with the keys or nodes being added. If provided, the values will be assigned to the corresponding
|
|
455
|
+
* keys or nodes. If not provided, the values will be set to `undefined`.
|
|
456
|
+
*/
|
|
457
|
+
refill(keysOrNodesOrEntriesOrRawElements, values) {
|
|
395
458
|
this.clear();
|
|
396
|
-
this.addMany(
|
|
459
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements, values);
|
|
397
460
|
}
|
|
398
461
|
/**
|
|
399
462
|
* Time Complexity: O(n)
|
|
400
463
|
* Space Complexity: O(1)
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
/**
|
|
464
|
+
*/
|
|
465
|
+
/**
|
|
404
466
|
* Time Complexity: O(n)
|
|
405
467
|
* Space Complexity: O(1)
|
|
406
468
|
*
|
|
407
|
-
* The function
|
|
408
|
-
*
|
|
409
|
-
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value
|
|
410
|
-
*
|
|
411
|
-
* the callback function
|
|
412
|
-
* specific node based on its value or object.
|
|
469
|
+
* The above function is a TypeScript implementation of deleting a node from a binary tree, returning
|
|
470
|
+
* the deleted node and the node that needs to be balanced.
|
|
471
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
472
|
+
* used to identify the node that needs to be deleted from the binary tree. It can be of any type
|
|
473
|
+
* that is returned by the callback function.
|
|
413
474
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
414
|
-
* identifier of the node to be deleted. It is
|
|
415
|
-
*
|
|
475
|
+
* identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
|
|
476
|
+
* interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
|
|
477
|
+
* type `NODE
|
|
416
478
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
417
479
|
*/
|
|
418
480
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
@@ -467,28 +529,27 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
467
529
|
*/
|
|
468
530
|
/**
|
|
469
531
|
* Time Complexity: O(n)
|
|
470
|
-
* Space Complexity: O(k + log n)
|
|
532
|
+
* Space Complexity: O(k + log n)
|
|
471
533
|
*
|
|
472
|
-
* The function `getNodes`
|
|
473
|
-
*
|
|
534
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
535
|
+
* recursive or iterative approach.
|
|
474
536
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
475
|
-
* that
|
|
476
|
-
* callback function
|
|
477
|
-
*
|
|
478
|
-
*
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
*
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
* @returns an array of nodes of type `NODE`.
|
|
537
|
+
* that is used to identify the nodes. It can be of any type and is used to match against the result
|
|
538
|
+
* of the callback function for each node.
|
|
539
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
540
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
541
|
+
* `callback` function is optional and defaults to a default callback function
|
|
542
|
+
* (`this._DEFAULT_CALLBACK`) if not provided.
|
|
543
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
|
|
544
|
+
* the identifier or all nodes that match the identifier. If set to true, only the first matching
|
|
545
|
+
* node will be returned. If set to false, all matching nodes will be returned. The default value is
|
|
546
|
+
* false.
|
|
547
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
548
|
+
* point for the search. It can be either a node object, a key-value pair, or a key. If it is not
|
|
549
|
+
* provided, the `root` of the data structure is used as the starting point.
|
|
550
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
551
|
+
* iteration to be performed on the nodes of a binary tree. It can have two possible values:
|
|
552
|
+
* @returns an array of NODE objects.
|
|
492
553
|
*/
|
|
493
554
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
494
555
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -533,24 +594,21 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
533
594
|
*/
|
|
534
595
|
/**
|
|
535
596
|
* Time Complexity: O(n)
|
|
536
|
-
* Space Complexity: O(log n)
|
|
597
|
+
* Space Complexity: O(log n).
|
|
537
598
|
*
|
|
538
|
-
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
539
|
-
*
|
|
599
|
+
* The function `getNode` returns the first node that matches the given identifier and callback,
|
|
600
|
+
* starting from the specified root node and using the specified iteration type.
|
|
540
601
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
541
|
-
* used to identify the node you want to retrieve. It can be of any type that is
|
|
542
|
-
* callback function
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
*
|
|
550
|
-
* @
|
|
551
|
-
* be performed when searching for nodes in the binary tree. It determines the order in which the
|
|
552
|
-
* nodes are visited during the search.
|
|
553
|
-
* @returns a value of type `NODE | null | undefined`.
|
|
602
|
+
* used to identify the node you want to retrieve. It can be of any type that is the return type of
|
|
603
|
+
* the `C` callback function, or it can be `null` or `undefined`.
|
|
604
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
605
|
+
* node matches the desired criteria. It should return a value that can be used to identify the node.
|
|
606
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
607
|
+
* point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
|
|
608
|
+
* a node entry. If not provided, the search will start from the root of the tree.
|
|
609
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
610
|
+
* of iteration to be performed when searching for nodes. It can have one of the following values:
|
|
611
|
+
* @returns The method is returning a NODE object, or null, or undefined.
|
|
554
612
|
*/
|
|
555
613
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
556
614
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
@@ -563,15 +621,13 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
563
621
|
* Time Complexity: O(n)
|
|
564
622
|
* Space Complexity: O(log n)
|
|
565
623
|
*
|
|
566
|
-
* The function `getNodeByKey`
|
|
567
|
-
*
|
|
568
|
-
*
|
|
569
|
-
*
|
|
570
|
-
*
|
|
571
|
-
*
|
|
572
|
-
*
|
|
573
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
574
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
624
|
+
* The function `getNodeByKey` returns a node with a specific key value from a tree structure.
|
|
625
|
+
* @param {K} key - The key parameter is the value that you want to search for in the tree. It is
|
|
626
|
+
* used to find the node with the matching key value.
|
|
627
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
628
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
629
|
+
* It has a default value of `'ITERATIVE'`.
|
|
630
|
+
* @returns a value of type NODE, null, or undefined.
|
|
575
631
|
*/
|
|
576
632
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
577
633
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
@@ -584,51 +640,49 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
584
640
|
* Time Complexity: O(n)
|
|
585
641
|
* Space Complexity: O(log n)
|
|
586
642
|
*
|
|
587
|
-
* The function `get`
|
|
588
|
-
*
|
|
643
|
+
* The function `get` in TypeScript overrides the base class method and returns the value associated
|
|
644
|
+
* with the given identifier.
|
|
589
645
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
590
|
-
* used to identify the node in the binary tree. It can be of any type that is
|
|
646
|
+
* used to identify the node in the binary tree. It can be of any type that is returned by the
|
|
591
647
|
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
592
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
593
|
-
* the
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
*
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
* found, `undefined` is returned.
|
|
648
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
649
|
+
* node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
650
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
651
|
+
* point for the search in the binary tree. It can be either a root node of the tree or a key, node,
|
|
652
|
+
* or entry object that exists in the tree. If no specific starting point is provided, the search
|
|
653
|
+
* will begin from the root of the
|
|
654
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
655
|
+
* of iteration to be performed when searching for a node in the tree. It can have one of the
|
|
656
|
+
* following values:
|
|
657
|
+
* @returns The method is returning the value associated with the specified identifier in the binary
|
|
658
|
+
* tree.
|
|
604
659
|
*/
|
|
605
660
|
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
606
661
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
|
|
607
662
|
}
|
|
608
663
|
/**
|
|
609
664
|
* Time Complexity: O(n)
|
|
610
|
-
* Space Complexity: O(log n)
|
|
665
|
+
* Space Complexity: O(log n)
|
|
611
666
|
*/
|
|
612
667
|
/**
|
|
613
668
|
* Time Complexity: O(n)
|
|
614
|
-
* Space Complexity: O(log n)
|
|
669
|
+
* Space Complexity: O(log n)
|
|
615
670
|
*
|
|
616
|
-
* The function checks if a
|
|
671
|
+
* The `has` function checks if a given identifier exists in the data structure and returns a boolean
|
|
672
|
+
* value.
|
|
617
673
|
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
618
|
-
*
|
|
619
|
-
* callback function `C`. It can also be `null` or `undefined` if
|
|
620
|
-
*
|
|
621
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
622
|
-
*
|
|
623
|
-
*
|
|
624
|
-
*
|
|
625
|
-
*
|
|
626
|
-
*
|
|
627
|
-
*
|
|
628
|
-
*
|
|
629
|
-
*
|
|
630
|
-
* be performed in a pre-order, in-order, or post-order manner.
|
|
631
|
-
* @returns a boolean value.
|
|
674
|
+
* used to identify a specific node or entry in the data structure. It can be of any type that is
|
|
675
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if no specific
|
|
676
|
+
* identifier is provided.
|
|
677
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine
|
|
678
|
+
* whether a node should be included in the result or not. It is of type `C`, which extends the
|
|
679
|
+
* `BTNCallback<NODE>` type.
|
|
680
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
681
|
+
* point for the iteration in the data structure. It can be either a root node, a key-value pair, or
|
|
682
|
+
* a node entry. If not specified, it defaults to the root of the data structure.
|
|
683
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
684
|
+
* of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
|
|
685
|
+
* @returns The method is returning a boolean value.
|
|
632
686
|
*/
|
|
633
687
|
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
634
688
|
callback = this._ensureCallback(identifier, callback);
|
|
@@ -672,9 +726,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
672
726
|
*
|
|
673
727
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
674
728
|
* height of the tree.
|
|
675
|
-
* @param {
|
|
676
|
-
*
|
|
677
|
-
*
|
|
729
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
|
|
730
|
+
* has a default value of `this.root`. It represents the starting point for checking if the tree is
|
|
731
|
+
* perfectly balanced. It can be either a root node (`R`), a key or node or entry
|
|
732
|
+
* (`KeyOrNodeOrEntry<K, V, NODE
|
|
678
733
|
* @returns a boolean value.
|
|
679
734
|
*/
|
|
680
735
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
@@ -688,12 +743,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
688
743
|
* Time Complexity: O(n)
|
|
689
744
|
* Space Complexity: O(1)
|
|
690
745
|
*
|
|
691
|
-
* The function `
|
|
692
|
-
* @param {
|
|
693
|
-
*
|
|
694
|
-
*
|
|
695
|
-
*
|
|
696
|
-
*
|
|
746
|
+
* The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
|
|
747
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
748
|
+
* starting point for checking if a binary search tree (BST) is valid. It can be either a root node
|
|
749
|
+
* of the BST, a key value of a node in the BST, or an entry object containing both the key and value
|
|
750
|
+
* of a node in the BST
|
|
751
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
752
|
+
* of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
|
|
753
|
+
* two possible values:
|
|
697
754
|
* @returns a boolean value.
|
|
698
755
|
*/
|
|
699
756
|
isBST(beginRoot = this.root, iterationType = this.iterationType) {
|
|
@@ -746,14 +803,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
746
803
|
* Time Complexity: O(n)
|
|
747
804
|
* Space Complexity: O(1)
|
|
748
805
|
*
|
|
749
|
-
* The function calculates the depth of a given node in a
|
|
750
|
-
* @param {
|
|
751
|
-
*
|
|
752
|
-
*
|
|
753
|
-
* @param {
|
|
754
|
-
* from which
|
|
755
|
-
*
|
|
756
|
-
*
|
|
806
|
+
* The function calculates the depth of a given node or key in a tree-like data structure.
|
|
807
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
|
|
808
|
+
* (representing a root node), or a `KeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
|
|
809
|
+
* entry).
|
|
810
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
|
|
811
|
+
* represents the starting point from which to calculate the depth. It can be either a reference to a
|
|
812
|
+
* node in the tree or a key-value pair or an entry object. If not provided, the default value is
|
|
813
|
+
* `this.root`, which refers to the root node
|
|
814
|
+
* @returns the depth of a node in a tree structure.
|
|
757
815
|
*/
|
|
758
816
|
getDepth(dist, beginRoot = this.root) {
|
|
759
817
|
let distEnsured = this.ensureNode(dist);
|
|
@@ -774,17 +832,16 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
774
832
|
*/
|
|
775
833
|
/**
|
|
776
834
|
* Time Complexity: O(n)
|
|
777
|
-
* Space Complexity: O(
|
|
835
|
+
* Space Complexity: O(1)
|
|
778
836
|
*
|
|
779
|
-
* The
|
|
780
|
-
* iterative
|
|
781
|
-
* @param {
|
|
782
|
-
* starting
|
|
783
|
-
*
|
|
784
|
-
* @param iterationType - The `iterationType` parameter
|
|
785
|
-
*
|
|
786
|
-
*
|
|
787
|
-
* @returns the height of the binary tree.
|
|
837
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
838
|
+
* or iterative approach.
|
|
839
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
840
|
+
* starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
|
|
841
|
+
* node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
|
|
842
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
843
|
+
* iteration used to calculate the height of the tree. It can have two possible values:
|
|
844
|
+
* @returns the maximum height of the binary tree.
|
|
788
845
|
*/
|
|
789
846
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
790
847
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -824,12 +881,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
824
881
|
*
|
|
825
882
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
826
883
|
* recursive or iterative approach.
|
|
827
|
-
* @param {
|
|
828
|
-
* starting
|
|
829
|
-
*
|
|
830
|
-
*
|
|
831
|
-
*
|
|
832
|
-
*
|
|
884
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
885
|
+
* starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
|
|
886
|
+
* key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
|
|
887
|
+
* tree.
|
|
888
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
889
|
+
* iteration to be used when calculating the minimum height of the tree. It can have two possible
|
|
890
|
+
* values:
|
|
891
|
+
* @returns The function `getMinHeight` returns a number, which represents the minimum height of the
|
|
892
|
+
* binary tree.
|
|
833
893
|
*/
|
|
834
894
|
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
835
895
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -878,31 +938,27 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
878
938
|
/**
|
|
879
939
|
* Time Complexity: O(log n)
|
|
880
940
|
* Space Complexity: O(log n)
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
/**
|
|
941
|
+
*/
|
|
942
|
+
/**
|
|
884
943
|
* Time Complexity: O(log n)
|
|
885
944
|
* Space Complexity: O(log n)
|
|
886
945
|
*
|
|
887
|
-
* The function `getPathToRoot` returns an array of nodes from a given node
|
|
888
|
-
*
|
|
889
|
-
* @param {
|
|
890
|
-
*
|
|
891
|
-
* `null`, or `undefined`.
|
|
946
|
+
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
947
|
+
* up to the root node, with an option to reverse the order of the nodes.
|
|
948
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
|
|
949
|
+
* type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
892
950
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
893
951
|
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
894
|
-
* reversed before returning it. If `isReverse` is set to `false
|
|
895
|
-
* @returns The function `getPathToRoot` returns an array of
|
|
952
|
+
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
953
|
+
* @returns The function `getPathToRoot` returns an array of `NODE` objects.
|
|
896
954
|
*/
|
|
897
955
|
getPathToRoot(beginNode, isReverse = true) {
|
|
898
|
-
// TODO to support get path through passing key
|
|
899
956
|
const result = [];
|
|
900
957
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
901
958
|
if (!beginNodeEnsured)
|
|
902
959
|
return result;
|
|
903
960
|
while (beginNodeEnsured.parent) {
|
|
904
961
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
905
|
-
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
906
962
|
result.push(beginNodeEnsured);
|
|
907
963
|
beginNodeEnsured = beginNodeEnsured.parent;
|
|
908
964
|
}
|
|
@@ -917,15 +973,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
917
973
|
* Time Complexity: O(log n)
|
|
918
974
|
* Space Complexity: O(1)
|
|
919
975
|
*
|
|
920
|
-
* The
|
|
921
|
-
*
|
|
922
|
-
* @param {
|
|
923
|
-
* for finding the leftmost node in a binary tree. It can be either a `
|
|
924
|
-
*
|
|
925
|
-
* @param iterationType - The `iterationType` parameter is used to
|
|
926
|
-
*
|
|
927
|
-
* @returns The function `getLeftMost` returns the leftmost node
|
|
928
|
-
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
976
|
+
* The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
|
|
977
|
+
* iterative traversal.
|
|
978
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
979
|
+
* starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
|
|
980
|
+
* a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
981
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
982
|
+
* of iteration to be performed. It can have two possible values:
|
|
983
|
+
* @returns The function `getLeftMost` returns the leftmost node in a binary tree.
|
|
929
984
|
*/
|
|
930
985
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
931
986
|
if (this.isNIL(beginRoot))
|
|
@@ -959,16 +1014,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
959
1014
|
* Time Complexity: O(log n)
|
|
960
1015
|
* Space Complexity: O(1)
|
|
961
1016
|
*
|
|
962
|
-
* The
|
|
1017
|
+
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
|
|
963
1018
|
* iteratively.
|
|
964
|
-
* @param {
|
|
965
|
-
* starting
|
|
966
|
-
* `
|
|
967
|
-
*
|
|
968
|
-
*
|
|
969
|
-
*
|
|
970
|
-
* @returns The function `getRightMost` returns
|
|
971
|
-
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
1019
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1020
|
+
* starting point for finding the rightmost node in a binary tree. It can be either a root node
|
|
1021
|
+
* (`R`), a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
1022
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
1023
|
+
* of iteration to be performed when finding the rightmost node in a binary tree. It can have two
|
|
1024
|
+
* possible values:
|
|
1025
|
+
* @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
|
|
972
1026
|
*/
|
|
973
1027
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
974
1028
|
if (this.isNIL(beginRoot))
|
|
@@ -1003,10 +1057,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1003
1057
|
* Time Complexity: O(log n)
|
|
1004
1058
|
* Space Complexity: O(1)
|
|
1005
1059
|
*
|
|
1006
|
-
* The function returns the predecessor of a given node in a tree.
|
|
1007
|
-
* @param {NODE} node - The parameter
|
|
1060
|
+
* The function returns the predecessor node of a given node in a binary tree.
|
|
1061
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
|
|
1008
1062
|
* tree.
|
|
1009
|
-
* @returns the predecessor of the given
|
|
1063
|
+
* @returns the predecessor node of the given node.
|
|
1010
1064
|
*/
|
|
1011
1065
|
getPredecessor(node) {
|
|
1012
1066
|
if (this.isRealNode(node.left)) {
|
|
@@ -1032,8 +1086,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1032
1086
|
*
|
|
1033
1087
|
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
1034
1088
|
* @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
|
|
1035
|
-
* @returns
|
|
1036
|
-
*
|
|
1089
|
+
* @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
|
|
1090
|
+
* there is no successor, and `undefined` if the input `x` is not a valid node.
|
|
1037
1091
|
*/
|
|
1038
1092
|
getSuccessor(x) {
|
|
1039
1093
|
x = this.ensureNode(x);
|
|
@@ -1052,30 +1106,29 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1052
1106
|
/**
|
|
1053
1107
|
* Time complexity: O(n)
|
|
1054
1108
|
* Space complexity: O(n)
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
/**
|
|
1109
|
+
*/
|
|
1110
|
+
/**
|
|
1058
1111
|
* Time complexity: O(n)
|
|
1059
1112
|
* Space complexity: O(n)
|
|
1060
1113
|
*
|
|
1061
|
-
* The `dfs` function performs a depth-first search traversal on a binary tree
|
|
1062
|
-
*
|
|
1063
|
-
* callback function
|
|
1064
|
-
*
|
|
1065
|
-
*
|
|
1066
|
-
*
|
|
1067
|
-
*
|
|
1068
|
-
*
|
|
1069
|
-
*
|
|
1070
|
-
*
|
|
1071
|
-
*
|
|
1072
|
-
* @param {IterationType} iterationType - The `iterationType` parameter determines the
|
|
1073
|
-
* iteration to use
|
|
1114
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
1115
|
+
* function on each node according to a specified pattern and iteration type.
|
|
1116
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
1117
|
+
* visited during the depth-first search. It takes a node as an argument and returns a value. The
|
|
1118
|
+
* return type of the callback function is determined by the generic type `C`.
|
|
1119
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
|
|
1120
|
+
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
1121
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1122
|
+
* point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
|
|
1123
|
+
* is a key or key-value pair, the method will find the corresponding node in the tree and start the
|
|
1124
|
+
* search from there.
|
|
1125
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
|
|
1126
|
+
* type of iteration to use during the depth-first search. It can have two possible values:
|
|
1074
1127
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1075
|
-
* whether
|
|
1076
|
-
* `true`, null
|
|
1077
|
-
*
|
|
1078
|
-
* @returns an array of
|
|
1128
|
+
* whether or not to include null values in the depth-first search traversal. If `includeNull` is set
|
|
1129
|
+
* to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
|
|
1130
|
+
* values will
|
|
1131
|
+
* @returns an array of the return types of the callback function.
|
|
1079
1132
|
*/
|
|
1080
1133
|
dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1081
1134
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1191,22 +1244,23 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1191
1244
|
* Time complexity: O(n)
|
|
1192
1245
|
* Space complexity: O(n)
|
|
1193
1246
|
*
|
|
1194
|
-
* The `bfs` function performs a breadth-first search
|
|
1195
|
-
*
|
|
1247
|
+
* The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
|
|
1248
|
+
* on each node and returning an array of the results.
|
|
1196
1249
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1197
|
-
* the breadth-first search traversal. It takes a single
|
|
1250
|
+
* the breadth-first search traversal. It takes a single argument, which is the current node being
|
|
1198
1251
|
* visited, and returns a value of any type.
|
|
1199
|
-
* @param {
|
|
1200
|
-
* starting
|
|
1201
|
-
* or
|
|
1202
|
-
*
|
|
1203
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
1204
|
-
*
|
|
1205
|
-
* @param [includeNull=false] - The `includeNull` parameter is a boolean
|
|
1206
|
-
* to include null values in the breadth-first search traversal. If
|
|
1207
|
-
* `true`, null values will be included in the traversal
|
|
1208
|
-
*
|
|
1209
|
-
*
|
|
1252
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1253
|
+
* starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
|
|
1254
|
+
* or entry object. If no value is provided, the `root` property of the class is used as the default
|
|
1255
|
+
* starting point.
|
|
1256
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1257
|
+
* iteration to be performed. It can have two possible values:
|
|
1258
|
+
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1259
|
+
* whether or not to include null values in the breadth-first search (BFS) traversal. If
|
|
1260
|
+
* `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
|
|
1261
|
+
* set to `false
|
|
1262
|
+
* @returns The function `bfs` returns an array of values that are the result of invoking the
|
|
1263
|
+
* `callback` function on each node in the breadth-first order traversal of the binary tree.
|
|
1210
1264
|
*/
|
|
1211
1265
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1212
1266
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1269,18 +1323,18 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1269
1323
|
* Space complexity: O(n)
|
|
1270
1324
|
*
|
|
1271
1325
|
* The `listLevels` function returns an array of arrays, where each inner array represents a level in
|
|
1272
|
-
* a binary tree and contains the
|
|
1273
|
-
* level.
|
|
1326
|
+
* a binary tree and contains the results of applying a callback function to the nodes at that level.
|
|
1274
1327
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1275
|
-
* the tree. It takes a
|
|
1276
|
-
*
|
|
1277
|
-
* @param {
|
|
1278
|
-
* starting
|
|
1279
|
-
*
|
|
1280
|
-
*
|
|
1281
|
-
*
|
|
1328
|
+
* the tree. It takes a node as an argument and returns a value. The return type of the callback
|
|
1329
|
+
* function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
|
|
1330
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1331
|
+
* starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
|
|
1332
|
+
* entry. If no value is provided, the `root` property of the class is used as the default starting
|
|
1333
|
+
* point.
|
|
1334
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1335
|
+
* iteration to be performed on the binary tree. It can have two possible values:
|
|
1282
1336
|
* @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1283
|
-
* whether to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1337
|
+
* whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
|
|
1284
1338
|
* null values will be included in the levels. If `includeNull` is set to `false`, null values will
|
|
1285
1339
|
* be excluded
|
|
1286
1340
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
@@ -1345,17 +1399,17 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1345
1399
|
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
|
|
1346
1400
|
* algorithm.
|
|
1347
1401
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1348
|
-
* the tree. It takes a single
|
|
1349
|
-
*
|
|
1350
|
-
*
|
|
1351
|
-
*
|
|
1402
|
+
* the tree. It takes a single argument, which is the current node, and can return any value. The
|
|
1403
|
+
* return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
|
|
1404
|
+
* the return
|
|
1405
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
|
|
1406
|
+
* to specify the order in which the nodes of a binary tree are traversed. It can take one of the
|
|
1352
1407
|
* following values:
|
|
1353
|
-
* @param {
|
|
1354
|
-
* for the traversal. It can be
|
|
1355
|
-
* the root of the tree
|
|
1356
|
-
* @returns The function `morris` returns an array of values that are the
|
|
1357
|
-
*
|
|
1358
|
-
* by the return type of the `callback` function.
|
|
1408
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1409
|
+
* point for the traversal. It can be either a node object, a key, or an entry object. If no value is
|
|
1410
|
+
* provided, the `root` of the tree is used as the starting point.
|
|
1411
|
+
* @returns The function `morris` returns an array of values that are the return values of the
|
|
1412
|
+
* callback function `callback`.
|
|
1359
1413
|
*/
|
|
1360
1414
|
morris(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root) {
|
|
1361
1415
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1450,8 +1504,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1450
1504
|
* Time complexity: O(n)
|
|
1451
1505
|
* Space complexity: O(n)
|
|
1452
1506
|
*
|
|
1453
|
-
* The `clone` function creates a
|
|
1454
|
-
* the new tree.
|
|
1507
|
+
* The `clone` function creates a deep copy of a tree object.
|
|
1455
1508
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
1456
1509
|
*/
|
|
1457
1510
|
clone() {
|
|
@@ -1472,16 +1525,16 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1472
1525
|
* Time Complexity: O(n)
|
|
1473
1526
|
* Space Complexity: O(n)
|
|
1474
1527
|
*
|
|
1475
|
-
* The `filter` function creates a new tree
|
|
1476
|
-
*
|
|
1477
|
-
*
|
|
1478
|
-
*
|
|
1479
|
-
*
|
|
1480
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1481
|
-
*
|
|
1482
|
-
*
|
|
1483
|
-
* @returns The `filter` method is returning a new tree object that contains the
|
|
1484
|
-
*
|
|
1528
|
+
* The `filter` function creates a new tree with entries that pass a given predicate function.
|
|
1529
|
+
* @param predicate - The `predicate` parameter is a callback function that is used to test each
|
|
1530
|
+
* element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
|
|
1531
|
+
* represents the value of the current element being processed, the `key` argument represents the key
|
|
1532
|
+
* of the
|
|
1533
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1534
|
+
* specify the value of `this` within the `predicate` function. When the `predicate` function is
|
|
1535
|
+
* called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
|
|
1536
|
+
* @returns The `filter` method is returning a new tree object that contains the entries that pass
|
|
1537
|
+
* the given predicate function.
|
|
1485
1538
|
*/
|
|
1486
1539
|
filter(predicate, thisArg) {
|
|
1487
1540
|
const newTree = this.createTree();
|
|
@@ -1501,15 +1554,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1501
1554
|
* Time Complexity: O(n)
|
|
1502
1555
|
* Space Complexity: O(n)
|
|
1503
1556
|
*
|
|
1504
|
-
* The `map` function creates a new tree by applying a callback function to each
|
|
1505
|
-
*
|
|
1506
|
-
* @param callback - The callback parameter is a function that will be called for each
|
|
1507
|
-
*
|
|
1508
|
-
* the
|
|
1509
|
-
*
|
|
1510
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that
|
|
1511
|
-
*
|
|
1512
|
-
*
|
|
1557
|
+
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
1558
|
+
* tree.
|
|
1559
|
+
* @param callback - The callback parameter is a function that will be called for each entry in the
|
|
1560
|
+
* tree. It takes three arguments: value, key, and index. The value argument represents the value of
|
|
1561
|
+
* the current entry, the key argument represents the key of the current entry, and the index
|
|
1562
|
+
* argument represents the index of the
|
|
1563
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
1564
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
1565
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
1513
1566
|
* @returns The `map` method is returning a new tree object.
|
|
1514
1567
|
*/
|
|
1515
1568
|
map(callback, thisArg) {
|
|
@@ -1537,11 +1590,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1537
1590
|
* Time Complexity: O(n)
|
|
1538
1591
|
* Space Complexity: O(n)
|
|
1539
1592
|
*
|
|
1540
|
-
* The `print` function
|
|
1541
|
-
* @param {
|
|
1542
|
-
*
|
|
1543
|
-
*
|
|
1544
|
-
*
|
|
1593
|
+
* The `print` function in TypeScript prints the binary tree structure with customizable options.
|
|
1594
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1595
|
+
* point for printing the binary tree. It can be either a node of the binary tree or a key or entry
|
|
1596
|
+
* that exists in the binary tree. If no value is provided, the root of the binary tree will be used
|
|
1597
|
+
* as the starting point.
|
|
1598
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
|
|
1599
|
+
* allows you to customize the printing behavior. It has the following properties:
|
|
1600
|
+
* @returns Nothing is being returned. The function has a return type of `void`, which means it does
|
|
1601
|
+
* not return any value.
|
|
1545
1602
|
*/
|
|
1546
1603
|
print(beginRoot = this.root, options) {
|
|
1547
1604
|
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
@@ -1566,13 +1623,18 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1566
1623
|
display(beginRoot);
|
|
1567
1624
|
}
|
|
1568
1625
|
/**
|
|
1569
|
-
*
|
|
1570
|
-
*
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
*
|
|
1574
|
-
*
|
|
1575
|
-
*
|
|
1626
|
+
* Time Complexity: O(1)
|
|
1627
|
+
* Space Complexity: O(1)
|
|
1628
|
+
*/
|
|
1629
|
+
/**
|
|
1630
|
+
* Time Complexity: O(1)
|
|
1631
|
+
* Space Complexity: O(1)
|
|
1632
|
+
*
|
|
1633
|
+
* The function `_getIterator` is a generator function that returns an iterator for the key-value
|
|
1634
|
+
* pairs in a binary search tree.
|
|
1635
|
+
* @param node - The `node` parameter represents the current node in the binary search tree. It is
|
|
1636
|
+
* initially set to the root node of the tree.
|
|
1637
|
+
* @returns an IterableIterator<[K, V | undefined]>.
|
|
1576
1638
|
*/
|
|
1577
1639
|
*_getIterator(node = this.root) {
|
|
1578
1640
|
if (!node)
|
|
@@ -1603,6 +1665,13 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1603
1665
|
}
|
|
1604
1666
|
}
|
|
1605
1667
|
/**
|
|
1668
|
+
* Time Complexity: O(n)
|
|
1669
|
+
* Space Complexity: O(n)
|
|
1670
|
+
*/
|
|
1671
|
+
/**
|
|
1672
|
+
* Time Complexity: O(n)
|
|
1673
|
+
* Space Complexity: O(n)
|
|
1674
|
+
*
|
|
1606
1675
|
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
|
|
1607
1676
|
* taking into account various options such as whether to show null, undefined, or NaN nodes.
|
|
1608
1677
|
* @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
|
|
@@ -1631,12 +1700,12 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1631
1700
|
}
|
|
1632
1701
|
else if (node !== null && node !== undefined) {
|
|
1633
1702
|
// Display logic of normal nodes
|
|
1634
|
-
const key = node.key, line = this.isNIL(node) ? 'S' : key
|
|
1703
|
+
const key = node.key, line = this.isNIL(node) ? 'S' : String(key), width = line.length;
|
|
1635
1704
|
return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
|
|
1636
1705
|
}
|
|
1637
1706
|
else {
|
|
1638
1707
|
// For cases where none of the conditions are met, null, undefined, and NaN nodes are not displayed
|
|
1639
|
-
const line = node === undefined ? 'U' : '
|
|
1708
|
+
const line = node === undefined ? 'U' : 'N', width = line.length;
|
|
1640
1709
|
return _buildNodeDisplay(line, width, [[''], 1, 0, 0], [[''], 1, 0, 0]);
|
|
1641
1710
|
}
|
|
1642
1711
|
function _buildNodeDisplay(line, width, left, right) {
|
|
@@ -1670,10 +1739,21 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1670
1739
|
}
|
|
1671
1740
|
_DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
|
|
1672
1741
|
/**
|
|
1673
|
-
*
|
|
1674
|
-
*
|
|
1675
|
-
|
|
1676
|
-
|
|
1742
|
+
* Time Complexity: O(1)
|
|
1743
|
+
* Space Complexity: O(1)
|
|
1744
|
+
*/
|
|
1745
|
+
/**
|
|
1746
|
+
* Time Complexity: O(1)
|
|
1747
|
+
* Space Complexity: O(1)
|
|
1748
|
+
*
|
|
1749
|
+
* The function `_swapProperties` swaps the key-value properties between two nodes.
|
|
1750
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
|
|
1751
|
+
* destination node. It can be either an instance of the class `R`, or an object of type
|
|
1752
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
1753
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
|
|
1754
|
+
* the properties will be swapped with the `srcNode`.
|
|
1755
|
+
* @returns either the `destNode` object with its properties swapped with the `srcNode` object's
|
|
1756
|
+
* properties, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
1677
1757
|
*/
|
|
1678
1758
|
_swapProperties(srcNode, destNode) {
|
|
1679
1759
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -1692,12 +1772,20 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1692
1772
|
return undefined;
|
|
1693
1773
|
}
|
|
1694
1774
|
/**
|
|
1695
|
-
*
|
|
1775
|
+
* Time Complexity: O(1)
|
|
1776
|
+
* Space Complexity: O(1)
|
|
1777
|
+
*/
|
|
1778
|
+
/**
|
|
1779
|
+
* Time Complexity: O(1)
|
|
1780
|
+
* Space Complexity: O(1)
|
|
1781
|
+
*
|
|
1782
|
+
* The function replaces a node in a binary tree with a new node, updating the parent, left child,
|
|
1783
|
+
* right child, and root if necessary.
|
|
1696
1784
|
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
1697
1785
|
* tree.
|
|
1698
1786
|
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
1699
1787
|
* tree.
|
|
1700
|
-
* @returns
|
|
1788
|
+
* @returns the newNode.
|
|
1701
1789
|
*/
|
|
1702
1790
|
_replaceNode(oldNode, newNode) {
|
|
1703
1791
|
if (oldNode.parent) {
|
|
@@ -1717,10 +1805,17 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1717
1805
|
return newNode;
|
|
1718
1806
|
}
|
|
1719
1807
|
/**
|
|
1720
|
-
*
|
|
1721
|
-
*
|
|
1722
|
-
|
|
1723
|
-
|
|
1808
|
+
* Time Complexity: O(1)
|
|
1809
|
+
* Space Complexity: O(1)
|
|
1810
|
+
*/
|
|
1811
|
+
/**
|
|
1812
|
+
* Time Complexity: O(1)
|
|
1813
|
+
* Space Complexity: O(1)
|
|
1814
|
+
*
|
|
1815
|
+
* The function sets the root property of an object to the provided value, and also updates the
|
|
1816
|
+
* parent property of the new root.
|
|
1817
|
+
* @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`. This
|
|
1818
|
+
* means that it can accept a value of type `NODE`, `null`, or `undefined`.
|
|
1724
1819
|
*/
|
|
1725
1820
|
_setRoot(v) {
|
|
1726
1821
|
if (v) {
|
|
@@ -1728,6 +1823,23 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1728
1823
|
}
|
|
1729
1824
|
this._root = v;
|
|
1730
1825
|
}
|
|
1826
|
+
/**
|
|
1827
|
+
* Time Complexity: O(1)
|
|
1828
|
+
* Space Complexity: O(1)
|
|
1829
|
+
*/
|
|
1830
|
+
/**
|
|
1831
|
+
* Time Complexity: O(1)
|
|
1832
|
+
* Space Complexity: O(1)
|
|
1833
|
+
*
|
|
1834
|
+
* The function `_ensureCallback` ensures that a callback function is provided and returns it.
|
|
1835
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
|
|
1836
|
+
* `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
|
|
1837
|
+
* the generic type `C`, or it can be `null` or `undefined`.
|
|
1838
|
+
* @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
|
|
1839
|
+
* and returns a value. It is of type `C`, which is a generic type that extends the
|
|
1840
|
+
* `BTNCallback<NODE>` type.
|
|
1841
|
+
* @returns the callback parameter.
|
|
1842
|
+
*/
|
|
1731
1843
|
_ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
1732
1844
|
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
|
|
1733
1845
|
callback = (node => node);
|