data-structure-typed 1.52.5 → 1.52.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 +33 -30
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +156 -156
- package/dist/cjs/constants/index.d.ts +4 -0
- package/dist/cjs/constants/index.js +9 -0
- package/dist/cjs/constants/index.js.map +1 -0
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/cjs/data-structures/base/iterable-element-base.js +10 -1
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- 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 +23 -24
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +669 -598
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/cjs/data-structures/binary-tree/bst.js +115 -113
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +40 -39
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +44 -43
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js +7 -4
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js +4 -2
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js +3 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +7 -7
- 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 +2 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +6 -6
- 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 -2
- package/dist/cjs/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/cjs/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/cjs/data-structures/matrix/navigator.js +4 -2
- package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +3 -3
- package/dist/cjs/data-structures/queue/deque.js +29 -29
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -2
- package/dist/cjs/data-structures/tree/tree.js +2 -0
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/cjs/data-structures/trie/trie.js +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/constants/index.d.ts +4 -0
- package/dist/mjs/constants/index.js +5 -0
- package/dist/mjs/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/mjs/data-structures/base/iterable-element-base.js +10 -1
- package/dist/mjs/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/mjs/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +44 -44
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +72 -64
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +668 -592
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/mjs/data-structures/binary-tree/bst.js +116 -112
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +41 -38
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +45 -42
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.js +7 -4
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/directed-graph.js +4 -2
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/mjs/data-structures/hash/hash-map.js +1 -1
- package/dist/mjs/data-structures/heap/heap.js +3 -3
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/navigator.js +4 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +3 -3
- package/dist/mjs/data-structures/queue/deque.js +29 -29
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -2
- package/dist/mjs/data-structures/tree/tree.js +2 -0
- package/dist/mjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/mjs/data-structures/trie/trie.js +1 -1
- package/dist/mjs/index.d.ts +1 -0
- package/dist/mjs/index.js +1 -0
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -5
- package/dist/umd/data-structure-typed.js +1344 -1614
- package/dist/umd/data-structure-typed.min.js +10 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/eslint.config.mjs +69 -0
- package/package.json +30 -28
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
- package/src/data-structures/binary-tree/avl-tree.ts +69 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +698 -726
- package/src/data-structures/binary-tree/bst.ts +123 -129
- package/src/data-structures/binary-tree/rb-tree.ts +44 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +1 -0
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
- package/test/config.ts +1 -7
- package/test/integration/all-in-one.test.ts +2 -2
- package/test/integration/avl-tree.test.ts +3 -3
- package/test/integration/bst.test.ts +19 -18
- package/test/integration/heap.test.js +6 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +39 -39
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +140 -112
- package/test/unit/data-structures/binary-tree/bst.test.ts +41 -13
- package/test/unit/data-structures/binary-tree/overall.test.ts +0 -6
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +88 -34
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +42 -42
- package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/undirected-graph.test.ts +14 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/matrix/navigator.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -4
- package/test/unit/data-structures/stack/stack.test.ts +6 -0
- package/test/unit/unrestricted-interconversion.test.ts +24 -24
- package/test/utils/big-o.ts +5 -4
- package/.eslintrc.js +0 -64
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { DFSOperation } from '../../types';
|
|
9
8
|
import { isComparable, trampoline } from '../../utils';
|
|
10
9
|
import { Queue } from '../queue';
|
|
11
10
|
import { IterableEntryBase } from '../base';
|
|
11
|
+
import { DFSOperation } from '../../constants';
|
|
12
12
|
/**
|
|
13
13
|
* Represents a node in a binary tree.
|
|
14
14
|
* @template V - The type of data stored in the node.
|
|
@@ -18,32 +18,14 @@ export class BinaryTreeNode {
|
|
|
18
18
|
key;
|
|
19
19
|
value;
|
|
20
20
|
parent;
|
|
21
|
-
/**
|
|
22
|
-
* The constructor function initializes an object with a key and an optional value.
|
|
23
|
-
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the
|
|
24
|
-
* constructor. It is used to set the key property of the object being created.
|
|
25
|
-
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
26
|
-
* value associated with the key in the constructor.
|
|
27
|
-
*/
|
|
28
21
|
constructor(key, value) {
|
|
29
22
|
this.key = key;
|
|
30
23
|
this.value = value;
|
|
31
24
|
}
|
|
32
25
|
_left;
|
|
33
|
-
/**
|
|
34
|
-
* The function returns the value of the `_left` property, which can be of type `NODE`, `null`, or
|
|
35
|
-
* `undefined`.
|
|
36
|
-
* @returns The left node of the current node is being returned. It can be either a NODE object,
|
|
37
|
-
* null, or undefined.
|
|
38
|
-
*/
|
|
39
26
|
get left() {
|
|
40
27
|
return this._left;
|
|
41
28
|
}
|
|
42
|
-
/**
|
|
43
|
-
* The function sets the left child of a node and updates its parent reference.
|
|
44
|
-
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
45
|
-
* `undefined`.
|
|
46
|
-
*/
|
|
47
29
|
set left(v) {
|
|
48
30
|
if (v) {
|
|
49
31
|
v.parent = this;
|
|
@@ -51,29 +33,15 @@ export class BinaryTreeNode {
|
|
|
51
33
|
this._left = v;
|
|
52
34
|
}
|
|
53
35
|
_right;
|
|
54
|
-
/**
|
|
55
|
-
* The function returns the right node of a binary tree or null if it doesn't exist.
|
|
56
|
-
* @returns The method is returning the value of the `_right` property, which can be a `NODE` object,
|
|
57
|
-
* `null`, or `undefined`.
|
|
58
|
-
*/
|
|
59
36
|
get right() {
|
|
60
37
|
return this._right;
|
|
61
38
|
}
|
|
62
|
-
/**
|
|
63
|
-
* The function sets the right child of a node and updates its parent.
|
|
64
|
-
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
65
|
-
* `undefined`.
|
|
66
|
-
*/
|
|
67
39
|
set right(v) {
|
|
68
40
|
if (v) {
|
|
69
41
|
v.parent = this;
|
|
70
42
|
}
|
|
71
43
|
this._right = v;
|
|
72
44
|
}
|
|
73
|
-
/**
|
|
74
|
-
* Get the position of the node within its family.
|
|
75
|
-
* @returns {FamilyPosition} - The family position of the node.
|
|
76
|
-
*/
|
|
77
45
|
get familyPosition() {
|
|
78
46
|
const that = this;
|
|
79
47
|
if (!this.parent) {
|
|
@@ -98,15 +66,15 @@ export class BinaryTreeNode {
|
|
|
98
66
|
export class BinaryTree extends IterableEntryBase {
|
|
99
67
|
iterationType = 'ITERATIVE';
|
|
100
68
|
/**
|
|
101
|
-
* The constructor
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* `
|
|
107
|
-
*
|
|
69
|
+
* The constructor initializes a binary tree with optional options and adds keys, nodes, entries, or
|
|
70
|
+
* raw data if provided.
|
|
71
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter in the constructor
|
|
72
|
+
* is an iterable that can contain elements of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It is
|
|
73
|
+
* initialized with an empty array `[]` by default.
|
|
74
|
+
* @param [options] - The `options` parameter in the constructor is an object that can contain the
|
|
75
|
+
* following properties:
|
|
108
76
|
*/
|
|
109
|
-
constructor(
|
|
77
|
+
constructor(keysOrNodesOrEntriesOrRaws = [], options) {
|
|
110
78
|
super();
|
|
111
79
|
if (options) {
|
|
112
80
|
const { iterationType, toEntryFn } = options;
|
|
@@ -117,81 +85,76 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
117
85
|
else if (toEntryFn)
|
|
118
86
|
throw TypeError('toEntryFn must be a function type');
|
|
119
87
|
}
|
|
120
|
-
if (
|
|
121
|
-
this.addMany(
|
|
88
|
+
if (keysOrNodesOrEntriesOrRaws)
|
|
89
|
+
this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
122
90
|
}
|
|
123
91
|
_root;
|
|
124
|
-
/**
|
|
125
|
-
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
126
|
-
* @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
|
|
127
|
-
* `null`, or `undefined`.
|
|
128
|
-
*/
|
|
129
92
|
get root() {
|
|
130
93
|
return this._root;
|
|
131
94
|
}
|
|
132
95
|
_size = 0;
|
|
133
|
-
/**
|
|
134
|
-
* The function returns the size of an object.
|
|
135
|
-
* @returns The size of the object, which is a number.
|
|
136
|
-
*/
|
|
137
96
|
get size() {
|
|
138
97
|
return this._size;
|
|
139
98
|
}
|
|
140
99
|
_NIL = new BinaryTreeNode(NaN);
|
|
141
|
-
/**
|
|
142
|
-
* The function returns the value of the _NIL property.
|
|
143
|
-
* @returns The method is returning the value of the `_NIL` property.
|
|
144
|
-
*/
|
|
145
100
|
get NIL() {
|
|
146
101
|
return this._NIL;
|
|
147
102
|
}
|
|
148
103
|
_toEntryFn;
|
|
149
|
-
/**
|
|
150
|
-
* The function returns the value of the _toEntryFn property.
|
|
151
|
-
* @returns The function being returned is `this._toEntryFn`.
|
|
152
|
-
*/
|
|
153
104
|
get toEntryFn() {
|
|
154
105
|
return this._toEntryFn;
|
|
155
106
|
}
|
|
156
107
|
/**
|
|
157
|
-
*
|
|
158
|
-
* @param {K} key - The key
|
|
159
|
-
* @param {V} value - The value
|
|
160
|
-
*
|
|
108
|
+
* The function creates a new binary tree node with a specified key and optional value.
|
|
109
|
+
* @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
|
|
110
|
+
* @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
|
|
111
|
+
* not required to be provided when calling the function. If a `value` is provided, it should be of
|
|
112
|
+
* type `V`, which is the type of the value associated with the node.
|
|
113
|
+
* @returns A new BinaryTreeNode instance with the provided key and value is being returned, casted
|
|
114
|
+
* as NODE.
|
|
161
115
|
*/
|
|
162
116
|
createNode(key, value) {
|
|
163
117
|
return new BinaryTreeNode(key, value);
|
|
164
118
|
}
|
|
165
119
|
/**
|
|
166
|
-
* The function creates a binary tree with the
|
|
167
|
-
* @param [options] - The `options` parameter
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
120
|
+
* The function creates a binary tree with the specified options.
|
|
121
|
+
* @param [options] - The `options` parameter in the `createTree` function is an optional parameter
|
|
122
|
+
* that allows you to provide partial configuration options for creating a binary tree. It is of type
|
|
123
|
+
* `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
|
|
124
|
+
* of properties
|
|
125
|
+
* @returns A new instance of a binary tree with the specified options is being returned.
|
|
171
126
|
*/
|
|
172
127
|
createTree(options) {
|
|
173
|
-
return new BinaryTree([], {
|
|
128
|
+
return new BinaryTree([], {
|
|
129
|
+
iterationType: this.iterationType,
|
|
130
|
+
toEntryFn: this._toEntryFn,
|
|
131
|
+
...options
|
|
132
|
+
});
|
|
174
133
|
}
|
|
175
134
|
/**
|
|
176
|
-
* The function `keyValueOrEntryOrRawElementToNode` converts
|
|
177
|
-
*
|
|
178
|
-
* @param {
|
|
179
|
-
* `
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
135
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts various input types into a node object
|
|
136
|
+
* or returns null.
|
|
137
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
|
|
138
|
+
* `keyValueOrEntryOrRawElementToNode` function takes in a parameter `keyOrNodeOrEntryOrRaw`, which
|
|
139
|
+
* can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
140
|
+
* node, an entry
|
|
141
|
+
* @param {V} [value] - The `value` parameter in the `keyValueOrEntryOrRawElementToNode` function is
|
|
142
|
+
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
143
|
+
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
144
|
+
* @returns The `keyValueOrEntryOrRawElementToNode` function returns an optional node
|
|
145
|
+
* (`OptBTNOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
146
|
+
* input parameter (`keyOrNodeOrEntryOrRaw`) and processes it accordingly to return a node or null
|
|
147
|
+
* value.
|
|
185
148
|
*/
|
|
186
|
-
keyValueOrEntryOrRawElementToNode(
|
|
187
|
-
if (
|
|
149
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value) {
|
|
150
|
+
if (keyOrNodeOrEntryOrRaw === undefined)
|
|
188
151
|
return;
|
|
189
|
-
if (
|
|
152
|
+
if (keyOrNodeOrEntryOrRaw === null)
|
|
190
153
|
return null;
|
|
191
|
-
if (this.isNode(
|
|
192
|
-
return
|
|
193
|
-
if (this.isEntry(
|
|
194
|
-
const [key, entryValue] =
|
|
154
|
+
if (this.isNode(keyOrNodeOrEntryOrRaw))
|
|
155
|
+
return keyOrNodeOrEntryOrRaw;
|
|
156
|
+
if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
157
|
+
const [key, entryValue] = keyOrNodeOrEntryOrRaw;
|
|
195
158
|
if (key === undefined)
|
|
196
159
|
return;
|
|
197
160
|
else if (key === null)
|
|
@@ -199,122 +162,139 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
199
162
|
if (this.isKey(key))
|
|
200
163
|
return this.createNode(key, value ?? entryValue);
|
|
201
164
|
}
|
|
202
|
-
if (this.
|
|
203
|
-
const [key, entryValue] = this.
|
|
165
|
+
if (this._toEntryFn) {
|
|
166
|
+
const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw);
|
|
204
167
|
if (this.isKey(key))
|
|
205
168
|
return this.createNode(key, value ?? entryValue);
|
|
206
169
|
else
|
|
207
170
|
return;
|
|
208
171
|
}
|
|
209
|
-
if (this.isKey(
|
|
210
|
-
return this.createNode(
|
|
172
|
+
if (this.isKey(keyOrNodeOrEntryOrRaw))
|
|
173
|
+
return this.createNode(keyOrNodeOrEntryOrRaw, value);
|
|
211
174
|
return;
|
|
212
175
|
}
|
|
213
176
|
/**
|
|
214
177
|
* Time Complexity: O(n)
|
|
215
178
|
* Space Complexity: O(log n)
|
|
216
179
|
*
|
|
217
|
-
* The `ensureNode`
|
|
218
|
-
*
|
|
219
|
-
* @param {
|
|
220
|
-
* `
|
|
221
|
-
* a raw
|
|
222
|
-
* @param {IterationType}
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
* @returns The
|
|
180
|
+
* The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
|
|
181
|
+
* value and returns the corresponding node or null.
|
|
182
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
183
|
+
* parameter in the `ensureNode` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It
|
|
184
|
+
* is used to determine whether the input is a key, node, entry, or raw data. The
|
|
185
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
|
|
186
|
+
* is used to specify the type of iteration to be performed. It has a default value of
|
|
187
|
+
* `this.iterationType` if not explicitly provided.
|
|
188
|
+
* @returns The `ensureNode` function returns either a node, `null`, or `undefined` based on the
|
|
189
|
+
* conditions specified in the code snippet.
|
|
226
190
|
*/
|
|
227
|
-
ensureNode(
|
|
228
|
-
if (
|
|
191
|
+
ensureNode(keyOrNodeOrEntryOrRaw, iterationType = this.iterationType) {
|
|
192
|
+
if (keyOrNodeOrEntryOrRaw === null)
|
|
229
193
|
return null;
|
|
230
|
-
if (
|
|
194
|
+
if (keyOrNodeOrEntryOrRaw === undefined)
|
|
231
195
|
return;
|
|
232
|
-
if (
|
|
196
|
+
if (keyOrNodeOrEntryOrRaw === this._NIL)
|
|
233
197
|
return;
|
|
234
|
-
if (this.isNode(
|
|
235
|
-
return
|
|
236
|
-
if (this.
|
|
237
|
-
const
|
|
238
|
-
if (this.isKey(key))
|
|
239
|
-
return this.getNodeByKey(key);
|
|
240
|
-
}
|
|
241
|
-
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
242
|
-
const key = keyOrNodeOrEntryOrRawElement[0];
|
|
198
|
+
if (this.isNode(keyOrNodeOrEntryOrRaw))
|
|
199
|
+
return keyOrNodeOrEntryOrRaw;
|
|
200
|
+
if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
201
|
+
const key = keyOrNodeOrEntryOrRaw[0];
|
|
243
202
|
if (key === null)
|
|
244
203
|
return null;
|
|
245
204
|
if (key === undefined)
|
|
246
205
|
return;
|
|
247
206
|
return this.getNodeByKey(key, iterationType);
|
|
248
207
|
}
|
|
249
|
-
if (this.
|
|
250
|
-
|
|
208
|
+
if (this._toEntryFn) {
|
|
209
|
+
const [key] = this._toEntryFn(keyOrNodeOrEntryOrRaw);
|
|
210
|
+
if (this.isKey(key))
|
|
211
|
+
return this.getNodeByKey(key);
|
|
212
|
+
}
|
|
213
|
+
if (this.isKey(keyOrNodeOrEntryOrRaw))
|
|
214
|
+
return this.getNodeByKey(keyOrNodeOrEntryOrRaw, iterationType);
|
|
251
215
|
return;
|
|
252
216
|
}
|
|
253
217
|
/**
|
|
254
|
-
* The function checks if the input is an instance of
|
|
255
|
-
* @param {
|
|
256
|
-
* `
|
|
257
|
-
*
|
|
258
|
-
*
|
|
218
|
+
* The function isNode checks if the input is an instance of BinaryTreeNode.
|
|
219
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
220
|
+
* `keyOrNodeOrEntryOrRaw` can be either a key, a node, an entry, or raw data. The function is
|
|
221
|
+
* checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
|
|
222
|
+
* accordingly.
|
|
223
|
+
* @returns The function `isNode` is checking if the input `keyOrNodeOrEntryOrRaw` is an instance of
|
|
224
|
+
* `BinaryTreeNode`. If it is, the function returns `true`, indicating that the input is a node. If
|
|
225
|
+
* it is not an instance of `BinaryTreeNode`, the function returns `false`, indicating that the input
|
|
226
|
+
* is not a node.
|
|
259
227
|
*/
|
|
260
|
-
isNode(
|
|
261
|
-
return
|
|
228
|
+
isNode(keyOrNodeOrEntryOrRaw) {
|
|
229
|
+
return keyOrNodeOrEntryOrRaw instanceof BinaryTreeNode;
|
|
262
230
|
}
|
|
263
231
|
/**
|
|
264
|
-
* The function checks if a given
|
|
265
|
-
* @param {
|
|
266
|
-
* `BTNKeyOrNodeOrEntry<K, V, NODE
|
|
267
|
-
*
|
|
232
|
+
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
233
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
234
|
+
* parameter in the `isRealNode` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`.
|
|
235
|
+
* The function checks if the input parameter is a `NODE` type by verifying if it is not equal
|
|
236
|
+
* @returns The function `isRealNode` is checking if the input `keyOrNodeOrEntryOrRaw` is a valid
|
|
237
|
+
* node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
|
|
238
|
+
* values, it then calls the `isNode` method to further determine if the input is a node. The
|
|
239
|
+
* function will return a boolean value indicating whether the
|
|
268
240
|
*/
|
|
269
|
-
isRealNode(
|
|
270
|
-
if (
|
|
241
|
+
isRealNode(keyOrNodeOrEntryOrRaw) {
|
|
242
|
+
if (keyOrNodeOrEntryOrRaw === this._NIL || keyOrNodeOrEntryOrRaw === null || keyOrNodeOrEntryOrRaw === undefined)
|
|
271
243
|
return false;
|
|
272
|
-
return this.isNode(
|
|
244
|
+
return this.isNode(keyOrNodeOrEntryOrRaw);
|
|
273
245
|
}
|
|
274
246
|
/**
|
|
275
|
-
* The function checks if a given
|
|
276
|
-
* @param {
|
|
277
|
-
* `BTNKeyOrNodeOrEntry<K,
|
|
278
|
-
*
|
|
247
|
+
* The function checks if a given input is a valid node or null.
|
|
248
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
249
|
+
* `keyOrNodeOrEntryOrRaw` in the `isRealNodeOrNull` function can be of type `BTNKeyOrNodeOrEntry<K,
|
|
250
|
+
* V, NODE>` or `R`. It is a union type that can either be a key, a node, an entry, or
|
|
251
|
+
* @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
|
|
252
|
+
* `keyOrNodeOrEntryOrRaw` is either `null` or a real node, and returns `true` if it is a node or
|
|
253
|
+
* `null`, and `false` otherwise.
|
|
279
254
|
*/
|
|
280
|
-
isRealNodeOrNull(
|
|
281
|
-
return this.isRealNode(
|
|
255
|
+
isRealNodeOrNull(keyOrNodeOrEntryOrRaw) {
|
|
256
|
+
return keyOrNodeOrEntryOrRaw === null || this.isRealNode(keyOrNodeOrEntryOrRaw);
|
|
282
257
|
}
|
|
283
258
|
/**
|
|
284
|
-
* The function checks if a given node is equal to the
|
|
285
|
-
* @param {
|
|
286
|
-
*
|
|
287
|
-
* @returns
|
|
259
|
+
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
|
|
260
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - BTNKeyOrNodeOrEntry<K, V,
|
|
261
|
+
* NODE> | R
|
|
262
|
+
* @returns The function is checking if the `keyOrNodeOrEntryOrRaw` parameter is equal to the `_NIL`
|
|
263
|
+
* property of the current object and returning a boolean value based on that comparison.
|
|
288
264
|
*/
|
|
289
|
-
isNIL(
|
|
290
|
-
return
|
|
265
|
+
isNIL(keyOrNodeOrEntryOrRaw) {
|
|
266
|
+
return keyOrNodeOrEntryOrRaw === this._NIL;
|
|
291
267
|
}
|
|
292
268
|
/**
|
|
293
|
-
* The function
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
* returns `
|
|
269
|
+
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
270
|
+
* tree.
|
|
271
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
272
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. It represents a
|
|
273
|
+
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
|
|
274
|
+
* provided
|
|
275
|
+
* @returns The function `isLeaf` returns a boolean value indicating whether the input
|
|
276
|
+
* `keyOrNodeOrEntryOrRaw` is a leaf node in a binary tree.
|
|
300
277
|
*/
|
|
301
|
-
isLeaf(
|
|
302
|
-
|
|
303
|
-
if (
|
|
278
|
+
isLeaf(keyOrNodeOrEntryOrRaw) {
|
|
279
|
+
keyOrNodeOrEntryOrRaw = this.ensureNode(keyOrNodeOrEntryOrRaw);
|
|
280
|
+
if (keyOrNodeOrEntryOrRaw === undefined)
|
|
304
281
|
return false;
|
|
305
|
-
if (
|
|
282
|
+
if (keyOrNodeOrEntryOrRaw === null)
|
|
306
283
|
return true;
|
|
307
|
-
return !this.isRealNode(
|
|
284
|
+
return !this.isRealNode(keyOrNodeOrEntryOrRaw.left) && !this.isRealNode(keyOrNodeOrEntryOrRaw.right);
|
|
308
285
|
}
|
|
309
286
|
/**
|
|
310
|
-
* The function checks if the input is
|
|
311
|
-
*
|
|
312
|
-
* @param {
|
|
313
|
-
* `
|
|
314
|
-
*
|
|
287
|
+
* The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
|
|
288
|
+
* with a length of 2.
|
|
289
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
290
|
+
* parameter in the `isEntry` function can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or type `R`.
|
|
291
|
+
* The function checks if the provided `keyOrNodeOrEntryOrRaw` is of type `BTN
|
|
292
|
+
* @returns The `isEntry` function is checking if the `keyOrNodeOrEntryOrRaw` parameter is an array
|
|
293
|
+
* with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
|
|
294
|
+
* `BTNEntry<K, V>`. If the condition is not met, it returns `false`.
|
|
315
295
|
*/
|
|
316
|
-
isEntry(
|
|
317
|
-
return Array.isArray(
|
|
296
|
+
isEntry(keyOrNodeOrEntryOrRaw) {
|
|
297
|
+
return Array.isArray(keyOrNodeOrEntryOrRaw) && keyOrNodeOrEntryOrRaw.length === 2;
|
|
318
298
|
}
|
|
319
299
|
/**
|
|
320
300
|
* Time Complexity O(1)
|
|
@@ -336,29 +316,30 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
336
316
|
* Time Complexity O(n)
|
|
337
317
|
* Space Complexity O(1)
|
|
338
318
|
*
|
|
339
|
-
* The `add` function
|
|
340
|
-
* and finding the
|
|
341
|
-
* @param {
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
* @returns a boolean value. It returns `true` if the insertion
|
|
349
|
-
* insertion position
|
|
319
|
+
* The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
|
|
320
|
+
* and finding the correct insertion position.
|
|
321
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `add` method you provided
|
|
322
|
+
* seems to be for adding a new node to a binary tree structure. The `keyOrNodeOrEntryOrRaw`
|
|
323
|
+
* parameter in the method can accept different types of values:
|
|
324
|
+
* @param {V} [value] - The `value` parameter in the `add` method represents the value associated
|
|
325
|
+
* with the key that you want to add to the binary tree. When adding a key-value pair to the binary
|
|
326
|
+
* tree, you provide the key and its corresponding value. The `add` method then creates a new node
|
|
327
|
+
* with this
|
|
328
|
+
* @returns The `add` method returns a boolean value. It returns `true` if the insertion of the new
|
|
329
|
+
* node was successful, and `false` if the insertion position could not be found or if a duplicate
|
|
330
|
+
* key was found and the node was replaced instead of inserted.
|
|
350
331
|
*/
|
|
351
|
-
add(
|
|
352
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
332
|
+
add(keyOrNodeOrEntryOrRaw, value) {
|
|
333
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
|
|
353
334
|
if (newNode === undefined)
|
|
354
335
|
return false;
|
|
355
336
|
// If the tree is empty, directly set the new node as the root node
|
|
356
|
-
if (!this.
|
|
357
|
-
this.
|
|
337
|
+
if (!this._root) {
|
|
338
|
+
this._setRoot(newNode);
|
|
358
339
|
this._size = 1;
|
|
359
340
|
return true;
|
|
360
341
|
}
|
|
361
|
-
const queue = new Queue([this.
|
|
342
|
+
const queue = new Queue([this._root]);
|
|
362
343
|
let potentialParent; // Record the parent node of the potential insertion location
|
|
363
344
|
while (queue.size > 0) {
|
|
364
345
|
const cur = queue.shift();
|
|
@@ -375,10 +356,12 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
375
356
|
}
|
|
376
357
|
// Continue traversing the left and right subtrees
|
|
377
358
|
if (cur.left !== null) {
|
|
378
|
-
|
|
359
|
+
if (cur.left)
|
|
360
|
+
queue.push(cur.left);
|
|
379
361
|
}
|
|
380
362
|
if (cur.right !== null) {
|
|
381
|
-
|
|
363
|
+
if (cur.right)
|
|
364
|
+
queue.push(cur.right);
|
|
382
365
|
}
|
|
383
366
|
}
|
|
384
367
|
// At the end of the traversal, if the insertion position is found, insert
|
|
@@ -398,24 +381,28 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
398
381
|
* Time Complexity: O(k * n)
|
|
399
382
|
* Space Complexity: O(1)
|
|
400
383
|
*
|
|
401
|
-
* The `addMany` function takes in
|
|
402
|
-
* optional
|
|
403
|
-
*
|
|
404
|
-
* @param
|
|
405
|
-
*
|
|
406
|
-
*
|
|
407
|
-
* in the `
|
|
408
|
-
*
|
|
409
|
-
*
|
|
384
|
+
* The `addMany` function takes in multiple keys or nodes or entries or raw values along with
|
|
385
|
+
* optional values, and adds them to a data structure while returning an array indicating whether
|
|
386
|
+
* each insertion was successful.
|
|
387
|
+
* @param keysOrNodesOrEntriesOrRaws - `keysOrNodesOrEntriesOrRaws` is an iterable that can contain a
|
|
388
|
+
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
|
|
389
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`.
|
|
390
|
+
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
|
|
391
|
+
* accepts an iterable of values. These values correspond to the keys or nodes being added in the
|
|
392
|
+
* `keysOrNodesOrEntriesOrRaws` parameter. If provided, the function will iterate over the values and
|
|
393
|
+
* assign them
|
|
394
|
+
* @returns The `addMany` method returns an array of boolean values indicating whether each key,
|
|
395
|
+
* node, entry, or raw value was successfully added to the data structure. Each boolean value
|
|
396
|
+
* corresponds to the success of adding the corresponding key or value in the input iterable.
|
|
410
397
|
*/
|
|
411
|
-
addMany(
|
|
398
|
+
addMany(keysOrNodesOrEntriesOrRaws, values) {
|
|
412
399
|
// TODO not sure addMany not be run multi times
|
|
413
400
|
const inserted = [];
|
|
414
401
|
let valuesIterator;
|
|
415
402
|
if (values) {
|
|
416
403
|
valuesIterator = values[Symbol.iterator]();
|
|
417
404
|
}
|
|
418
|
-
for (const
|
|
405
|
+
for (const keyOrNodeOrEntryOrRaw of keysOrNodesOrEntriesOrRaws) {
|
|
419
406
|
let value = undefined;
|
|
420
407
|
if (valuesIterator) {
|
|
421
408
|
const valueResult = valuesIterator.next();
|
|
@@ -423,7 +410,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
423
410
|
value = valueResult.value;
|
|
424
411
|
}
|
|
425
412
|
}
|
|
426
|
-
inserted.push(this.add(
|
|
413
|
+
inserted.push(this.add(keyOrNodeOrEntryOrRaw, value));
|
|
427
414
|
}
|
|
428
415
|
return inserted;
|
|
429
416
|
}
|
|
@@ -431,38 +418,38 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
431
418
|
* Time Complexity: O(k * n)
|
|
432
419
|
* Space Complexity: O(1)
|
|
433
420
|
*
|
|
434
|
-
* The `refill` function clears the
|
|
435
|
-
*
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
*
|
|
421
|
+
* The `refill` function clears the existing data structure and then adds new key-value pairs based
|
|
422
|
+
* on the provided input.
|
|
423
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter in the `refill`
|
|
424
|
+
* method can accept an iterable containing a mix of `BTNKeyOrNodeOrEntry<K, V, NODE>` objects or `R`
|
|
425
|
+
* objects.
|
|
426
|
+
* @param [values] - The `values` parameter in the `refill` method is an optional parameter that
|
|
427
|
+
* accepts an iterable of values of type `V` or `undefined`.
|
|
440
428
|
*/
|
|
441
|
-
refill(
|
|
429
|
+
refill(keysOrNodesOrEntriesOrRaws, values) {
|
|
442
430
|
this.clear();
|
|
443
|
-
this.addMany(
|
|
431
|
+
this.addMany(keysOrNodesOrEntriesOrRaws, values);
|
|
444
432
|
}
|
|
445
433
|
/**
|
|
446
434
|
* Time Complexity: O(n)
|
|
447
435
|
* Space Complexity: O(1)
|
|
448
436
|
*
|
|
449
|
-
* The
|
|
450
|
-
* the deleted node
|
|
451
|
-
* @param {
|
|
452
|
-
*
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
*
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
*
|
|
437
|
+
* The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
|
|
438
|
+
* the deleted node along with information for tree balancing.
|
|
439
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
440
|
+
* - The `delete` method you provided is used to delete a node from a binary tree based on the key,
|
|
441
|
+
* node, entry, raw data, or a custom predicate. The method returns an array of
|
|
442
|
+
* `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
|
|
443
|
+
* balancing is needed.
|
|
444
|
+
* @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in
|
|
445
|
+
* the array contains information about the node that was deleted (`deleted`) and the node that may
|
|
446
|
+
* need to be balanced (`needBalanced`).
|
|
459
447
|
*/
|
|
460
|
-
delete(
|
|
448
|
+
delete(keyOrNodeOrEntryOrRawOrPredicate) {
|
|
461
449
|
const deletedResult = [];
|
|
462
|
-
if (!this.
|
|
450
|
+
if (!this._root)
|
|
463
451
|
return deletedResult;
|
|
464
|
-
|
|
465
|
-
const curr = this.getNode(identifier, callback);
|
|
452
|
+
const curr = this.getNode(keyOrNodeOrEntryOrRawOrPredicate);
|
|
466
453
|
if (!curr)
|
|
467
454
|
return deletedResult;
|
|
468
455
|
const parent = curr?.parent;
|
|
@@ -499,7 +486,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
499
486
|
this._setRoot(curr.right);
|
|
500
487
|
curr.right = undefined;
|
|
501
488
|
}
|
|
502
|
-
this._size = this.
|
|
489
|
+
this._size = this._size - 1;
|
|
503
490
|
deletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
504
491
|
return deletedResult;
|
|
505
492
|
}
|
|
@@ -507,47 +494,46 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
507
494
|
* Time Complexity: O(n)
|
|
508
495
|
* Space Complexity: O(k + log n)
|
|
509
496
|
*
|
|
510
|
-
* The function `getNodes`
|
|
511
|
-
* recursive or iterative
|
|
512
|
-
* @param {
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
518
|
-
*
|
|
519
|
-
*
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
527
|
-
* iteration to be performed on the nodes of a binary tree. It can have two possible values:
|
|
528
|
-
* @returns an array of NODE objects.
|
|
497
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
498
|
+
* or predicate, with options for recursive or iterative traversal.
|
|
499
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
500
|
+
* - The `getNodes` function you provided takes several parameters:
|
|
501
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
502
|
+
* determines whether to return only the first node that matches the criteria specified by the
|
|
503
|
+
* `keyOrNodeOrEntryOrRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
504
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
505
|
+
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
506
|
+
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
507
|
+
* not provided, the default value is set to `this._root
|
|
508
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
|
|
509
|
+
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It
|
|
510
|
+
* can have two possible values:
|
|
511
|
+
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
512
|
+
* based on the input parameters and the iteration type specified.
|
|
529
513
|
*/
|
|
530
|
-
getNodes(
|
|
531
|
-
if (
|
|
514
|
+
getNodes(keyOrNodeOrEntryOrRawOrPredicate, onlyOne = false, beginRoot = this._root, iterationType = this.iterationType) {
|
|
515
|
+
if (keyOrNodeOrEntryOrRawOrPredicate === undefined)
|
|
532
516
|
return [];
|
|
533
|
-
if (
|
|
517
|
+
if (keyOrNodeOrEntryOrRawOrPredicate === null)
|
|
534
518
|
return [];
|
|
535
519
|
beginRoot = this.ensureNode(beginRoot);
|
|
536
520
|
if (!beginRoot)
|
|
537
521
|
return [];
|
|
538
|
-
callback = this.
|
|
522
|
+
const callback = this._ensurePredicate(keyOrNodeOrEntryOrRawOrPredicate);
|
|
539
523
|
const ans = [];
|
|
540
524
|
if (iterationType === 'RECURSIVE') {
|
|
541
525
|
const dfs = (cur) => {
|
|
542
|
-
if (callback(cur)
|
|
526
|
+
if (callback(cur)) {
|
|
543
527
|
ans.push(cur);
|
|
544
528
|
if (onlyOne)
|
|
545
529
|
return;
|
|
546
530
|
}
|
|
547
531
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
548
532
|
return;
|
|
549
|
-
this.isRealNode(cur.left)
|
|
550
|
-
|
|
533
|
+
if (this.isRealNode(cur.left))
|
|
534
|
+
dfs(cur.left);
|
|
535
|
+
if (this.isRealNode(cur.right))
|
|
536
|
+
dfs(cur.right);
|
|
551
537
|
};
|
|
552
538
|
dfs(beginRoot);
|
|
553
539
|
}
|
|
@@ -556,13 +542,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
556
542
|
while (stack.length > 0) {
|
|
557
543
|
const cur = stack.pop();
|
|
558
544
|
if (this.isRealNode(cur)) {
|
|
559
|
-
if (callback(cur)
|
|
545
|
+
if (callback(cur)) {
|
|
560
546
|
ans.push(cur);
|
|
561
547
|
if (onlyOne)
|
|
562
548
|
return ans;
|
|
563
549
|
}
|
|
564
|
-
this.isRealNode(cur.left)
|
|
565
|
-
|
|
550
|
+
if (this.isRealNode(cur.left))
|
|
551
|
+
stack.push(cur.left);
|
|
552
|
+
if (this.isRealNode(cur.right))
|
|
553
|
+
stack.push(cur.right);
|
|
566
554
|
}
|
|
567
555
|
}
|
|
568
556
|
}
|
|
@@ -572,91 +560,95 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
572
560
|
* Time Complexity: O(n)
|
|
573
561
|
* Space Complexity: O(log n).
|
|
574
562
|
*
|
|
575
|
-
* The
|
|
576
|
-
*
|
|
577
|
-
* @param {
|
|
578
|
-
*
|
|
579
|
-
*
|
|
580
|
-
* @param {
|
|
581
|
-
*
|
|
582
|
-
*
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
* of
|
|
587
|
-
*
|
|
563
|
+
* The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
|
|
564
|
+
* predicate.
|
|
565
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
566
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `getNode` function can accept a key,
|
|
567
|
+
* node, entry, raw data, or a predicate function.
|
|
568
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
569
|
+
* `getNode` function is used to specify the starting point for searching for a node in a binary
|
|
570
|
+
* tree. If no specific starting point is provided, the default value is set to `this._root`, which
|
|
571
|
+
* is typically the root node of the binary tree.
|
|
572
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is
|
|
573
|
+
* used to specify the type of iteration to be performed when searching for a node. It has a default
|
|
574
|
+
* value of `this.iterationType`, which means it will use the iteration type defined in the current
|
|
575
|
+
* context if no specific value is provided
|
|
576
|
+
* @returns The `getNode` function is returning the first node that matches the specified criteria,
|
|
577
|
+
* or `null` if no matching node is found.
|
|
588
578
|
*/
|
|
589
|
-
getNode(
|
|
590
|
-
return this.getNodes(
|
|
579
|
+
getNode(keyOrNodeOrEntryOrRawOrPredicate, beginRoot = this._root, iterationType = this.iterationType) {
|
|
580
|
+
return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType)[0] ?? null;
|
|
591
581
|
}
|
|
592
582
|
/**
|
|
593
583
|
* Time Complexity: O(n)
|
|
594
584
|
* Space Complexity: O(log n)
|
|
595
585
|
*
|
|
596
|
-
* The function `getNodeByKey`
|
|
597
|
-
* @param {K} key - The key parameter is the value
|
|
598
|
-
*
|
|
599
|
-
* @param {IterationType}
|
|
600
|
-
*
|
|
601
|
-
*
|
|
602
|
-
* @returns
|
|
586
|
+
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
587
|
+
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
588
|
+
* structure.
|
|
589
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
590
|
+
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
591
|
+
* is an optional parameter with a default value of `this.iterationType`.
|
|
592
|
+
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
593
|
+
* (`OptBTNOrNull<NODE>`).
|
|
603
594
|
*/
|
|
604
595
|
getNodeByKey(key, iterationType = this.iterationType) {
|
|
605
|
-
return this.getNode(key, this.
|
|
596
|
+
return this.getNode(key, this._root, iterationType);
|
|
606
597
|
}
|
|
607
598
|
/**
|
|
608
599
|
* Time Complexity: O(n)
|
|
609
600
|
* Space Complexity: O(log n)
|
|
610
601
|
*
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
* @param {
|
|
614
|
-
*
|
|
615
|
-
*
|
|
616
|
-
* @param {
|
|
617
|
-
*
|
|
618
|
-
*
|
|
619
|
-
*
|
|
620
|
-
*
|
|
621
|
-
*
|
|
622
|
-
*
|
|
623
|
-
*
|
|
624
|
-
*
|
|
625
|
-
*
|
|
626
|
-
*
|
|
602
|
+
* This function overrides the `get` method to retrieve the value associated with a specified key,
|
|
603
|
+
* node, entry, raw data, or predicate in a data structure.
|
|
604
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
605
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `get` method can accept one of the
|
|
606
|
+
* following types:
|
|
607
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `get`
|
|
608
|
+
* method is used to specify the starting point for searching for a key or node in the binary tree.
|
|
609
|
+
* If no specific starting point is provided, the default starting point is the root of the binary
|
|
610
|
+
* tree (`this._root`).
|
|
611
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `get` method is used
|
|
612
|
+
* to specify the type of iteration to be performed when searching for a key in the binary tree. It
|
|
613
|
+
* is an optional parameter with a default value of `this.iterationType`, which means it will use the
|
|
614
|
+
* iteration type defined in the
|
|
615
|
+
* @returns The `get` method is returning the value associated with the specified key, node, entry,
|
|
616
|
+
* raw data, or predicate in the binary tree map. If the specified key or node is found in the tree,
|
|
617
|
+
* the method returns the corresponding value. If the key or node is not found, it returns
|
|
618
|
+
* `undefined`.
|
|
627
619
|
*/
|
|
628
|
-
get(
|
|
629
|
-
return this.getNode(
|
|
620
|
+
get(keyOrNodeOrEntryOrRawOrPredicate, beginRoot = this._root, iterationType = this.iterationType) {
|
|
621
|
+
return this.getNode(keyOrNodeOrEntryOrRawOrPredicate, beginRoot, iterationType)?.value;
|
|
630
622
|
}
|
|
631
623
|
/**
|
|
632
624
|
* Time Complexity: O(n)
|
|
633
625
|
* Space Complexity: O(log n)
|
|
634
626
|
*
|
|
635
|
-
* The `has` function checks if a
|
|
636
|
-
*
|
|
637
|
-
* @param {
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
*
|
|
641
|
-
*
|
|
642
|
-
*
|
|
643
|
-
* `
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
-
*
|
|
647
|
-
* @
|
|
648
|
-
*
|
|
649
|
-
*
|
|
627
|
+
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
|
|
628
|
+
* exists in the data structure.
|
|
629
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrNodeOrEntryOrRawOrPredicate
|
|
630
|
+
* - The `keyOrNodeOrEntryOrRawOrPredicate` parameter in the `override has` method can accept one of
|
|
631
|
+
* the following types:
|
|
632
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
633
|
+
* `override` method is used to specify the starting point for the search operation within the data
|
|
634
|
+
* structure. It defaults to `this._root` if not provided explicitly.
|
|
635
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
|
|
636
|
+
* is used to specify the type of iteration to be performed. It has a default value of
|
|
637
|
+
* `this.iterationType`, which means it will use the iteration type defined in the current context if
|
|
638
|
+
* no value is provided when calling the method.
|
|
639
|
+
* @returns The `override has` method is returning a boolean value. It checks if there are any nodes
|
|
640
|
+
* that match the provided key, node, entry, raw data, or predicate in the tree structure. If there
|
|
641
|
+
* are matching nodes, it returns `true`, indicating that the tree contains the specified element.
|
|
642
|
+
* Otherwise, it returns `false`.
|
|
650
643
|
*/
|
|
651
|
-
has(
|
|
652
|
-
|
|
653
|
-
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
644
|
+
has(keyOrNodeOrEntryOrRawOrPredicate, beginRoot = this._root, iterationType = this.iterationType) {
|
|
645
|
+
return this.getNodes(keyOrNodeOrEntryOrRawOrPredicate, true, beginRoot, iterationType).length > 0;
|
|
654
646
|
}
|
|
655
647
|
/**
|
|
656
648
|
* Time Complexity: O(1)
|
|
657
649
|
* Space Complexity: O(1)
|
|
658
650
|
*
|
|
659
|
-
*
|
|
651
|
+
* The `clear` function resets the root node and size of a data structure to empty.
|
|
660
652
|
*/
|
|
661
653
|
clear() {
|
|
662
654
|
this._setRoot(undefined);
|
|
@@ -666,42 +658,51 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
666
658
|
* Time Complexity: O(1)
|
|
667
659
|
* Space Complexity: O(1)
|
|
668
660
|
*
|
|
669
|
-
*
|
|
670
|
-
*
|
|
661
|
+
* The `isEmpty` function in TypeScript checks if a data structure has no elements and returns a
|
|
662
|
+
* boolean value.
|
|
663
|
+
* @returns The `isEmpty()` method is returning a boolean value, specifically `true` if the `_size`
|
|
664
|
+
* property is equal to 0, indicating that the data structure is empty, and `false` otherwise.
|
|
671
665
|
*/
|
|
672
666
|
isEmpty() {
|
|
673
|
-
return this.
|
|
667
|
+
return this._size === 0;
|
|
674
668
|
}
|
|
675
669
|
/**
|
|
676
670
|
* Time Complexity: O(n)
|
|
677
671
|
* Space Complexity: O(log n)
|
|
678
672
|
*
|
|
679
|
-
* The function checks if a binary tree is perfectly balanced by comparing
|
|
680
|
-
* height
|
|
681
|
-
* @param {
|
|
682
|
-
*
|
|
683
|
-
*
|
|
684
|
-
*
|
|
685
|
-
*
|
|
673
|
+
* The function checks if a binary tree is perfectly balanced by comparing its minimum height with
|
|
674
|
+
* its height.
|
|
675
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
676
|
+
* point for checking if the binary tree is perfectly balanced. It represents the root node of the
|
|
677
|
+
* binary tree or a specific node from which the balance check should begin.
|
|
678
|
+
* @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
|
|
679
|
+
* the tree starting from the `beginRoot` node is perfectly balanced or not. The return value is
|
|
680
|
+
* determined by comparing the minimum height of the tree with the height of the tree. If the minimum
|
|
681
|
+
* height plus 1 is greater than or equal to the height of the tree, then it is considered perfectly
|
|
682
|
+
* balanced and
|
|
686
683
|
*/
|
|
687
|
-
isPerfectlyBalanced(beginRoot = this.
|
|
684
|
+
isPerfectlyBalanced(beginRoot = this._root) {
|
|
688
685
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
689
686
|
}
|
|
690
687
|
/**
|
|
691
688
|
* Time Complexity: O(n)
|
|
692
689
|
* Space Complexity: O(1)
|
|
693
690
|
*
|
|
694
|
-
* The function `isBST` checks if a binary search tree is valid
|
|
695
|
-
*
|
|
696
|
-
*
|
|
697
|
-
*
|
|
698
|
-
*
|
|
699
|
-
*
|
|
700
|
-
*
|
|
701
|
-
*
|
|
702
|
-
*
|
|
691
|
+
* The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
|
|
692
|
+
* or iterative methods.
|
|
693
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `isBST`
|
|
694
|
+
* function represents the starting point for checking whether a binary search tree (BST) is valid.
|
|
695
|
+
* It can be a node in the BST or a reference to the root of the BST. If no specific node is
|
|
696
|
+
* provided, the function will default to
|
|
697
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `isBST` function
|
|
698
|
+
* determines whether the function should use a recursive approach or an iterative approach to check
|
|
699
|
+
* if the binary search tree (BST) is valid.
|
|
700
|
+
* @returns The `isBST` method is returning a boolean value, which indicates whether the binary
|
|
701
|
+
* search tree (BST) represented by the given root node is a valid BST or not. The method checks if
|
|
702
|
+
* the tree satisfies the BST property, where for every node, all nodes in its left subtree have keys
|
|
703
|
+
* less than the node's key, and all nodes in its right subtree have keys greater than the node's
|
|
703
704
|
*/
|
|
704
|
-
isBST(beginRoot = this.
|
|
705
|
+
isBST(beginRoot = this._root, iterationType = this.iterationType) {
|
|
705
706
|
// TODO there is a bug
|
|
706
707
|
beginRoot = this.ensureNode(beginRoot);
|
|
707
708
|
if (!beginRoot)
|
|
@@ -747,17 +748,19 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
747
748
|
* Time Complexity: O(n)
|
|
748
749
|
* Space Complexity: O(1)
|
|
749
750
|
*
|
|
750
|
-
* The function calculates the depth
|
|
751
|
-
* @param {
|
|
752
|
-
*
|
|
753
|
-
*
|
|
754
|
-
* @param {
|
|
755
|
-
* represents the starting point from which to calculate the depth
|
|
756
|
-
* node
|
|
757
|
-
* `
|
|
758
|
-
* @returns the depth of a node
|
|
751
|
+
* The `getDepth` function calculates the depth between two nodes in a binary tree.
|
|
752
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} dist - The `dist` parameter in the `getDepth`
|
|
753
|
+
* function represents the node or entry in a binary tree map, or a reference to a node in the tree.
|
|
754
|
+
* It is the target node for which you want to calculate the depth from the `beginRoot` node.
|
|
755
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
756
|
+
* `getDepth` function represents the starting point from which you want to calculate the depth of a
|
|
757
|
+
* given node or entry in a binary tree. If no specific starting point is provided, the default value
|
|
758
|
+
* for `beginRoot` is set to the root of the binary
|
|
759
|
+
* @returns The `getDepth` method returns the depth of a given node `dist` relative to the
|
|
760
|
+
* `beginRoot` node in a binary tree. If the `dist` node is not found in the path to the `beginRoot`
|
|
761
|
+
* node, it returns the depth of the `dist` node from the root of the tree.
|
|
759
762
|
*/
|
|
760
|
-
getDepth(dist, beginRoot = this.
|
|
763
|
+
getDepth(dist, beginRoot = this._root) {
|
|
761
764
|
let distEnsured = this.ensureNode(dist);
|
|
762
765
|
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
763
766
|
let depth = 0;
|
|
@@ -775,15 +778,19 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
775
778
|
* Space Complexity: O(1)
|
|
776
779
|
*
|
|
777
780
|
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
778
|
-
* or iterative approach.
|
|
779
|
-
* @param {
|
|
780
|
-
*
|
|
781
|
-
*
|
|
782
|
-
*
|
|
783
|
-
*
|
|
784
|
-
*
|
|
781
|
+
* or iterative approach in TypeScript.
|
|
782
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
783
|
+
* point from which the height of the binary tree will be calculated. It can be a node in the binary
|
|
784
|
+
* tree or a reference to the root of the tree. If not provided, it defaults to the root of the
|
|
785
|
+
* binary tree data structure.
|
|
786
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
|
|
787
|
+
* of iteration to be performed while calculating the height of the binary tree. It can have two
|
|
788
|
+
* possible values:
|
|
789
|
+
* @returns The `getHeight` method returns the height of the binary tree starting from the specified
|
|
790
|
+
* root node. The height is calculated based on the maximum depth of the tree, considering either a
|
|
791
|
+
* recursive approach or an iterative approach depending on the `iterationType` parameter.
|
|
785
792
|
*/
|
|
786
|
-
getHeight(beginRoot = this.
|
|
793
|
+
getHeight(beginRoot = this._root, iterationType = this.iterationType) {
|
|
787
794
|
beginRoot = this.ensureNode(beginRoot);
|
|
788
795
|
if (!this.isRealNode(beginRoot))
|
|
789
796
|
return -1;
|
|
@@ -816,18 +823,20 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
816
823
|
* Space Complexity: O(log n)
|
|
817
824
|
*
|
|
818
825
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
819
|
-
* recursive or iterative approach.
|
|
820
|
-
* @param {
|
|
821
|
-
* starting
|
|
822
|
-
*
|
|
823
|
-
* tree.
|
|
824
|
-
* @param {IterationType} iterationType - The `iterationType` parameter
|
|
825
|
-
* iteration to
|
|
826
|
-
* values:
|
|
827
|
-
* @returns The
|
|
828
|
-
*
|
|
826
|
+
* recursive or iterative approach in TypeScript.
|
|
827
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
828
|
+
* `getMinHeight` function represents the starting node from which the minimum height of the binary
|
|
829
|
+
* tree will be calculated. It is either a node in the binary tree or a reference to the root of the
|
|
830
|
+
* tree. If not provided, the default value is the root
|
|
831
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getMinHeight` method
|
|
832
|
+
* specifies the type of iteration to use when calculating the minimum height of a binary tree. It
|
|
833
|
+
* can have two possible values:
|
|
834
|
+
* @returns The `getMinHeight` method returns the minimum height of the binary tree starting from the
|
|
835
|
+
* specified root node. The height is calculated based on the shortest path from the root node to a
|
|
836
|
+
* leaf node in the tree. The method uses either a recursive approach or an iterative approach (using
|
|
837
|
+
* a stack) based on the `iterationType` parameter.
|
|
829
838
|
*/
|
|
830
|
-
getMinHeight(beginRoot = this.
|
|
839
|
+
getMinHeight(beginRoot = this._root, iterationType = this.iterationType) {
|
|
831
840
|
beginRoot = this.ensureNode(beginRoot);
|
|
832
841
|
if (!beginRoot)
|
|
833
842
|
return -1;
|
|
@@ -857,8 +866,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
857
866
|
if (!this.isRealNode(node.right) || last === node.right) {
|
|
858
867
|
node = stack.pop();
|
|
859
868
|
if (this.isRealNode(node)) {
|
|
860
|
-
const leftMinHeight = this.isRealNode(node.left) ?
|
|
861
|
-
const rightMinHeight = this.isRealNode(node.right) ?
|
|
869
|
+
const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) : -1;
|
|
870
|
+
const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) : -1;
|
|
862
871
|
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
863
872
|
last = node;
|
|
864
873
|
node = null;
|
|
@@ -868,33 +877,40 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
868
877
|
node = node.right;
|
|
869
878
|
}
|
|
870
879
|
}
|
|
871
|
-
return depths.get(beginRoot)
|
|
880
|
+
return depths.get(beginRoot);
|
|
872
881
|
}
|
|
873
882
|
}
|
|
874
883
|
/**
|
|
875
884
|
* Time Complexity: O(log n)
|
|
876
885
|
* Space Complexity: O(log n)
|
|
877
886
|
*
|
|
878
|
-
* The function `getPathToRoot`
|
|
879
|
-
*
|
|
880
|
-
* @param {
|
|
881
|
-
*
|
|
882
|
-
*
|
|
883
|
-
*
|
|
884
|
-
*
|
|
885
|
-
*
|
|
887
|
+
* The function `getPathToRoot` in TypeScript retrieves the path from a given node to the root of a
|
|
888
|
+
* tree structure, applying a specified callback function along the way.
|
|
889
|
+
* @param {C} callback - The `callback` parameter is a function that is used to process each node in
|
|
890
|
+
* the path to the root. It is expected to be a function that takes a node as an argument and returns
|
|
891
|
+
* a value based on that node. The return type of the callback function is determined by the generic
|
|
892
|
+
* type `C
|
|
893
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginNode - The `beginNode` parameter in the
|
|
894
|
+
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
|
|
895
|
+
* @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
|
|
896
|
+
* whether the resulting path from the given `beginNode` to the root should be in reverse order or
|
|
897
|
+
* not. If `isReverse` is set to `true`, the path will be reversed before being returned. If `is
|
|
898
|
+
* @returns The function `getPathToRoot` returns an array of the return values of the callback
|
|
899
|
+
* function `callback` applied to each node in the path from the `beginNode` to the root node. The
|
|
900
|
+
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
901
|
+
* parameter.
|
|
886
902
|
*/
|
|
887
|
-
getPathToRoot(beginNode, isReverse = true) {
|
|
903
|
+
getPathToRoot(callback = this._DEFAULT_BTN_CALLBACK, beginNode, isReverse = true) {
|
|
888
904
|
const result = [];
|
|
889
905
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
890
906
|
if (!beginNodeEnsured)
|
|
891
907
|
return result;
|
|
892
908
|
while (beginNodeEnsured.parent) {
|
|
893
909
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
894
|
-
result.push(beginNodeEnsured);
|
|
910
|
+
result.push(callback(beginNodeEnsured));
|
|
895
911
|
beginNodeEnsured = beginNodeEnsured.parent;
|
|
896
912
|
}
|
|
897
|
-
result.push(beginNodeEnsured);
|
|
913
|
+
result.push(callback(beginNodeEnsured));
|
|
898
914
|
return isReverse ? result.reverse() : result;
|
|
899
915
|
}
|
|
900
916
|
/**
|
|
@@ -904,21 +920,21 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
904
920
|
* The function `getLeftMost` retrieves the leftmost node in a binary tree using either recursive or
|
|
905
921
|
* tail-recursive iteration.
|
|
906
922
|
* @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
|
|
907
|
-
* node of a binary tree or
|
|
908
|
-
* if not
|
|
909
|
-
* @param {
|
|
923
|
+
* node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
|
|
924
|
+
* value of `_DEFAULT_BTN_CALLBACK` if not specified.
|
|
925
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
910
926
|
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary
|
|
911
|
-
* tree. It can be either a
|
|
912
|
-
* the
|
|
927
|
+
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
928
|
+
* starting point is provided, the function will default
|
|
913
929
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getLeftMost` function
|
|
914
930
|
* specifies the type of iteration to be used when traversing the binary tree nodes. It can have two
|
|
915
931
|
* possible values:
|
|
916
932
|
* @returns The `getLeftMost` function returns the result of the callback function `C` applied to the
|
|
917
|
-
* leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` is
|
|
918
|
-
* it returns the result of the callback function applied to `undefined`. If the `beginRoot`
|
|
919
|
-
* real node, it returns the result of the callback
|
|
933
|
+
* leftmost node in the binary tree starting from the `beginRoot` node. If the `beginRoot` node is
|
|
934
|
+
* `NIL`, it returns the result of the callback function applied to `undefined`. If the `beginRoot`
|
|
935
|
+
* node is not a real node, it returns the result of the callback
|
|
920
936
|
*/
|
|
921
|
-
getLeftMost(callback = this.
|
|
937
|
+
getLeftMost(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
|
|
922
938
|
if (this.isNIL(beginRoot))
|
|
923
939
|
return callback(undefined);
|
|
924
940
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -949,22 +965,22 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
949
965
|
* The function `getRightMost` retrieves the rightmost node in a binary tree using either recursive
|
|
950
966
|
* or iterative traversal methods.
|
|
951
967
|
* @param {C} callback - The `callback` parameter is a function that will be called with the result
|
|
952
|
-
* of the
|
|
953
|
-
*
|
|
954
|
-
*
|
|
968
|
+
* of finding the rightmost node in a binary tree. It is of type `BTNCallback<OptBTNOrNull<NODE>>`,
|
|
969
|
+
* which means it is a callback function that can accept either an optional binary tree node or null
|
|
970
|
+
* as
|
|
971
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
955
972
|
* `getRightMost` function represents the starting point for finding the rightmost node in a binary
|
|
956
|
-
* tree. It can be either a
|
|
957
|
-
*
|
|
973
|
+
* tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
|
|
974
|
+
* starting point is provided, the function will default
|
|
958
975
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getRightMost`
|
|
959
|
-
* function specifies the type of iteration to be used when
|
|
960
|
-
*
|
|
961
|
-
* @returns The `getRightMost` function returns the result of the callback function `C
|
|
962
|
-
*
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
* rightmost node is returned
|
|
976
|
+
* function specifies the type of iteration to be used when traversing the binary tree nodes. It can
|
|
977
|
+
* have two possible values:
|
|
978
|
+
* @returns The `getRightMost` function returns the result of the callback function `C`, which is
|
|
979
|
+
* passed as a parameter to the function. The callback function is called with the rightmost node in
|
|
980
|
+
* the binary tree structure, determined based on the specified iteration type ('RECURSIVE' or
|
|
981
|
+
* other).
|
|
966
982
|
*/
|
|
967
|
-
getRightMost(callback = this.
|
|
983
|
+
getRightMost(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
|
|
968
984
|
if (this.isNIL(beginRoot))
|
|
969
985
|
return callback(undefined);
|
|
970
986
|
// TODO support get right most by passing key in
|
|
@@ -993,10 +1009,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
993
1009
|
* Time Complexity: O(log n)
|
|
994
1010
|
* Space Complexity: O(1)
|
|
995
1011
|
*
|
|
996
|
-
* The function returns the predecessor node of a given node in a
|
|
997
|
-
*
|
|
998
|
-
*
|
|
999
|
-
*
|
|
1012
|
+
* The function `getPredecessor` in TypeScript returns the predecessor node of a given node in a
|
|
1013
|
+
* binary tree.
|
|
1014
|
+
* @param {NODE} node - The `getPredecessor` function you provided seems to be attempting to find the
|
|
1015
|
+
* predecessor of a given node in a binary tree. However, there seems to be a logical issue in the
|
|
1016
|
+
* while loop condition that might cause an infinite loop.
|
|
1017
|
+
* @returns The `getPredecessor` function returns the predecessor node of the input `NODE` parameter.
|
|
1018
|
+
* If the left child of the input node exists, it traverses to the rightmost node of the left subtree
|
|
1019
|
+
* to find the predecessor. If the left child does not exist, it returns the input node itself.
|
|
1000
1020
|
*/
|
|
1001
1021
|
getPredecessor(node) {
|
|
1002
1022
|
if (this.isRealNode(node.left)) {
|
|
@@ -1016,10 +1036,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1016
1036
|
* Time Complexity: O(log n)
|
|
1017
1037
|
* Space Complexity: O(1)
|
|
1018
1038
|
*
|
|
1019
|
-
* The function `getSuccessor` returns the next node in
|
|
1020
|
-
*
|
|
1021
|
-
* @
|
|
1022
|
-
*
|
|
1039
|
+
* The function `getSuccessor` in TypeScript returns the next node in an in-order traversal of a
|
|
1040
|
+
* binary tree.
|
|
1041
|
+
* @param {K | NODE | null} [x] - The `getSuccessor` function takes a parameter `x`, which can be of
|
|
1042
|
+
* type `K`, `NODE`, or `null`.
|
|
1043
|
+
* @returns The `getSuccessor` function returns the successor node of the input node `x`. If `x` has
|
|
1044
|
+
* a right child, the function returns the leftmost node in the right subtree of `x`. If `x` does not
|
|
1045
|
+
* have a right child, the function traverses up the parent nodes until it finds a node that is not
|
|
1046
|
+
* the right child of its parent, and returns that node
|
|
1023
1047
|
*/
|
|
1024
1048
|
getSuccessor(x) {
|
|
1025
1049
|
x = this.ensureNode(x);
|
|
@@ -1039,26 +1063,29 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1039
1063
|
* Time complexity: O(n)
|
|
1040
1064
|
* Space complexity: O(n)
|
|
1041
1065
|
*
|
|
1042
|
-
* The `dfs`
|
|
1043
|
-
*
|
|
1044
|
-
* @param {C} callback - The `callback` parameter is a
|
|
1045
|
-
*
|
|
1046
|
-
*
|
|
1047
|
-
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter
|
|
1048
|
-
*
|
|
1049
|
-
*
|
|
1050
|
-
*
|
|
1051
|
-
* is
|
|
1052
|
-
*
|
|
1053
|
-
*
|
|
1054
|
-
*
|
|
1055
|
-
*
|
|
1056
|
-
*
|
|
1057
|
-
*
|
|
1058
|
-
* values
|
|
1059
|
-
*
|
|
1066
|
+
* The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
|
|
1067
|
+
* specified parameters.
|
|
1068
|
+
* @param {C} callback - The `callback` parameter is a generic type `C` that extends the
|
|
1069
|
+
* `BTNCallback` interface with a type parameter of `OptBTNOrNull<NODE>`. It has a default value of
|
|
1070
|
+
* `this._DEFAULT_BTN_CALLBACK as C`.
|
|
1071
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
|
|
1072
|
+
* order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
|
|
1073
|
+
* possible values for the `pattern` parameter are:
|
|
1074
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `dfs`
|
|
1075
|
+
* method is used to specify the starting point for the Depth-First Search traversal. It can be
|
|
1076
|
+
* either a `BTNKeyOrNodeOrEntry` object representing a key, node, or entry in the binary tree map,
|
|
1077
|
+
* or it can be a
|
|
1078
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` method specifies
|
|
1079
|
+
* the type of iteration to be performed during the depth-first search traversal. It is used to
|
|
1080
|
+
* determine the order in which nodes are visited during the traversal.
|
|
1081
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `dfs` method is a boolean flag
|
|
1082
|
+
* that determines whether null values should be included in the traversal or not. If `includeNull`
|
|
1083
|
+
* is set to `true`, then null values will be included in the traversal process. If it is set to
|
|
1084
|
+
* `false`,
|
|
1085
|
+
* @returns The `dfs` method is returning an array of the return type specified by the generic type
|
|
1086
|
+
* parameter `C`. The return type is determined by the callback function provided to the method.
|
|
1060
1087
|
*/
|
|
1061
|
-
dfs(callback = this.
|
|
1088
|
+
dfs(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1062
1089
|
beginRoot = this.ensureNode(beginRoot);
|
|
1063
1090
|
if (!beginRoot)
|
|
1064
1091
|
return [];
|
|
@@ -1068,25 +1095,26 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1068
1095
|
* Time complexity: O(n)
|
|
1069
1096
|
* Space complexity: O(n)
|
|
1070
1097
|
*
|
|
1071
|
-
* The `bfs` function performs a breadth-first search on a binary tree
|
|
1072
|
-
*
|
|
1073
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1074
|
-
* the breadth-first search traversal. It
|
|
1075
|
-
*
|
|
1076
|
-
* @param {
|
|
1077
|
-
* starting point
|
|
1078
|
-
*
|
|
1079
|
-
*
|
|
1080
|
-
* @param {IterationType} iterationType - The `iterationType` parameter
|
|
1081
|
-
* iteration to be performed. It can have two
|
|
1082
|
-
*
|
|
1083
|
-
*
|
|
1084
|
-
* `
|
|
1085
|
-
* set to `
|
|
1086
|
-
*
|
|
1087
|
-
* `
|
|
1098
|
+
* The `bfs` function performs a breadth-first search traversal on a binary tree or binary search
|
|
1099
|
+
* tree, executing a specified callback function on each node visited.
|
|
1100
|
+
* @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
|
|
1101
|
+
* called on each node visited during the breadth-first search traversal. It is a generic type `C`
|
|
1102
|
+
* that extends the `BTNCallback` type, which takes a parameter of type `NODE` or `null`.
|
|
1103
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `bfs`
|
|
1104
|
+
* function represents the starting point for the breadth-first search traversal in a binary tree. It
|
|
1105
|
+
* can be specified as a key, node, or entry in the binary tree structure. If not provided, the
|
|
1106
|
+
* default value is the root node of the binary
|
|
1107
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `bfs` function
|
|
1108
|
+
* determines the type of iteration to be performed on the binary tree nodes. It can have two
|
|
1109
|
+
* possible values:
|
|
1110
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `bfs` function determines whether
|
|
1111
|
+
* to include `null` values in the breadth-first search traversal of a binary tree. If `includeNull`
|
|
1112
|
+
* is set to `true`, the traversal will include `null` values for nodes that do not have children
|
|
1113
|
+
* (left
|
|
1114
|
+
* @returns The `bfs` function returns an array of values that are the result of applying the
|
|
1115
|
+
* provided callback function to each node in the binary tree in a breadth-first search manner.
|
|
1088
1116
|
*/
|
|
1089
|
-
bfs(callback = this.
|
|
1117
|
+
bfs(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1090
1118
|
beginRoot = this.ensureNode(beginRoot);
|
|
1091
1119
|
if (!beginRoot)
|
|
1092
1120
|
return [];
|
|
@@ -1142,27 +1170,25 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1142
1170
|
* Time complexity: O(n)
|
|
1143
1171
|
* Space complexity: O(n)
|
|
1144
1172
|
*
|
|
1145
|
-
* The `leaves` function in TypeScript
|
|
1146
|
-
*
|
|
1173
|
+
* The `leaves` function in TypeScript returns an array of values from leaf nodes in a binary tree
|
|
1174
|
+
* structure based on a specified callback and iteration type.
|
|
1147
1175
|
* @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
|
|
1148
|
-
* in the binary tree. It is
|
|
1149
|
-
*
|
|
1150
|
-
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the `leaves`
|
|
1176
|
+
* in the binary tree. It is optional and defaults to a default callback function if not provided.
|
|
1177
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `leaves`
|
|
1151
1178
|
* method is used to specify the starting point for finding and processing the leaves of a binary
|
|
1152
|
-
* tree. It
|
|
1153
|
-
*
|
|
1179
|
+
* tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
|
|
1180
|
+
* explicitly provided, the default value
|
|
1154
1181
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `leaves` method
|
|
1155
1182
|
* specifies the type of iteration to be performed when collecting the leaves of a binary tree. It
|
|
1156
1183
|
* can have two possible values:
|
|
1157
1184
|
* @returns The `leaves` method returns an array of values that are the result of applying the
|
|
1158
|
-
* provided callback function to
|
|
1185
|
+
* provided callback function to each leaf node in the binary tree.
|
|
1159
1186
|
*/
|
|
1160
|
-
leaves(callback = this.
|
|
1187
|
+
leaves(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
|
|
1161
1188
|
beginRoot = this.ensureNode(beginRoot);
|
|
1162
1189
|
const leaves = [];
|
|
1163
|
-
if (!this.isRealNode(beginRoot))
|
|
1190
|
+
if (!this.isRealNode(beginRoot))
|
|
1164
1191
|
return [];
|
|
1165
|
-
}
|
|
1166
1192
|
if (iterationType === 'RECURSIVE') {
|
|
1167
1193
|
const dfs = (cur) => {
|
|
1168
1194
|
if (this.isLeaf(cur)) {
|
|
@@ -1170,8 +1196,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1170
1196
|
}
|
|
1171
1197
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
1172
1198
|
return;
|
|
1173
|
-
this.isRealNode(cur.left)
|
|
1174
|
-
|
|
1199
|
+
if (this.isRealNode(cur.left))
|
|
1200
|
+
dfs(cur.left);
|
|
1201
|
+
if (this.isRealNode(cur.right))
|
|
1202
|
+
dfs(cur.right);
|
|
1175
1203
|
};
|
|
1176
1204
|
dfs(beginRoot);
|
|
1177
1205
|
}
|
|
@@ -1183,8 +1211,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1183
1211
|
if (this.isLeaf(cur)) {
|
|
1184
1212
|
leaves.push(callback(cur));
|
|
1185
1213
|
}
|
|
1186
|
-
this.isRealNode(cur.left)
|
|
1187
|
-
|
|
1214
|
+
if (this.isRealNode(cur.left))
|
|
1215
|
+
queue.push(cur.left);
|
|
1216
|
+
if (this.isRealNode(cur.right))
|
|
1217
|
+
queue.push(cur.right);
|
|
1188
1218
|
}
|
|
1189
1219
|
}
|
|
1190
1220
|
}
|
|
@@ -1194,24 +1224,27 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1194
1224
|
* Time complexity: O(n)
|
|
1195
1225
|
* Space complexity: O(n)
|
|
1196
1226
|
*
|
|
1197
|
-
* The `listLevels` function
|
|
1198
|
-
*
|
|
1199
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1200
|
-
* the tree. It
|
|
1201
|
-
*
|
|
1202
|
-
* @param {
|
|
1203
|
-
* starting point for traversing the tree. It can be
|
|
1204
|
-
*
|
|
1205
|
-
*
|
|
1206
|
-
* @param {IterationType} iterationType - The `iterationType` parameter
|
|
1207
|
-
* iteration to be performed on the binary tree. It can have two
|
|
1208
|
-
*
|
|
1209
|
-
*
|
|
1210
|
-
*
|
|
1211
|
-
*
|
|
1212
|
-
*
|
|
1227
|
+
* The `listLevels` function in TypeScript generates a list of nodes at each level of a binary tree,
|
|
1228
|
+
* using either recursive or iterative traversal based on the specified iteration type.
|
|
1229
|
+
* @param {C} callback - The `callback` parameter is a function that will be applied to each node in
|
|
1230
|
+
* the binary tree during the traversal. It is used to process each node and determine what
|
|
1231
|
+
* information to include in the output for each level of the tree.
|
|
1232
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1233
|
+
* `listLevels` function represents the starting point for traversing the binary tree. It can be
|
|
1234
|
+
* either a key, a node, or an entry in the binary tree. If not provided, the default value is the
|
|
1235
|
+
* root of the binary tree.
|
|
1236
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `listLevels` function
|
|
1237
|
+
* determines the type of iteration to be performed on the binary tree nodes. It can have two
|
|
1238
|
+
* possible values:
|
|
1239
|
+
* @param [includeNull=false] - The `includeNull` parameter in the `listLevels` method determines
|
|
1240
|
+
* whether or not to include null nodes in the traversal of the binary tree. If `includeNull` is set
|
|
1241
|
+
* to `true`, the traversal will include null nodes in the levels of the tree. If set to `false`,
|
|
1242
|
+
* null
|
|
1243
|
+
* @returns The `listLevels` method returns an array of arrays, where each inner array represents a
|
|
1244
|
+
* level in a binary tree. Each inner array contains the return value of the provided callback
|
|
1245
|
+
* function applied to the nodes at that level.
|
|
1213
1246
|
*/
|
|
1214
|
-
listLevels(callback = this.
|
|
1247
|
+
listLevels(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType, includeNull = false) {
|
|
1215
1248
|
beginRoot = this.ensureNode(beginRoot);
|
|
1216
1249
|
const levelsNodes = [];
|
|
1217
1250
|
if (!beginRoot)
|
|
@@ -1264,24 +1297,25 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1264
1297
|
* Time complexity: O(n)
|
|
1265
1298
|
* Space complexity: O(n)
|
|
1266
1299
|
*
|
|
1267
|
-
* The `morris` function performs a
|
|
1268
|
-
* algorithm.
|
|
1269
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1270
|
-
*
|
|
1271
|
-
*
|
|
1272
|
-
* the
|
|
1273
|
-
*
|
|
1274
|
-
*
|
|
1275
|
-
*
|
|
1276
|
-
*
|
|
1277
|
-
*
|
|
1278
|
-
*
|
|
1279
|
-
* @returns The
|
|
1280
|
-
* callback function
|
|
1300
|
+
* The `morris` function in TypeScript performs a Depth-First Search traversal on a binary tree using
|
|
1301
|
+
* Morris Traversal algorithm with different order patterns.
|
|
1302
|
+
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
|
|
1303
|
+
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the
|
|
1304
|
+
* `BTNCallback<NODE>` type. The default value for `callback` is `this._DEFAULT
|
|
1305
|
+
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
|
|
1306
|
+
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
|
|
1307
|
+
* values for the `pattern` parameter are:
|
|
1308
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `morris`
|
|
1309
|
+
* function is the starting point for the Morris traversal algorithm. It represents the root node of
|
|
1310
|
+
* the binary tree or the node from which the traversal should begin. It can be provided as either a
|
|
1311
|
+
* key, a node, an entry, or a reference
|
|
1312
|
+
* @returns The `morris` function is returning an array of values that are the result of applying the
|
|
1313
|
+
* provided callback function to each node in the binary tree in the specified order pattern (IN,
|
|
1314
|
+
* PRE, or POST).
|
|
1281
1315
|
*/
|
|
1282
|
-
morris(callback = this.
|
|
1316
|
+
morris(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root) {
|
|
1283
1317
|
beginRoot = this.ensureNode(beginRoot);
|
|
1284
|
-
if (beginRoot
|
|
1318
|
+
if (!beginRoot)
|
|
1285
1319
|
return [];
|
|
1286
1320
|
const ans = [];
|
|
1287
1321
|
let cur = beginRoot;
|
|
@@ -1368,8 +1402,12 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1368
1402
|
* Time complexity: O(n)
|
|
1369
1403
|
* Space complexity: O(n)
|
|
1370
1404
|
*
|
|
1371
|
-
* The `clone` function creates a deep copy of a tree
|
|
1372
|
-
*
|
|
1405
|
+
* The `clone` function creates a deep copy of a tree structure by traversing it using breadth-first
|
|
1406
|
+
* search.
|
|
1407
|
+
* @returns The `clone()` method is returning a cloned copy of the tree with the same structure and
|
|
1408
|
+
* values as the original tree. The method creates a new tree, iterates over the nodes of the
|
|
1409
|
+
* original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
|
|
1410
|
+
* the original tree is null, a null node is added to the cloned tree. If a node
|
|
1373
1411
|
*/
|
|
1374
1412
|
clone() {
|
|
1375
1413
|
const cloned = this.createTree();
|
|
@@ -1378,23 +1416,24 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1378
1416
|
cloned.add(null);
|
|
1379
1417
|
else
|
|
1380
1418
|
cloned.add([node.key, node.value]);
|
|
1381
|
-
}, this.
|
|
1419
|
+
}, this._root, this.iterationType, true);
|
|
1382
1420
|
return cloned;
|
|
1383
1421
|
}
|
|
1384
1422
|
/**
|
|
1385
1423
|
* Time Complexity: O(n)
|
|
1386
1424
|
* Space Complexity: O(n)
|
|
1387
1425
|
*
|
|
1388
|
-
* The `filter` function
|
|
1389
|
-
*
|
|
1390
|
-
*
|
|
1391
|
-
*
|
|
1392
|
-
* of the
|
|
1393
|
-
* @param {any} [thisArg] - The `thisArg` parameter
|
|
1394
|
-
*
|
|
1395
|
-
*
|
|
1396
|
-
*
|
|
1397
|
-
*
|
|
1426
|
+
* The `filter` function iterates over key-value pairs in a tree data structure and creates a new
|
|
1427
|
+
* tree with elements that satisfy a given predicate.
|
|
1428
|
+
* @param predicate - The `predicate` parameter in the `filter` method is a function that will be
|
|
1429
|
+
* called with four arguments: the `value` of the current entry, the `key` of the current entry, the
|
|
1430
|
+
* `index` of the current entry in the iteration, and the reference to the tree itself (`
|
|
1431
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `filter` method allows you to specify the
|
|
1432
|
+
* value of `this` that should be used when executing the `predicate` function. This is useful when
|
|
1433
|
+
* the `predicate` function relies on the context of a specific object or value. By providing a
|
|
1434
|
+
* `thisArg
|
|
1435
|
+
* @returns The `filter` method is returning a new tree that contains entries that pass the provided
|
|
1436
|
+
* predicate function.
|
|
1398
1437
|
*/
|
|
1399
1438
|
filter(predicate, thisArg) {
|
|
1400
1439
|
const newTree = this.createTree();
|
|
@@ -1410,16 +1449,16 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1410
1449
|
* Time Complexity: O(n)
|
|
1411
1450
|
* Space Complexity: O(n)
|
|
1412
1451
|
*
|
|
1413
|
-
* The `map` function
|
|
1414
|
-
* tree.
|
|
1415
|
-
* @param callback - The callback parameter is a function that will be called
|
|
1416
|
-
* tree. It takes
|
|
1417
|
-
*
|
|
1418
|
-
*
|
|
1419
|
-
*
|
|
1420
|
-
*
|
|
1421
|
-
*
|
|
1422
|
-
*
|
|
1452
|
+
* The `map` function iterates over key-value pairs in a tree data structure, applies a callback
|
|
1453
|
+
* function to each value, and returns a new tree with the updated values.
|
|
1454
|
+
* @param callback - The `callback` parameter in the `map` method is a function that will be called
|
|
1455
|
+
* on each entry in the tree. It takes four arguments:
|
|
1456
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` function is an optional parameter
|
|
1457
|
+
* that specifies the value to be passed as `this` when executing the callback function. If provided,
|
|
1458
|
+
* the `thisArg` value will be used as the `this` value within the callback function. If `thisArg
|
|
1459
|
+
* @returns The `map` method is returning a new tree with the entries modified by the provided
|
|
1460
|
+
* callback function. Each entry in the original tree is passed to the callback function, and the
|
|
1461
|
+
* result of the callback function is added to the new tree.
|
|
1423
1462
|
*/
|
|
1424
1463
|
map(callback, thisArg) {
|
|
1425
1464
|
const newTree = this.createTree();
|
|
@@ -1442,17 +1481,21 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1442
1481
|
* Time Complexity: O(n)
|
|
1443
1482
|
* Space Complexity: O(n)
|
|
1444
1483
|
*
|
|
1445
|
-
* The `
|
|
1446
|
-
*
|
|
1447
|
-
*
|
|
1448
|
-
*
|
|
1449
|
-
*
|
|
1450
|
-
*
|
|
1451
|
-
*
|
|
1452
|
-
*
|
|
1453
|
-
*
|
|
1484
|
+
* The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
|
|
1485
|
+
* customizable options for displaying undefined, null, and sentinel nodes.
|
|
1486
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
1487
|
+
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
|
|
1488
|
+
* It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
|
|
1489
|
+
* the default is set to the root
|
|
1490
|
+
* @param {BinaryTreePrintOptions} [options] - The `options` parameter in the `toVisual` method is an
|
|
1491
|
+
* object that contains the following properties:
|
|
1492
|
+
* @returns The `override toVisual` method returns a string that represents the visual display of the
|
|
1493
|
+
* binary tree based on the provided options for showing undefined, null, and Red-Black NIL nodes.
|
|
1494
|
+
* The method constructs the visual representation by calling the `_displayAux` method and appending
|
|
1495
|
+
* the lines to the output string. The final output string contains the visual representation of the
|
|
1496
|
+
* binary tree with the specified options.
|
|
1454
1497
|
*/
|
|
1455
|
-
|
|
1498
|
+
toVisual(beginRoot = this._root, options) {
|
|
1456
1499
|
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1457
1500
|
beginRoot = this.ensureNode(beginRoot);
|
|
1458
1501
|
let output = '';
|
|
@@ -1482,50 +1525,49 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1482
1525
|
* Time complexity: O(n)
|
|
1483
1526
|
* Space complexity: O(n)
|
|
1484
1527
|
*
|
|
1485
|
-
* The
|
|
1528
|
+
* The `_dfs` function performs a depth-first search traversal on a binary tree structure based on
|
|
1486
1529
|
* the specified order pattern and callback function.
|
|
1487
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
1488
|
-
* visited during the depth-first search. It is of type `C`, which
|
|
1489
|
-
* `BTNCallback<OptBTNOrNull<NODE>>`. The default value
|
|
1490
|
-
* provided.
|
|
1530
|
+
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
|
|
1531
|
+
* called on each node visited during the depth-first search traversal. It is of type `C`, which
|
|
1532
|
+
* extends `BTNCallback<OptBTNOrNull<NODE>>`. The default value for this parameter is `this._DEFAULT
|
|
1491
1533
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
|
|
1492
|
-
* order in which the Depth-First Search
|
|
1493
|
-
*
|
|
1494
|
-
* @param {
|
|
1534
|
+
* order in which the nodes are visited during the Depth-First Search traversal. It can have one of
|
|
1535
|
+
* the following values:
|
|
1536
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the `_dfs`
|
|
1495
1537
|
* method is used to specify the starting point for the depth-first search traversal in a binary
|
|
1496
|
-
* tree. It can be provided as either
|
|
1497
|
-
*
|
|
1538
|
+
* tree. It can be provided as either a `BTNKeyOrNodeOrEntry` object or a reference to the root node
|
|
1539
|
+
* of the tree. If no specific
|
|
1498
1540
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
|
|
1499
|
-
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal
|
|
1500
|
-
* can have two possible values:
|
|
1541
|
+
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a
|
|
1542
|
+
* binary tree. It can have two possible values:
|
|
1501
1543
|
* @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method is a boolean flag
|
|
1502
1544
|
* that determines whether null nodes should be included in the depth-first search traversal. If
|
|
1503
|
-
* `includeNull` is set to `true`,
|
|
1504
|
-
*
|
|
1545
|
+
* `includeNull` is set to `true`, null nodes will be considered during the traversal process. If it
|
|
1546
|
+
* is set to `false`,
|
|
1505
1547
|
* @param shouldVisitLeft - The `shouldVisitLeft` parameter is a function that takes a node as input
|
|
1506
1548
|
* and returns a boolean value. It is used to determine whether the left child of a node should be
|
|
1507
1549
|
* visited during the depth-first search traversal. By default, it checks if the node is truthy (not
|
|
1508
1550
|
* null or undefined
|
|
1509
|
-
* @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as
|
|
1510
|
-
*
|
|
1551
|
+
* @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as an
|
|
1552
|
+
* argument and returns a boolean value. It is used to determine whether the right child of a node
|
|
1511
1553
|
* should be visited during the depth-first search traversal. The default implementation checks if
|
|
1512
|
-
* the node is truthy before visiting the right child
|
|
1554
|
+
* the node is truthy before visiting the right child
|
|
1513
1555
|
* @param shouldVisitRoot - The `shouldVisitRoot` parameter is a function that takes a node as an
|
|
1514
|
-
* argument and returns a boolean value. It is used to determine whether
|
|
1556
|
+
* argument and returns a boolean value. It is used to determine whether the root node should be
|
|
1515
1557
|
* visited during the depth-first search traversal based on certain conditions. The default
|
|
1516
1558
|
* implementation checks if the node is a real node or null based
|
|
1517
|
-
* @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as
|
|
1518
|
-
*
|
|
1519
|
-
* depth-first search traversal. The default implementation
|
|
1520
|
-
*
|
|
1521
|
-
* @returns The `_dfs`
|
|
1559
|
+
* @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as an
|
|
1560
|
+
* argument and returns a boolean value indicating whether the node should be processed during the
|
|
1561
|
+
* depth-first search traversal. The default implementation checks if the node is a real node or null
|
|
1562
|
+
* based on the `includeNull` flag. If `
|
|
1563
|
+
* @returns The function `_dfs` returns an array of the return type of the callback function provided
|
|
1522
1564
|
* as input.
|
|
1523
1565
|
*/
|
|
1524
|
-
_dfs(callback = this.
|
|
1566
|
+
_dfs(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
|
|
1525
1567
|
if (includeNull)
|
|
1526
1568
|
return this.isRealNodeOrNull(node);
|
|
1527
1569
|
return this.isRealNode(node);
|
|
1528
|
-
}, shouldProcessRoot = node =>
|
|
1570
|
+
}, shouldProcessRoot = node => this.isRealNodeOrNull(node)) {
|
|
1529
1571
|
beginRoot = this.ensureNode(beginRoot);
|
|
1530
1572
|
if (!beginRoot)
|
|
1531
1573
|
return [];
|
|
@@ -1616,13 +1658,18 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1616
1658
|
* Time Complexity: O(1)
|
|
1617
1659
|
* Space Complexity: O(1)
|
|
1618
1660
|
*
|
|
1619
|
-
* The function `_getIterator`
|
|
1620
|
-
*
|
|
1621
|
-
* @param node - The `node` parameter
|
|
1622
|
-
* initially set to the root node of the
|
|
1623
|
-
*
|
|
1661
|
+
* The function `_getIterator` returns an iterable iterator for a binary tree data structure, either
|
|
1662
|
+
* using an iterative approach or a recursive approach based on the specified iteration type.
|
|
1663
|
+
* @param node - The `node` parameter in the `_getIterator` method represents the current node being
|
|
1664
|
+
* processed during iteration. It is initially set to the root node of the data structure (or the
|
|
1665
|
+
* node passed as an argument), and then it is traversed through the data structure based on the
|
|
1666
|
+
* iteration type specified (`ITER
|
|
1667
|
+
* @returns The `_getIterator` method returns an IterableIterator containing key-value pairs of nodes
|
|
1668
|
+
* in a binary tree structure. The method uses an iterative approach to traverse the tree based on
|
|
1669
|
+
* the `iterationType` property. If the `iterationType` is set to 'ITERATIVE', the method uses a
|
|
1670
|
+
* stack to perform an in-order traversal of the tree. If the `iterationType` is not 'ITERATIVE
|
|
1624
1671
|
*/
|
|
1625
|
-
*_getIterator(node = this.
|
|
1672
|
+
*_getIterator(node = this._root) {
|
|
1626
1673
|
if (!node)
|
|
1627
1674
|
return;
|
|
1628
1675
|
if (this.iterationType === 'ITERATIVE') {
|
|
@@ -1654,18 +1701,16 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1654
1701
|
* Time Complexity: O(n)
|
|
1655
1702
|
* Space Complexity: O(n)
|
|
1656
1703
|
*
|
|
1657
|
-
* The `_displayAux`
|
|
1658
|
-
*
|
|
1659
|
-
* @param
|
|
1660
|
-
* It can be
|
|
1661
|
-
*
|
|
1662
|
-
*
|
|
1663
|
-
*
|
|
1664
|
-
*
|
|
1665
|
-
*
|
|
1666
|
-
*
|
|
1667
|
-
* 3. `totalHeight`: The total height of the node display.
|
|
1668
|
-
* 4. `middleIndex`: The index of the middle character
|
|
1704
|
+
* The function `_displayAux` in TypeScript is responsible for generating the display layout of nodes
|
|
1705
|
+
* in a binary tree based on specified options.
|
|
1706
|
+
* @param node - The `node` parameter in the `_displayAux` function represents a node in a binary
|
|
1707
|
+
* tree. It can be either a valid node containing a key or a special type of node like null,
|
|
1708
|
+
* undefined, or a Red-Black tree NIL node. The function checks the type of the node and its
|
|
1709
|
+
* @param {BinaryTreePrintOptions} options - The `options` parameter in the `_displayAux` function
|
|
1710
|
+
* contains the following properties:
|
|
1711
|
+
* @returns The `_displayAux` function returns a `NodeDisplayLayout`, which is an array containing
|
|
1712
|
+
* information about how to display a node in a binary tree. The `NodeDisplayLayout` consists of four
|
|
1713
|
+
* elements:
|
|
1669
1714
|
*/
|
|
1670
1715
|
_displayAux(node, options) {
|
|
1671
1716
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
@@ -1719,19 +1764,21 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1719
1764
|
];
|
|
1720
1765
|
}
|
|
1721
1766
|
}
|
|
1722
|
-
|
|
1767
|
+
_DEFAULT_BTN_CALLBACK = (node) => (node ? node.key : undefined);
|
|
1723
1768
|
/**
|
|
1724
1769
|
* Time Complexity: O(1)
|
|
1725
1770
|
* Space Complexity: O(1)
|
|
1726
1771
|
*
|
|
1727
|
-
* The function
|
|
1728
|
-
* @param {
|
|
1729
|
-
*
|
|
1730
|
-
*
|
|
1731
|
-
* @param {
|
|
1732
|
-
* the properties will be swapped with
|
|
1733
|
-
*
|
|
1734
|
-
*
|
|
1772
|
+
* The _swapProperties function swaps key and value properties between two nodes in a binary tree.
|
|
1773
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} srcNode - The `srcNode` parameter in the
|
|
1774
|
+
* `_swapProperties` method can be either a BTNKeyOrNodeOrEntry object containing key and value
|
|
1775
|
+
* properties, or it can be of type R.
|
|
1776
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} destNode - The `destNode` parameter in the
|
|
1777
|
+
* `_swapProperties` method represents the node or entry where the properties will be swapped with
|
|
1778
|
+
* the `srcNode`. It can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>` or `R`. The method ensures that
|
|
1779
|
+
* both `srcNode
|
|
1780
|
+
* @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
|
|
1781
|
+
* with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
1735
1782
|
*/
|
|
1736
1783
|
_swapProperties(srcNode, destNode) {
|
|
1737
1784
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -1753,13 +1800,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1753
1800
|
* Time Complexity: O(1)
|
|
1754
1801
|
* Space Complexity: O(1)
|
|
1755
1802
|
*
|
|
1756
|
-
* The function replaces
|
|
1757
|
-
*
|
|
1758
|
-
*
|
|
1759
|
-
*
|
|
1760
|
-
*
|
|
1761
|
-
*
|
|
1762
|
-
*
|
|
1803
|
+
* The _replaceNode function replaces an old node with a new node in a binary tree structure.
|
|
1804
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that you want to replace in a
|
|
1805
|
+
* tree data structure.
|
|
1806
|
+
* @param {NODE} newNode - The `newNode` parameter in the `_replaceNode` function represents the node
|
|
1807
|
+
* that will replace the `oldNode` in a tree data structure. This function is responsible for
|
|
1808
|
+
* updating the parent, left child, right child, and root (if necessary) references when replacing a
|
|
1809
|
+
* node in the tree.
|
|
1810
|
+
* @returns The method `_replaceNode` is returning the `newNode` that was passed as a parameter after
|
|
1811
|
+
* replacing the `oldNode` with it in the binary tree structure.
|
|
1763
1812
|
*/
|
|
1764
1813
|
_replaceNode(oldNode, newNode) {
|
|
1765
1814
|
if (oldNode.parent) {
|
|
@@ -1773,8 +1822,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1773
1822
|
newNode.left = oldNode.left;
|
|
1774
1823
|
newNode.right = oldNode.right;
|
|
1775
1824
|
newNode.parent = oldNode.parent;
|
|
1776
|
-
if (this.
|
|
1777
|
-
this.
|
|
1825
|
+
if (this._root === oldNode) {
|
|
1826
|
+
this._setRoot(newNode);
|
|
1778
1827
|
}
|
|
1779
1828
|
return newNode;
|
|
1780
1829
|
}
|
|
@@ -1782,10 +1831,10 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1782
1831
|
* Time Complexity: O(1)
|
|
1783
1832
|
* Space Complexity: O(1)
|
|
1784
1833
|
*
|
|
1785
|
-
* The function sets the root
|
|
1786
|
-
*
|
|
1787
|
-
* @param
|
|
1788
|
-
*
|
|
1834
|
+
* The function _setRoot sets the root node of a data structure while updating the parent reference
|
|
1835
|
+
* of the previous root node.
|
|
1836
|
+
* @param v - The parameter `v` in the `_setRoot` method is of type `OptBTNOrNull<NODE>`, which means
|
|
1837
|
+
* it can either be an optional `NODE` type or `null`.
|
|
1789
1838
|
*/
|
|
1790
1839
|
_setRoot(v) {
|
|
1791
1840
|
if (v) {
|
|
@@ -1797,19 +1846,46 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1797
1846
|
* Time Complexity: O(1)
|
|
1798
1847
|
* Space Complexity: O(1)
|
|
1799
1848
|
*
|
|
1800
|
-
* The function `
|
|
1801
|
-
*
|
|
1802
|
-
*
|
|
1803
|
-
* the
|
|
1804
|
-
*
|
|
1805
|
-
*
|
|
1806
|
-
*
|
|
1807
|
-
* @returns the callback parameter.
|
|
1849
|
+
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
|
|
1850
|
+
* predicate function for a binary tree node.
|
|
1851
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} keyOrEntryOrRawOrPredicate - The
|
|
1852
|
+
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
|
|
1853
|
+
* parameter `keyOrEntryOrRawOrPredicate` is transformed into a valid predicate function that can be
|
|
1854
|
+
* used for filtering nodes in a binary tree.
|
|
1855
|
+
* @returns A BTNPredicate<NODE> function is being returned.
|
|
1808
1856
|
*/
|
|
1809
|
-
|
|
1810
|
-
if (
|
|
1811
|
-
|
|
1857
|
+
_ensurePredicate(keyOrEntryOrRawOrPredicate) {
|
|
1858
|
+
if (keyOrEntryOrRawOrPredicate === null || keyOrEntryOrRawOrPredicate === undefined)
|
|
1859
|
+
return (node) => (node ? false : false);
|
|
1860
|
+
if (this._isPredicated(keyOrEntryOrRawOrPredicate))
|
|
1861
|
+
return keyOrEntryOrRawOrPredicate;
|
|
1862
|
+
if (this.isRealNode(keyOrEntryOrRawOrPredicate))
|
|
1863
|
+
return (node) => node === keyOrEntryOrRawOrPredicate;
|
|
1864
|
+
if (this.isEntry(keyOrEntryOrRawOrPredicate)) {
|
|
1865
|
+
const [key] = keyOrEntryOrRawOrPredicate;
|
|
1866
|
+
return (node) => node.key === key;
|
|
1812
1867
|
}
|
|
1813
|
-
|
|
1868
|
+
if (this.isKey(keyOrEntryOrRawOrPredicate))
|
|
1869
|
+
return (node) => node.key === keyOrEntryOrRawOrPredicate;
|
|
1870
|
+
if (this._toEntryFn) {
|
|
1871
|
+
const [key] = this._toEntryFn(keyOrEntryOrRawOrPredicate);
|
|
1872
|
+
return (node) => node.key === key;
|
|
1873
|
+
}
|
|
1874
|
+
return (node) => node.key === keyOrEntryOrRawOrPredicate;
|
|
1875
|
+
}
|
|
1876
|
+
/**
|
|
1877
|
+
* Time Complexity: O(1)
|
|
1878
|
+
* Space Complexity: O(1)
|
|
1879
|
+
*
|
|
1880
|
+
* The function `_isPredicated` checks if a given parameter is a function.
|
|
1881
|
+
* @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type
|
|
1882
|
+
* of value. In this context, the function `_isPredicated` is checking if `p` is a function that
|
|
1883
|
+
* satisfies the type `BTNPredicate<NODE>`.
|
|
1884
|
+
* @returns The function is checking if the input `p` is a function and returning a boolean value
|
|
1885
|
+
* based on that check. If `p` is a function, it will return `true`, indicating that `p` is a
|
|
1886
|
+
* predicate function for a binary tree node. If `p` is not a function, it will return `false`.
|
|
1887
|
+
*/
|
|
1888
|
+
_isPredicated(p) {
|
|
1889
|
+
return typeof p === 'function';
|
|
1814
1890
|
}
|
|
1815
1891
|
}
|