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.
Files changed (181) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +33 -30
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +156 -156
  5. package/dist/cjs/constants/index.d.ts +4 -0
  6. package/dist/cjs/constants/index.js +9 -0
  7. package/dist/cjs/constants/index.js.map +1 -0
  8. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +8 -1
  9. package/dist/cjs/data-structures/base/iterable-element-base.js +10 -1
  10. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
  11. package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +8 -1
  12. package/dist/cjs/data-structures/base/iterable-entry-base.js +10 -10
  13. package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
  15. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
  16. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +23 -24
  18. package/dist/cjs/data-structures/binary-tree/avl-tree.js +71 -64
  19. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
  21. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
  22. package/dist/cjs/data-structures/binary-tree/binary-tree.js +669 -598
  23. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/bst.d.ts +72 -65
  25. package/dist/cjs/data-structures/binary-tree/bst.js +115 -113
  26. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  27. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
  28. package/dist/cjs/data-structures/binary-tree/rb-tree.js +40 -39
  29. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  30. package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
  31. package/dist/cjs/data-structures/binary-tree/segment-tree.js +2 -2
  32. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
  33. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +44 -43
  34. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  35. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +2 -2
  36. package/dist/cjs/data-structures/graph/abstract-graph.js +7 -4
  37. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  38. package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
  39. package/dist/cjs/data-structures/graph/directed-graph.js +4 -2
  40. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  41. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +2 -2
  42. package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -2
  43. package/dist/cjs/data-structures/hash/hash-map.js +1 -1
  44. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  45. package/dist/cjs/data-structures/heap/heap.js +3 -3
  46. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  47. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  48. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +7 -7
  49. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  50. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  51. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +6 -6
  52. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  53. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  54. package/dist/cjs/data-structures/matrix/matrix.d.ts +2 -2
  55. package/dist/cjs/data-structures/matrix/navigator.d.ts +2 -2
  56. package/dist/cjs/data-structures/matrix/navigator.js +4 -2
  57. package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
  58. package/dist/cjs/data-structures/queue/deque.d.ts +3 -3
  59. package/dist/cjs/data-structures/queue/deque.js +29 -29
  60. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  61. package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
  62. package/dist/cjs/data-structures/stack/stack.d.ts +2 -2
  63. package/dist/cjs/data-structures/tree/tree.js +2 -0
  64. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  65. package/dist/cjs/data-structures/trie/trie.d.ts +2 -2
  66. package/dist/cjs/data-structures/trie/trie.js +1 -1
  67. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  68. package/dist/cjs/index.d.ts +1 -0
  69. package/dist/cjs/index.js +1 -0
  70. package/dist/cjs/index.js.map +1 -1
  71. package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
  72. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
  73. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -6
  74. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
  75. package/dist/mjs/constants/index.d.ts +4 -0
  76. package/dist/mjs/constants/index.js +5 -0
  77. package/dist/mjs/data-structures/base/iterable-element-base.d.ts +8 -1
  78. package/dist/mjs/data-structures/base/iterable-element-base.js +10 -1
  79. package/dist/mjs/data-structures/base/iterable-entry-base.d.ts +8 -1
  80. package/dist/mjs/data-structures/base/iterable-entry-base.js +10 -10
  81. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
  82. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +44 -44
  83. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +23 -24
  84. package/dist/mjs/data-structures/binary-tree/avl-tree.js +72 -64
  85. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
  86. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
  87. package/dist/mjs/data-structures/binary-tree/binary-tree.js +668 -592
  88. package/dist/mjs/data-structures/binary-tree/bst.d.ts +72 -65
  89. package/dist/mjs/data-structures/binary-tree/bst.js +116 -112
  90. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
  91. package/dist/mjs/data-structures/binary-tree/rb-tree.js +41 -38
  92. package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
  93. package/dist/mjs/data-structures/binary-tree/segment-tree.js +2 -2
  94. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
  95. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +45 -42
  96. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +2 -2
  97. package/dist/mjs/data-structures/graph/abstract-graph.js +7 -4
  98. package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
  99. package/dist/mjs/data-structures/graph/directed-graph.js +4 -2
  100. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +2 -2
  101. package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -2
  102. package/dist/mjs/data-structures/hash/hash-map.js +1 -1
  103. package/dist/mjs/data-structures/heap/heap.js +3 -3
  104. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  105. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +7 -7
  106. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  107. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +6 -6
  108. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  109. package/dist/mjs/data-structures/matrix/matrix.d.ts +2 -2
  110. package/dist/mjs/data-structures/matrix/navigator.d.ts +2 -2
  111. package/dist/mjs/data-structures/matrix/navigator.js +4 -2
  112. package/dist/mjs/data-structures/queue/deque.d.ts +3 -3
  113. package/dist/mjs/data-structures/queue/deque.js +29 -29
  114. package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
  115. package/dist/mjs/data-structures/stack/stack.d.ts +2 -2
  116. package/dist/mjs/data-structures/tree/tree.js +2 -0
  117. package/dist/mjs/data-structures/trie/trie.d.ts +2 -2
  118. package/dist/mjs/data-structures/trie/trie.js +1 -1
  119. package/dist/mjs/index.d.ts +1 -0
  120. package/dist/mjs/index.js +1 -0
  121. package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
  122. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
  123. package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -5
  124. package/dist/umd/data-structure-typed.js +1344 -1614
  125. package/dist/umd/data-structure-typed.min.js +10 -3
  126. package/dist/umd/data-structure-typed.min.js.map +1 -1
  127. package/eslint.config.mjs +69 -0
  128. package/package.json +30 -28
  129. package/src/constants/index.ts +4 -0
  130. package/src/data-structures/base/iterable-element-base.ts +11 -1
  131. package/src/data-structures/base/iterable-entry-base.ts +11 -19
  132. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
  133. package/src/data-structures/binary-tree/avl-tree.ts +69 -71
  134. package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
  135. package/src/data-structures/binary-tree/binary-tree.ts +698 -726
  136. package/src/data-structures/binary-tree/bst.ts +123 -129
  137. package/src/data-structures/binary-tree/rb-tree.ts +44 -46
  138. package/src/data-structures/binary-tree/segment-tree.ts +2 -2
  139. package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
  140. package/src/data-structures/graph/abstract-graph.ts +6 -6
  141. package/src/data-structures/graph/directed-graph.ts +4 -4
  142. package/src/data-structures/graph/undirected-graph.ts +2 -2
  143. package/src/data-structures/hash/hash-map.ts +3 -3
  144. package/src/data-structures/heap/heap.ts +3 -3
  145. package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
  146. package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
  147. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  148. package/src/data-structures/matrix/matrix.ts +2 -2
  149. package/src/data-structures/matrix/navigator.ts +4 -4
  150. package/src/data-structures/queue/deque.ts +31 -31
  151. package/src/data-structures/queue/queue.ts +1 -1
  152. package/src/data-structures/stack/stack.ts +2 -2
  153. package/src/data-structures/trie/trie.ts +3 -3
  154. package/src/index.ts +1 -0
  155. package/src/interfaces/binary-tree.ts +3 -3
  156. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
  157. package/test/config.ts +1 -7
  158. package/test/integration/all-in-one.test.ts +2 -2
  159. package/test/integration/avl-tree.test.ts +3 -3
  160. package/test/integration/bst.test.ts +19 -18
  161. package/test/integration/heap.test.js +6 -1
  162. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +39 -39
  163. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
  164. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +140 -112
  165. package/test/unit/data-structures/binary-tree/bst.test.ts +41 -13
  166. package/test/unit/data-structures/binary-tree/overall.test.ts +0 -6
  167. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +6 -6
  168. package/test/unit/data-structures/binary-tree/segment-tree.test.ts +88 -34
  169. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +42 -42
  170. package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
  171. package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
  172. package/test/unit/data-structures/graph/undirected-graph.test.ts +14 -2
  173. package/test/unit/data-structures/hash/hash-map.test.ts +1 -1
  174. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
  175. package/test/unit/data-structures/matrix/navigator.test.ts +2 -2
  176. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -1
  177. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -4
  178. package/test/unit/data-structures/stack/stack.test.ts +6 -0
  179. package/test/unit/unrestricted-interconversion.test.ts +24 -24
  180. package/test/utils/big-o.ts +5 -4
  181. package/.eslintrc.js +0 -64
