min-heap-typed 1.50.1 → 1.50.3

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 (85) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +120 -9
  2. package/dist/data-structures/base/iterable-base.js +143 -7
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
  4. package/dist/data-structures/binary-tree/avl-tree.js +101 -72
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  6. package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
  8. package/dist/data-structures/binary-tree/binary-tree.js +484 -376
  9. package/dist/data-structures/binary-tree/bst.d.ts +92 -79
  10. package/dist/data-structures/binary-tree/bst.js +68 -76
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
  12. package/dist/data-structures/binary-tree/rb-tree.js +152 -99
  13. package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
  14. package/dist/data-structures/binary-tree/segment-tree.js +127 -10
  15. package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  16. package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
  17. package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
  18. package/dist/data-structures/graph/abstract-graph.js +3 -189
  19. package/dist/data-structures/graph/directed-graph.d.ts +73 -0
  20. package/dist/data-structures/graph/directed-graph.js +131 -0
  21. package/dist/data-structures/graph/map-graph.d.ts +8 -0
  22. package/dist/data-structures/graph/map-graph.js +14 -0
  23. package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
  24. package/dist/data-structures/graph/undirected-graph.js +151 -18
  25. package/dist/data-structures/hash/hash-map.d.ts +254 -28
  26. package/dist/data-structures/hash/hash-map.js +347 -78
  27. package/dist/data-structures/heap/heap.d.ts +95 -25
  28. package/dist/data-structures/heap/heap.js +95 -26
  29. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  30. package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
  31. package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  32. package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
  33. package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  34. package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
  35. package/dist/data-structures/matrix/matrix.d.ts +35 -4
  36. package/dist/data-structures/matrix/matrix.js +50 -11
  37. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  38. package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
  39. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  40. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
  41. package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
  42. package/dist/data-structures/priority-queue/priority-queue.js +8 -0
  43. package/dist/data-structures/queue/deque.d.ts +139 -35
  44. package/dist/data-structures/queue/deque.js +200 -62
  45. package/dist/data-structures/queue/queue.d.ts +103 -49
  46. package/dist/data-structures/queue/queue.js +111 -49
  47. package/dist/data-structures/stack/stack.d.ts +51 -21
  48. package/dist/data-structures/stack/stack.js +58 -22
  49. package/dist/data-structures/tree/tree.d.ts +57 -3
  50. package/dist/data-structures/tree/tree.js +77 -11
  51. package/dist/data-structures/trie/trie.d.ts +135 -34
  52. package/dist/data-structures/trie/trie.js +153 -33
  53. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  54. package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
  55. package/dist/types/utils/utils.d.ts +1 -0
  56. package/package.json +2 -2
  57. package/src/data-structures/base/iterable-base.ts +184 -19
  58. package/src/data-structures/binary-tree/avl-tree.ts +134 -100
  59. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  60. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  61. package/src/data-structures/binary-tree/bst.ts +127 -136
  62. package/src/data-structures/binary-tree/rb-tree.ts +199 -166
  63. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  64. package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
  65. package/src/data-structures/graph/abstract-graph.ts +4 -211
  66. package/src/data-structures/graph/directed-graph.ts +152 -0
  67. package/src/data-structures/graph/map-graph.ts +15 -0
  68. package/src/data-structures/graph/undirected-graph.ts +171 -19
  69. package/src/data-structures/hash/hash-map.ts +389 -96
  70. package/src/data-structures/heap/heap.ts +97 -26
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
  72. package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
  73. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  74. package/src/data-structures/matrix/matrix.ts +52 -12
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  78. package/src/data-structures/queue/deque.ts +225 -70
  79. package/src/data-structures/queue/queue.ts +118 -49
  80. package/src/data-structures/stack/stack.ts +63 -23
  81. package/src/data-structures/tree/tree.ts +89 -15
  82. package/src/data-structures/trie/trie.ts +173 -38
  83. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  84. package/src/types/data-structures/hash/hash-map.ts +4 -3
  85. package/src/types/utils/utils.ts +2 -0
@@ -24,19 +24,23 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
24
24
  */
25
25
  constructor(elements?: Iterable<E>);
26
26
  protected _elements: E[];
27
- get elements(): E[];
28
27
  /**
29
- * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
30
- * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
28
+ * The elements function returns the elements of this set.
29
+ * @return An array of elements
31
30
  */
31
+ get elements(): E[];
32
32
  /**
33
33
  * The size() function returns the number of elements in an array.
34
34
  * @returns The size of the elements array.
35
35
  */
36
36
  get size(): number;
37
37
  /**
38
- * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
39
- * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
38
+ * Time Complexity: O(n)
39
+ * Space Complexity: O(n)
40
+ */
41
+ /**
42
+ * Time Complexity: O(n)
43
+ * Space Complexity: O(n)
40
44
  *
41
45
  * The function "fromArray" creates a new Stack object from an array of elements.
42
46
  * @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
@@ -50,24 +54,24 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
50
54
  */
51
55
  isEmpty(): boolean;
52
56
  /**
53
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
54
- * Space Complexity: O(1), as it does not use any additional space.
57
+ * Time Complexity: O(1)
58
+ * Space Complexity: O(1)
55
59
  */
56
60
  /**
57
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
58
- * Space Complexity: O(1), as it does not use any additional space.
61
+ * Time Complexity: O(1)
62
+ * Space Complexity: O(1)
59
63
  *
60
64
  * The `peek` function returns the last element of an array, or undefined if the array is empty.
61
65
  * @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
62
66
  */
63
67
  peek(): E | undefined;
64
68
  /**
65
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
66
- * Space Complexity: O(1), as it does not use any additional space.
69
+ * Time Complexity: O(1)
70
+ * Space Complexity: O(1)
67
71
  */
68
72
  /**
69
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
70
- * Space Complexity: O(1), as it does not use any additional space.
73
+ * Time Complexity: O(1)
74
+ * Space Complexity: O(1)
71
75
  *
72
76
  * The push function adds an element to the stack and returns the updated stack.
73
77
  * @param {E} element - The parameter "element" is of type E, which means it can be any data type.
@@ -75,18 +79,30 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
75
79
  */
76
80
  push(element: E): boolean;
77
81
  /**
78
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
79
- * Space Complexity: O(1), as it does not use any additional space.
82
+ * Time Complexity: O(1)
83
+ * Space Complexity: O(1)
80
84
  */
81
85
  /**
82
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
83
- * Space Complexity: O(1), as it does not use any additional space.
86
+ * Time Complexity: O(1)
87
+ * Space Complexity: O(1)
84
88
  *
85
89
  * The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
86
90
  * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
87
91
  * array is empty, it returns `undefined`.
88
92
  */
89
93
  pop(): E | undefined;
94
+ /**
95
+ * The delete function removes an element from the stack.
96
+ * @param element: E Specify the element to be deleted
97
+ * @return A boolean value indicating whether the element was successfully deleted or not
98
+ */
99
+ delete(element: E): boolean;
100
+ /**
101
+ * The deleteAt function deletes the element at a given index.
102
+ * @param index: number Determine the index of the element to be deleted
103
+ * @return A boolean value
104
+ */
105
+ deleteAt(index: number): boolean;
90
106
  /**
91
107
  * Time Complexity: O(n)
92
108
  * Space Complexity: O(n)
@@ -100,16 +116,23 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
100
116
  */
101
117
  toArray(): E[];
102
118
  /**
119
+ * Time Complexity: O(1)
120
+ * Space Complexity: O(1)
121
+ */
122
+ /**
123
+ * Time Complexity: O(1)
124
+ * Space Complexity: O(1)
125
+ *
103
126
  * The clear function clears the elements array.
104
127
  */
105
128
  clear(): void;
106
129
  /**
107
- * Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it.
108
- * Space Complexity: O(n), as it creates a new stack.
130
+ * Time Complexity: O(n)
131
+ * Space Complexity: O(n)
109
132
  */
110
133
  /**
111
- * Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it.
112
- * Space Complexity: O(n), as it creates a new stack.
134
+ * Time Complexity: O(n)
135
+ * Space Complexity: O(n)
113
136
  *
114
137
  * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
115
138
  * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
@@ -155,6 +178,13 @@ export declare class Stack<E = any> extends IterableElementBase<E> {
155
178
  */