@@ -1,14 +1,14 @@
1
1
  /**
2
2
  * data-structure-typed
3
3
  *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
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 function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
102
- * @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of BTNKeyOrNodeOrEntry objects. These objects represent the
103
- * nodes to be added to the binary tree.
104
- * @param [options] - The `options` parameter is an optional object that can contain additional
105
- * configuration options for the binary tree. In this case, it is of type
106
- * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
107
- * required.
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(keysOrNodesOrEntriesOrRawElements = [], options) {
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 (keysOrNodesOrEntriesOrRawElements)
121
- this.addMany(keysOrNodesOrEntriesOrRawElements);
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
- * Creates a new instance of BinaryTreeNode with the given key and value.
158
- * @param {K} key - The key for the new node.
159
- * @param {V} value - The value for the new node.
160
- * @returns {NODE} - The newly created BinaryTreeNode.
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 given options.
167
- * @param [options] - The `options` parameter is an optional object that allows you to customize the
168
- * behavior of the `BinaryTree` class. It is of type `Partial<BinaryTreeOptions>`, which means that
169
- * you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
170
- * @returns a new instance of a binary tree.
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([], { iterationType: this.iterationType, ...options });
128
+ return new BinaryTree([], {
129
+ iterationType: this.iterationType,
130
+ toEntryFn: this._toEntryFn,
131
+ ...options
132
+ });
174
133
  }
175
134
  /**
176
- * The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
177
- * into a node object.
178
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
179
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
180
- * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
181
- * `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
182
- * key-value pair. If provided, it will be used to create a node with the specified key and value.
183
- * @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
184
- * or `undefined`.
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(keyOrNodeOrEntryOrRawElement, value) {
187
- if (keyOrNodeOrEntryOrRawElement === undefined)
149
+ keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value) {
150
+ if (keyOrNodeOrEntryOrRaw === undefined)
188
151
  return;
189
- if (keyOrNodeOrEntryOrRawElement === null)
152
+ if (keyOrNodeOrEntryOrRaw === null)
190
153
  return null;
191
- if (this.isNode(keyOrNodeOrEntryOrRawElement))
192
- return keyOrNodeOrEntryOrRawElement;
193
- if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
194
- const [key, entryValue] = keyOrNodeOrEntryOrRawElement;
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.toEntryFn) {
203
- const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
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(keyOrNodeOrEntryOrRawElement))
210
- return this.createNode(keyOrNodeOrEntryOrRawElement, value);
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` function checks if the input is a valid node and returns it, or converts it to a
218
- * node if it is a key or entry.
219
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
220
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
221
- * a raw element.
222
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
223
- * parameter that specifies the type of iteration to be used when searching for a node. It has a
224
- * default value of `'ITERATIVE'`.
225
- * @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
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(keyOrNodeOrEntryOrRawElement, iterationType = this.iterationType) {
228
- if (keyOrNodeOrEntryOrRawElement === null)
191
+ ensureNode(keyOrNodeOrEntryOrRaw, iterationType = this.iterationType) {
192
+ if (keyOrNodeOrEntryOrRaw === null)
229
193
  return null;
230
- if (keyOrNodeOrEntryOrRawElement === undefined)
194
+ if (keyOrNodeOrEntryOrRaw === undefined)
231
195
  return;
232
- if (keyOrNodeOrEntryOrRawElement === this.NIL)
196
+ if (keyOrNodeOrEntryOrRaw === this._NIL)
233
197
  return;
234
- if (this.isNode(keyOrNodeOrEntryOrRawElement))
235
- return keyOrNodeOrEntryOrRawElement;
236
- if (this.toEntryFn) {
237
- const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
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.isKey(keyOrNodeOrEntryOrRawElement))
250
- return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
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 the BinaryTreeNode class.
255
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
256
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
257
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
258
- * an instance of the `BinaryTreeNode` class.
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(keyOrNodeOrEntryOrRawElement) {
261
- return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
228
+ isNode(keyOrNodeOrEntryOrRaw) {
229
+ return keyOrNodeOrEntryOrRaw instanceof BinaryTreeNode;
262
230
  }
263
231
  /**
264
- * The function checks if a given node is a valid node in a binary search tree.
265
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
266
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
267
- * @returns a boolean value.
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(node) {
270
- if (node === this.NIL || node === null || node === undefined)
241
+ isRealNode(keyOrNodeOrEntryOrRaw) {
242
+ if (keyOrNodeOrEntryOrRaw === this._NIL || keyOrNodeOrEntryOrRaw === null || keyOrNodeOrEntryOrRaw === undefined)
271
243
  return false;
272
- return this.isNode(node);
244
+ return this.isNode(keyOrNodeOrEntryOrRaw);
273
245
  }
274
246
  /**
275
- * The function checks if a given node is a real node or null.
276
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
277
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
278
- * @returns a boolean value.
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(node) {
281
- return this.isRealNode(node) || node === null;
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 NIL value.
285
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
286
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
287
- * @returns a boolean value.
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(node) {
290
- return node === this.NIL;
265
+ isNIL(keyOrNodeOrEntryOrRaw) {
266
+ return keyOrNodeOrEntryOrRaw === this._NIL;
291
267
  }
292
268
  /**
293
- * The function `isLeaf` determines whether a given node is a leaf node in a binary tree structure.
294
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter in the `isLeaf` function
295
- * can be either a regular node (`R`) or a `BTNKeyOrNodeOrEntry<K, V, NODE>`.
296
- * @returns The `isLeaf` function is checking if the provided node is a leaf node in a binary tree.
297
- * If the node is `undefined`, it returns `false`. If the node is `null`, it returns `true`.
298
- * Otherwise, it checks if both the left and right children of the node are not real nodes, and
299
- * returns `true` if they are not, indicating that the node is a
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(node) {
302
- node = this.ensureNode(node);
303
- if (node === undefined)
278
+ isLeaf(keyOrNodeOrEntryOrRaw) {
279
+ keyOrNodeOrEntryOrRaw = this.ensureNode(keyOrNodeOrEntryOrRaw);
280
+ if (keyOrNodeOrEntryOrRaw === undefined)
304
281
  return false;
305
- if (node === null)
282
+ if (keyOrNodeOrEntryOrRaw === null)
306
283
  return true;
307
- return !this.isRealNode(node.left) && !this.isRealNode(node.right);
284
+ return !this.isRealNode(keyOrNodeOrEntryOrRaw.left) && !this.isRealNode(keyOrNodeOrEntryOrRaw.right);
308
285
  }
309
286
  /**
310
- * The function checks if the input is an array with two elements, indicating it is a binary tree
311
- * node entry.
312
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
313
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
314
- * @returns a boolean value.
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(keyOrNodeOrEntryOrRawElement) {
317
- return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
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 is used to insert a new node into a binary tree, checking for duplicate keys
340
- * and finding the appropriate insertion position.
341
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
342
- * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
343
- * node, entry, or raw element to be added to the tree. It can also accept a value of type
344
- * `BTNKeyOrNodeOrEntry<K, V, NODE>
345
- * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
346
- * key being added to the tree. It represents the value that will be stored in the tree for the given
347
- * key.
348
- * @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
349
- * insertion position cannot be found or if there are duplicate keys.
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(keyOrNodeOrEntryOrRawElement, value) {
352
- const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
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.root) {
357
- this._root = newNode;
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.root]);
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
- cur.left && queue.push(cur.left);
359
+ if (cur.left)
360
+ queue.push(cur.left);
379
361
  }
380
362
  if (cur.right !== null) {
381
- cur.right && queue.push(cur.right);
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 an iterable of keys or nodes or entries or raw elements, and an
402
- * optional iterable of values, and adds each key or node or entry with its corresponding value to a
403
- * data structure, returning an array of booleans indicating whether each insertion was successful.
404
- * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
405
- * elements. These elements will be added to the data structure.
406
- * @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
407
- * in the `keysOrNodesOrEntriesOrRawElements` parameter.
408
- * @returns The function `addMany` returns an array of booleans indicating whether each element was
409
- * successfully added to the data structure.
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(keysOrNodesOrEntriesOrRawElements, values) {
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 keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
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(keyOrNodeOrEntryOrRawElement, value));
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 current data and adds new data to the collection.
435
- * @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
436
- * elements. These can be of any type (R) or a specific type (BTNKeyOrNodeOrEntry<K, V, NODE>).
437
- * @param [values] - The `values` parameter is an optional iterable of values that will be associated
438
- * with the keys or nodes being added. If provided, the values will be assigned to the corresponding
439
- * keys or nodes. If not provided, the values will be set to `undefined`.
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(keysOrNodesOrEntriesOrRawElements, values) {
429
+ refill(keysOrNodesOrEntriesOrRaws, values) {
442
430
  this.clear();
443
- this.addMany(keysOrNodesOrEntriesOrRawElements, values);
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 above function is a TypeScript implementation of deleting a node from a binary tree, returning
450
- * the deleted node and the node that needs to be balanced.
451
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
452
- * used to identify the node that needs to be deleted from the binary tree. It can be of any type
453
- * that is returned by the callback function.
454
- * @param {C} callback - The `callback` parameter is a function that is used to determine the
455
- * identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
456
- * interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
457
- * type `NODE
458
- * @returns an array of `BinaryTreeDeleteResult<NODE>`.
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(identifier, callback = this._DEFAULT_CALLBACK) {
448
+ delete(keyOrNodeOrEntryOrRawOrPredicate) {
461
449
  const deletedResult = [];
462
- if (!this.root)
450
+ if (!this._root)
463
451
  return deletedResult;
464
- callback = this._ensureCallback(identifier, callback);
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.size - 1;
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` returns an array of nodes that match a given identifier, using either a
511
- * recursive or iterative approach.
512
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
513
- * that is used to identify the nodes. It can be of any type and is used to match against the result
514
- * of the callback function for each node.
515
- * @param {C} callback - The `callback` parameter is a function that takes a node as input and
516
- * returns a value. This value is used to identify the nodes that match the given identifier. The
517
- * `callback` function is optional and defaults to a default callback function
518
- * (`this._DEFAULT_CALLBACK`) if not provided.
519
- * @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
520
- * the identifier or all nodes that match the identifier. If set to true, only the first matching
521
- * node will be returned. If set to false, all matching nodes will be returned. The default value is
522
- * false.
523
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
524
- * point for the search. It can be either a node object, a key-value pair, or a key. If it is not
525
- * provided, the `root` of the data structure is used as the starting point.
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(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
531
- if (identifier === undefined)
514
+ getNodes(keyOrNodeOrEntryOrRawOrPredicate, onlyOne = false, beginRoot = this._root, iterationType = this.iterationType) {
515
+ if (keyOrNodeOrEntryOrRawOrPredicate === undefined)
532
516
  return [];
533
- if (identifier === null)
517
+ if (keyOrNodeOrEntryOrRawOrPredicate === null)
534
518
  return [];
535
519
  beginRoot = this.ensureNode(beginRoot);
536
520
  if (!beginRoot)
537
521
  return [];
538
- callback = this._ensureCallback(identifier, callback);
522
+ const callback = this._ensurePredicate(keyOrNodeOrEntryOrRawOrPredicate);
539
523
  const ans = [];
540
524
  if (iterationType === 'RECURSIVE') {
541
525
  const dfs = (cur) => {
542
- if (callback(cur) === identifier) {
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) && dfs(cur.left);
550
- this.isRealNode(cur.right) && dfs(cur.right);
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) === identifier) {
545
+ if (callback(cur)) {
560
546
  ans.push(cur);
561
547
  if (onlyOne)
562
548
  return ans;
563
549
  }
564
- this.isRealNode(cur.left) && stack.push(cur.left);
565
- this.isRealNode(cur.right) && stack.push(cur.right);
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 function `getNode` returns the first node that matches the given identifier and callback,
576
- * starting from the specified root node and using the specified iteration type.
577
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
578
- * used to identify the node you want to retrieve. It can be of any type that is the return type of
579
- * the `C` callback function, or it can be `null` or `undefined`.
580
- * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
581
- * node matches the desired criteria. It should return a value that can be used to identify the node.
582
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
583
- * point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
584
- * a node entry. If not provided, the search will start from the root of the tree.
585
- * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
586
- * of iteration to be performed when searching for nodes. It can have one of the following values:
587
- * @returns The method is returning a NODE object, or null, or undefined.
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(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
590
- return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
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` returns a node with a specific key value from a tree structure.
597
- * @param {K} key - The key parameter is the value that you want to search for in the tree. It is
598
- * used to find the node with the matching key value.
599
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
600
- * parameter that specifies the type of iteration to be used when searching for a node in the tree.
601
- * It has a default value of `'ITERATIVE'`.
602
- * @returns a value of type NODE, null, or undefined.
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._DEFAULT_CALLBACK, this.root, iterationType);
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
- * The function `get` in TypeScript overrides the base class method and returns the value associated
612
- * with the given identifier.
613
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
614
- * used to identify the node in the binary tree. It can be of any type that is returned by the
615
- * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
616
- * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
617
- * node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
618
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
619
- * point for the search in the binary tree. It can be either a root node of the tree or a key, node,
620
- * or entry object that exists in the tree. If no specific starting point is provided, the search
621
- * will begin from the root of the
622
- * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
623
- * of iteration to be performed when searching for a node in the tree. It can have one of the
624
- * following values:
625
- * @returns The method is returning the value associated with the specified identifier in the binary
626
- * tree.
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(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
629
- return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
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 given identifier exists in the data structure and returns a boolean
636
- * value.
637
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
638
- * used to identify a specific node or entry in the data structure. It can be of any type that is
639
- * returned by the callback function `C`. It can also be `null` or `undefined` if no specific
640
- * identifier is provided.
641
- * @param {C} callback - The `callback` parameter is a function that will be used to determine
642
- * whether a node should be included in the result or not. It is of type `C`, which extends the
643
- * `BTNCallback<NODE>` type.
644
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
645
- * point for the iteration in the data structure. It can be either a root node, a key-value pair, or
646
- * a node entry. If not specified, it defaults to the root of the data structure.
647
- * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
648
- * of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
649
- * @returns The method is returning a boolean value.
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(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
652
- callback = this._ensureCallback(identifier, callback);
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
- * Clear the binary tree, removing all nodes.
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
- * Check if the binary tree is empty.
670
- * @returns {boolean} - True if the binary tree is empty, false otherwise.
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.size === 0;
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 the minimum height and the
680
- * height of the tree.
681
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
682
- * has a default value of `this.root`. It represents the starting point for checking if the tree is
683
- * perfectly balanced. It can be either a root node (`R`), a key or node or entry
684
- * (`BTNKeyOrNodeOrEntry<K, V, NODE
685
- * @returns a boolean value.
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.root) {
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, either recursively or iteratively.
695
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
696
- * starting point for checking if a binary search tree (BST) is valid. It can be either a root node
697
- * of the BST, a key value of a node in the BST, or an entry object containing both the key and value
698
- * of a node in the BST
699
- * @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
700
- * of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
701
- * two possible values:
702
- * @returns a boolean value.
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.root, iterationType = this.iterationType) {
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 of a given node or key in a tree-like data structure.
751
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
752
- * (representing a root node), or a `BTNKeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
753
- * entry).
754
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
755
- * represents the starting point from which to calculate the depth. It can be either a reference to a
756
- * node in the tree or a key-value pair or an entry object. If not provided, the default value is
757
- * `this.root`, which refers to the root node
758
- * @returns the depth of a node in a tree structure.
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.root) {
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 {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
780
- * starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
781
- * node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
782
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
783
- * iteration used to calculate the height of the tree. It can have two possible values:
784
- * @returns the maximum height of the binary tree.
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.root, iterationType = this.iterationType) {
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 {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
821
- * starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
822
- * key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
823
- * tree.
824
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
825
- * iteration to be used when calculating the minimum height of the tree. It can have two possible
826
- * values:
827
- * @returns The function `getMinHeight` returns a number, which represents the minimum height of the
828
- * binary tree.
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.root, iterationType = this.iterationType) {
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) ? (depths.get(node.left) ?? -1) : -1;
861
- const rightMinHeight = this.isRealNode(node.right) ? (depths.get(node.right) ?? -1) : -1;
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) ?? -1;
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` returns an array of nodes starting from a given node and traversing
879
- * up to the root node, with an option to reverse the order of the nodes.
880
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
881
- * type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
882
- * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
883
- * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
884
- * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
885
- * @returns The function `getPathToRoot` returns an array of `NODE` objects.
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 null if the tree is empty. It has a default value of `_DEFAULT_CALLBACK`
908
- * if not provided explicitly.
909
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the
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 reference to the root node of the tree (`R`), or a key, node, or entry in
912
- * the binary tree structure (`
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 `NIL`,
918
- * it returns the result of the callback function applied to `undefined`. If the `beginRoot` is not a
919
- * real node, it returns the result of the callback function applied
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._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
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 operation. It has a generic type `C` which extends `BTNCallback<OptBTNOrNull<NODE>>`. The
953
- * default value for `callback` is `this._DEFAULT_CALLBACK` if it is not provided.
954
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the
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 reference to the root node of the tree (`this.root`) or a specific key,
957
- * node, or entry in the tree. If
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 finding the rightmost node in a binary
960
- * tree. It can have two possible values:
961
- * @returns The `getRightMost` function returns the result of the callback function `C` applied to
962
- * the rightmost node in the binary tree. The rightmost node is found either through a recursive
963
- * depth-first search (if `iterationType` is 'RECURSIVE') or through an indirect implementation of
964
- * iteration using tail recursion optimization. The result of the callback function applied to the
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._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
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 binary tree.
997
- * @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
998
- * tree.
999
- * @returns the predecessor node of the given node.
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 a binary tree given a current node.
1020
- * @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
1021
- * @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
1022
- * there is no successor, and `undefined` if the input `x` is not a valid node.
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` function performs a depth-first search traversal on a binary tree, executing a callback
1043
- * function on each node according to a specified pattern and iteration type.
1044
- * @param {C} callback - The `callback` parameter is a function that will be called for each node
1045
- * visited during the depth-first search. It takes a node as an argument and returns a value. The
1046
- * return type of the callback function is determined by the generic type `C`.
1047
- * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
1048
- * nodes are visited during the depth-first search. It can have one of the following values:
1049
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1050
- * point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
1051
- * is a key or key-value pair, the method will find the corresponding node in the tree and start the
1052
- * search from there.
1053
- * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
1054
- * type of iteration to use during the depth-first search. It can have two possible values:
1055
- * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1056
- * whether or not to include null values in the depth-first search traversal. If `includeNull` is set
1057
- * to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
1058
- * values will
1059
- * @returns an array of the return types of the callback function.
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._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
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, calling a callback function
1072
- * on each node and returning an array of the results.
1073
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1074
- * the breadth-first search traversal. It takes a single argument, which is the current node being
1075
- * visited, and returns a value of any type.
1076
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1077
- * starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
1078
- * or entry object. If no value is provided, the `root` property of the class is used as the default
1079
- * starting point.
1080
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1081
- * iteration to be performed. It can have two possible values:
1082
- * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1083
- * whether or not to include null values in the breadth-first search (BFS) traversal. If
1084
- * `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
1085
- * set to `false
1086
- * @returns The function `bfs` returns an array of values that are the result of invoking the
1087
- * `callback` function on each node in the breadth-first order traversal of the binary tree.
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._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
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 iterates through a binary tree to find and return the leaf
1146
- * nodes based on a specified callback and iteration type.
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 a generic type `C` that extends `BTNCallback<NODE | null>`, where `NODE`
1149
- * represents a node in the binary tree. The default value for `callback` is
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 represents the root node of the binary tree or a specific key, node, or entry within the
1153
- * tree from which the search for leaves should begin
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 the leaf nodes in the binary tree structure.
1185
+ * provided callback function to each leaf node in the binary tree.
1159
1186
  */