156
179
  map<T>(callback: ElementCallback<E, T>, thisArg?: any): Stack<T>;
157
180
  /**
181
+ * Time Complexity: O(n)
182
+ * Space Complexity: O(n)
183
+ */
184
+ /**
185
+ * Time Complexity: O(n)
186
+ * Space Complexity: O(n)
187
+ *
158
188
  * Custom iterator for the Stack class.
159
189
  * @returns An iterator object.
160
190
  */
@@ -25,13 +25,13 @@ class Stack extends base_1.IterableElementBase {
25
25
  this.push(el);
26
26
  }
27
27
  }
28
+ /**
29
+ * The elements function returns the elements of this set.
30
+ * @return An array of elements
31
+ */
28
32
  get elements() {
29
33
  return this._elements;
30
34
  }
31
- /**
32
- * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
33
- * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
34
- */
35
35
  /**
36
36
  * The size() function returns the number of elements in an array.
37
37
  * @returns The size of the elements array.
@@ -40,8 +40,12 @@ class Stack extends base_1.IterableElementBase {
40
40
  return this.elements.length;
41
41
  }
42
42
  /**
43
- * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
44
- * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
43
+ * Time Complexity: O(n)
44
+ * Space Complexity: O(n)
45
+ */
46
+ /**
47
+ * Time Complexity: O(n)
48
+ * Space Complexity: O(n)
45
49
  *
46
50
  * The function "fromArray" creates a new Stack object from an array of elements.
47
51
  * @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
@@ -59,12 +63,12 @@ class Stack extends base_1.IterableElementBase {
59
63
  return this.elements.length === 0;
60
64
  }
61
65
  /**
62
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
63
- * Space Complexity: O(1), as it does not use any additional space.
66
+ * Time Complexity: O(1)
67
+ * Space Complexity: O(1)
64
68
  */
65
69
  /**
66
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
67
- * Space Complexity: O(1), as it does not use any additional space.
70
+ * Time Complexity: O(1)
71
+ * Space Complexity: O(1)
68
72
  *
69
73
  * The `peek` function returns the last element of an array, or undefined if the array is empty.
70
74
  * @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
@@ -75,12 +79,12 @@ class Stack extends base_1.IterableElementBase {
75
79
  return this.elements[this.elements.length - 1];
76
80
  }
77
81
  /**
78
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
79
- * Space Complexity: O(1), as it does not use any additional space.
82
+ * Time Complexity: O(1)
83
+ * Space Complexity: O(1)
80
84
  */
81
85
  /**
82
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
83
- * Space Complexity: O(1), as it does not use any additional space.
86
+ * Time Complexity: O(1)
87
+ * Space Complexity: O(1)
84
88
  *
85
89
  * The push function adds an element to the stack and returns the updated stack.
86
90
  * @param {E} element - The parameter "element" is of type E, which means it can be any data type.
@@ -91,12 +95,12 @@ class Stack extends base_1.IterableElementBase {
91
95
  return true;
92
96
  }
93
97
  /**
94
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
95
- * Space Complexity: O(1), as it does not use any additional space.
98
+ * Time Complexity: O(1)
99
+ * Space Complexity: O(1)
96
100
  */
97
101
  /**
98
- * Time Complexity: O(1), as it only involves accessing the last element of the array.
99
- * Space Complexity: O(1), as it does not use any additional space.
102
+ * Time Complexity: O(1)
103
+ * Space Complexity: O(1)
100
104
  *
101
105
  * The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
102
106
  * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
@@ -107,6 +111,24 @@ class Stack extends base_1.IterableElementBase {
107
111
  return;
108
112
  return this.elements.pop();
109
113
  }
114
+ /**
115
+ * The delete function removes an element from the stack.
116
+ * @param element: E Specify the element to be deleted
117
+ * @return A boolean value indicating whether the element was successfully deleted or not
118
+ */
119
+ delete(element) {
120
+ const index = this.elements.indexOf(element);
121
+ return this.deleteAt(index);
122
+ }
123
+ /**
124
+ * The deleteAt function deletes the element at a given index.
125
+ * @param index: number Determine the index of the element to be deleted
126
+ * @return A boolean value
127
+ */
128
+ deleteAt(index) {
129
+ const spliced = this.elements.splice(index, 1);
130
+ return spliced.length === 1;
131
+ }
110
132
  /**
111
133
  * Time Complexity: O(n)
112
134
  * Space Complexity: O(n)
@@ -122,18 +144,25 @@ class Stack extends base_1.IterableElementBase {
122
144
  return this.elements.slice();
123
145
  }
124
146
  /**
147
+ * Time Complexity: O(1)
148
+ * Space Complexity: O(1)
149
+ */
150
+ /**
151
+ * Time Complexity: O(1)
152
+ * Space Complexity: O(1)
153
+ *
125
154
  * The clear function clears the elements array.
126
155
  */