1160
- leaves(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
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) && dfs(cur.left);
1174
- this.isRealNode(cur.right) && dfs(cur.right);
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) && queue.push(cur.left);
1187
- this.isRealNode(cur.right) && queue.push(cur.right);
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 returns an array of arrays, where each inner array represents a level in
1198
- * a binary tree and contains the results of applying a callback function to the nodes at that level.
1199
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1200
- * the tree. It takes a node as an argument and returns a value. The return type of the callback
1201
- * function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
1202
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1203
- * starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
1204
- * entry. If no value is provided, the `root` property of the class is used as the default starting
1205
- * point.
1206
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1207
- * iteration to be performed on the binary tree. It can have two possible values:
1208
- * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1209
- * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1210
- * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1211
- * be excluded
1212
- * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
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._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
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 depth-first traversal on a binary tree using the Morris traversal
1268
- * algorithm.
1269
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1270
- * the tree. It takes a single argument, which is the current node, and can return any value. The
1271
- * return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
1272
- * the return
1273
- * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
1274
- * to specify the order in which the nodes of a binary tree are traversed. It can take one of the
1275
- * following values:
1276
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1277
- * point for the traversal. It can be either a node object, a key, or an entry object. If no value is
1278
- * provided, the `root` of the tree is used as the starting point.
1279
- * @returns The function `morris` returns an array of values that are the return values of the
1280
- * callback function `callback`.
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._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root) {
1316
+ morris(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root) {
1283
1317
  beginRoot = this.ensureNode(beginRoot);
1284
- if (beginRoot === null)
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 object.
1372
- * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
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.root, this.iterationType, true);
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 creates a new tree with entries that pass a given predicate function.
1389
- * @param predicate - The `predicate` parameter is a callback function that is used to test each
1390
- * element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
1391
- * represents the value of the current element being processed, the `key` argument represents the key
1392
- * of the
1393
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1394
- * specify the value of `this` within the `predicate` function. When the `predicate` function is
1395
- * called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
1396
- * @returns The `filter` method is returning a new tree object that contains the entries that pass
1397
- * the given predicate function.
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 creates a new tree by applying a callback function to each entry in the current
1414
- * tree.
1415
- * @param callback - The callback parameter is a function that will be called for each entry in the
1416
- * tree. It takes three arguments: value, key, and index. The value argument represents the value of
1417
- * the current entry, the key argument represents the key of the current entry, and the index
1418
- * argument represents the index of the
1419
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1420
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
1421
- * passed as the `this` value to the `callback` function. If `thisArg` is
1422
- * @returns The `map` method is returning a new tree object.
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 `print` function in TypeScript prints the binary tree structure with customizable options.
1446
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1447
- * point for printing the binary tree. It can be either a node of the binary tree or a key or entry
1448
- * that exists in the binary tree. If no value is provided, the root of the binary tree will be used
1449
- * as the starting point.
1450
- * @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
1451
- * allows you to customize the printing behavior. It has the following properties:
1452
- * @returns Nothing is being returned. The function has a return type of `void`, which means it does
1453
- * not return any value.
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
- print(beginRoot = this.root, options) {
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 function `_dfs` performs a depth-first search traversal on a binary tree structure based on
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 called on each node
1488
- * visited during the depth-first search. It is of type `C`, which extends
1489
- * `BTNCallback<OptBTNOrNull<NODE>>`. The default value is set to `this._DEFAULT_CALLBACK` if not
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 (DFS) algorithm should traverse the nodes in a binary tree.
1493
- * It can have one of the following values:
1494
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter in the `_dfs`
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 the root node of the tree or a key, node, or entry that exists
1497
- * in the tree. If no specific `
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. It
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`, the traversal will consider null nodes as valid nodes to visit and
1504
- * process. If set to `
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
- * input and returns a boolean value. It is used to determine whether the right child of a node
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 a given node should be
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
- * input and returns a boolean value indicating whether the node should be processed during the
1519
- * depth-first search traversal. The default implementation of this function simply returns `true`,
1520
- * meaning that by default all nodes will be processed. However, you can
1521
- * @returns The `_dfs` method returns an array of the return type of the callback function provided
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._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
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 => true) {
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` is a generator function that returns an iterator for the key-value
1620
- * pairs in a binary search tree.
1621
- * @param node - The `node` parameter represents the current node in the binary search tree. It is
1622
- * initially set to the root node of the tree.
1623
- * @returns an IterableIterator<[K, V | undefined]>.
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.root) {
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` function is responsible for generating the display layout of a binary tree node,
1658
- * taking into account various options such as whether to show null, undefined, or NaN nodes.
1659
- * @param {OptBTNOrNull<NODE>} node - The `node` parameter represents a node in a binary tree.
1660
- * It can be of type `NODE`, `null`, or `undefined`.
1661
- * @param {BinaryTreePrintOptions} options - The `options` parameter is an object that contains the
1662
- * following properties:
1663
- * @returns The function `_displayAux` returns a `NodeDisplayLayout` which is an array containing the
1664
- * following elements:
1665
- * 1. `mergedLines`: An array of strings representing the lines of the node display.
1666
- * 2. `totalWidth`: The total width of the node display.
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
- _DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
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 `_swapProperties` swaps the key-value properties between two nodes.
1728
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
1729
- * destination node. It can be either an instance of the class `R`, or an object of type
1730
- * `BTNKeyOrNodeOrEntry<K, V, NODE>`.
1731
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
1732
- * the properties will be swapped with the `srcNode`.
1733
- * @returns either the `destNode` object with its properties swapped with the `srcNode` object's
1734
- * properties, or `undefined` if either `srcNode` or `destNode` is falsy.
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 a node in a binary tree with a new node, updating the parent, left child,
1757
- * right child, and root if necessary.
1758
- * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
1759
- * tree.
1760
- * @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
1761
- * tree.
1762
- * @returns the newNode.
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.root === oldNode) {
1777
- this._root = newNode;
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 property of an object to the provided value, and also updates the
1786
- * parent property of the new root.
1787
- * @param {OptBTNOrNull<NODE>} v - The parameter `v` is of type `OptBTNOrNull<NODE>`. This
1788
- * means that it can accept a value of type `NODE`, `null`, or `undefined`.
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 `_ensureCallback` ensures that a callback function is provided and returns it.
1801
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
1802
- * `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
1803
- * the generic type `C`, or it can be `null` or `undefined`.
1804
- * @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
1805
- * and returns a value. It is of type `C`, which is a generic type that extends the
1806
- * `BTNCallback<NODE>` type.
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
- _ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
1810
- if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
1811
- callback = (node => node);
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
- return callback;
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
  }