127
156
  clear() {
128
157
  this._elements = [];
129
158
  }
130
159
  /**
131
- * Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it.
132
- * Space Complexity: O(n), as it creates a new stack.
160
+ * Time Complexity: O(n)
161
+ * Space Complexity: O(n)
133
162
  */
134
163
  /**
135
- * Time Complexity: O(n), where n is the number of elements in the stack, as it creates a new stack and copies all elements into it.
136
- * Space Complexity: O(n), as it creates a new stack.
164
+ * Time Complexity: O(n)
165
+ * Space Complexity: O(n)
137
166
  *
138
167
  * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
139
168
  * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
@@ -199,6 +228,13 @@ class Stack extends base_1.IterableElementBase {
199
228
  return newStack;
200
229
  }
201
230
  /**
231
+ * Time Complexity: O(n)
232
+ * Space Complexity: O(n)
233
+ */
234
+ /**
235
+ * Time Complexity: O(n)
236
+ * Space Complexity: O(n)
237
+ *
202
238
  * Custom iterator for the Stack class.
203
239
  * @returns An iterator object.
204
240
  */
@@ -1,8 +1,62 @@
1
1
  export declare class TreeNode<V = any> {
2
- key: string;
3
- value?: V | undefined;
4
- children?: TreeNode<V>[] | undefined;
2
+ /**
3
+ * The constructor function initializes a TreeNode object with a key, optional value, and optional
4
+ * children.
5
+ * @param {string} key - A string representing the key of the tree node.
6
+ * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
7
+ * value associated with the node. If no value is provided, it defaults to `undefined`.
8
+ * @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>`
9
+ * objects. It represents the child nodes of the current node. If no children are provided, the
10
+ * default value is an empty array.
11
+ */
5
12
  constructor(key: string, value?: V, children?: TreeNode<V>[]);
13
+ protected _key: string;
14
+ /**
15
+ * The function returns the value of the protected variable _key.
16
+ * @returns The value of the `_key` property, which is a string.
17
+ */
18
+ get key(): string;
19
+ /**
20
+ * The above function sets the value of a protected variable called "key".
21
+ * @param {string} value - The value parameter is a string that represents the value to be assigned
22
+ * to the key.
23
+ */
24
+ set key(value: string);
25
+ protected _value?: V | undefined;
26
+ /**
27
+ * The function returns the value stored in a variable, or undefined if the variable is empty.
28
+ * @returns The value of the variable `_value` is being returned.
29
+ */
30
+ get value(): V | undefined;
31
+ /**
32
+ * The function sets the value of a variable.
33
+ * @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it
34
+ * can accept a value of type "V" or it can be undefined.
35
+ */
36
+ set value(value: V | undefined);
37
+ protected _children?: TreeNode<V>[] | undefined;
38
+ /**
39
+ * The function returns an array of TreeNode objects or undefined.
40
+ * @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`,
41
+ * which means it can either be an array of `TreeNode<V>` objects or `undefined`.
42
+ */
43
+ get children(): TreeNode<V>[] | undefined;
44
+ /**
45
+ * The function sets the value of the children property of a TreeNode object.
46
+ * @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] |
47
+ * undefined. This means that it can accept an array of TreeNode objects or undefined.
48
+ */
49
+ set children(value: TreeNode<V>[] | undefined);
50
+ /**
51
+ * The function `addChildren` adds one or more child nodes to the current node.
52
+ * @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single
53
+ * `TreeNode<V>` object or an array of `TreeNode<V>` objects.
54
+ */
6
55
  addChildren(children: TreeNode<V> | TreeNode<V>[]): void;
56
+ /**
57
+ * The function `getHeight()` calculates the maximum depth of a tree structure by performing a
58
+ * breadth-first search.
59
+ * @returns the maximum depth or height of the tree.
60
+ */
7
61
  getHeight(): number;
8
62
  }
@@ -2,22 +2,88 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TreeNode = void 0;
4
4
  class TreeNode {
5
+ /**
6
+ * The constructor function initializes a TreeNode object with a key, optional value, and optional
7
+ * children.
8
+ * @param {string} key - A string representing the key of the tree node.
9
+ * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
10
+ * value associated with the node. If no value is provided, it defaults to `undefined`.
11
+ * @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>`
12
+ * objects. It represents the child nodes of the current node. If no children are provided, the
13
+ * default value is an empty array.
14
+ */
5
15
  constructor(key, value, children) {
6
- this.key = key;
7
- this.value = value || undefined;
8
- this.children = children || [];
16
+ this._key = key;
17
+ this._value = value || undefined;
18
+ this._children = children || [];
9
19
  }
20
+ /**
21
+ * The function returns the value of the protected variable _key.
22
+ * @returns The value of the `_key` property, which is a string.
23
+ */
24
+ get key() {
25
+ return this._key;
26
+ }
27
+ /**
28
+ * The above function sets the value of a protected variable called "key".
29
+ * @param {string} value - The value parameter is a string that represents the value to be assigned
30
+ * to the key.
31
+ */
32
+ set key(value) {
33
+ this._key = value;
34
+ }
35
+ /**
36
+ * The function returns the value stored in a variable, or undefined if the variable is empty.
37
+ * @returns The value of the variable `_value` is being returned.
38
+ */
39
+ get value() {
40
+ return this._value;
41
+ }
42
+ /**
43
+ * The function sets the value of a variable.
44
+ * @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it
45
+ * can accept a value of type "V" or it can be undefined.
46
+ */
47
+ set value(value) {
48
+ this._value = value;
49
+ }
50
+ /**
51
+ * The function returns an array of TreeNode objects or undefined.
52
+ * @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`,
53
+ * which means it can either be an array of `TreeNode<V>` objects or `undefined`.
54
+ */
55
+ get children() {
56
+ return this._children;
57
+ }
58
+ /**
59
+ * The function sets the value of the children property of a TreeNode object.
60
+ * @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] |
61
+ * undefined. This means that it can accept an array of TreeNode objects or undefined.
62
+ */
63
+ set children(value) {
64
+ this._children = value;
65
+ }
66
+ /**
67
+ * The function `addChildren` adds one or more child nodes to the current node.
68
+ * @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single
69
+ * `TreeNode<V>` object or an array of `TreeNode<V>` objects.
70
+ */
10
71
  addChildren(children) {
11
- if (!this.children) {
12
- this.children = [];
72
+ if (!this._children) {
73
+ this._children = [];
13
74
  }
14
75
  if (children instanceof TreeNode) {
15
- this.children.push(children);
76
+ this._children.push(children);
16
77
  }
17
78
  else {
18
- this.children = this.children.concat(children);
79
+ this._children = this._children.concat(children);
19
80
  }
20
81
  }
82
+ /**
83
+ * The function `getHeight()` calculates the maximum depth of a tree structure by performing a
84
+ * breadth-first search.
85
+ * @returns the maximum depth or height of the tree.
86
+ */
21
87
  getHeight() {
22
88
  let maxDepth = 0;
23
89
  if (this) {
@@ -25,10 +91,10 @@ class TreeNode {
25
91
  if (level > maxDepth) {
26
92
  maxDepth = level;
27
93
  }
28
- const { children } = node;
29
- if (children) {
30
- for (let i = 0, len = children.length; i < len; i++) {
31
- bfs(children[i], level + 1);
94
+ const { _children } = node;
95
+ if (_children) {
96
+ for (let i = 0, len = _children.length; i < len; i++) {
97
+ bfs(_children[i], level + 1);
32
98
  }
33
99
  }
34
100
  };