data-structure-typed 1.39.3 → 1.39.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +4 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +5 -13
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +17 -25
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +6 -6
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +88 -88
- package/dist/cjs/data-structures/graph/abstract-graph.js +41 -41
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +63 -63
- package/dist/cjs/data-structures/graph/directed-graph.js +36 -36
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +10 -10
- package/dist/cjs/data-structures/graph/map-graph.js +7 -7
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +38 -38
- package/dist/cjs/data-structures/graph/undirected-graph.js +21 -21
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/data-structures/queue/queue.js +3 -3
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/graph.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +4 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -13
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +17 -25
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.js +6 -6
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +88 -88
- package/dist/mjs/data-structures/graph/abstract-graph.js +41 -41
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +63 -63
- package/dist/mjs/data-structures/graph/directed-graph.js +36 -36
- package/dist/mjs/data-structures/graph/map-graph.d.ts +10 -10
- package/dist/mjs/data-structures/graph/map-graph.js +7 -7
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +38 -38
- package/dist/mjs/data-structures/graph/undirected-graph.js +21 -21
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +3 -3
- package/dist/mjs/interfaces/graph.d.ts +3 -3
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +19 -28
- package/src/data-structures/binary-tree/bst.ts +6 -6
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
- package/src/data-structures/graph/abstract-graph.ts +135 -133
- package/src/data-structures/graph/directed-graph.ts +92 -87
- package/src/data-structures/graph/map-graph.ts +17 -20
- package/src/data-structures/graph/undirected-graph.ts +56 -54
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/graph.ts +3 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +20 -15
- package/test/unit/data-structures/graph/map-graph.test.ts +23 -23
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/queue/queue.test.ts +8 -8
|
@@ -31,7 +31,7 @@ class UndirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
|
31
31
|
* @param {VertexKey} v2 - The parameter `v2` is a `VertexKey`, which represents the identifier of the second vertex in a
|
|
32
32
|
* graph edge.
|
|
33
33
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
34
|
-
* @param {
|
|
34
|
+
* @param {E} [val] - The "val" parameter is an optional parameter of type E. It is used to store a value associated
|
|
35
35
|
* with the edge.
|
|
36
36
|
*/
|
|
37
37
|
constructor(v1, v2, weight, val) {
|
|
@@ -64,7 +64,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
64
64
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
65
65
|
* it will be used as the value of the vertex. If no value is provided, the `key` parameter will be used as the value of
|
|
66
66
|
* the vertex.
|
|
67
|
-
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `
|
|
67
|
+
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `VO`.
|
|
68
68
|
*/
|
|
69
69
|
createVertex(key, val) {
|
|
70
70
|
return new UndirectedVertex(key, val !== null && val !== void 0 ? val : key);
|
|
@@ -77,18 +77,18 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
77
77
|
* no weight is provided, it defaults to 1.
|
|
78
78
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
79
79
|
* is used to store additional information or data associated with the edge.
|
|
80
|
-
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `
|
|
80
|
+
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `EO`.
|
|
81
81
|
*/
|
|
82
82
|
createEdge(v1, v2, weight, val) {
|
|
83
83
|
return new UndirectedEdge(v1, v2, weight !== null && weight !== void 0 ? weight : 1, val);
|
|
84
84
|
}
|
|
85
85
|
/**
|
|
86
86
|
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
|
|
87
|
-
* @param {
|
|
87
|
+
* @param {VO | null | VertexKey} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
88
88
|
* object), `null`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
89
|
-
* @param {
|
|
89
|
+
* @param {VO | null | VertexKey} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
90
90
|
* object), `null`, or `VertexKey` (vertex ID).
|
|
91
|
-
* @returns an edge (
|
|
91
|
+
* @returns an edge (EO) or null.
|
|
92
92
|
*/
|
|
93
93
|
getEdge(v1, v2) {
|
|
94
94
|
var _a;
|
|
@@ -104,10 +104,10 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
106
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
107
|
-
* @param {
|
|
108
|
-
* @param {
|
|
107
|
+
* @param {VO | VertexKey} v1 - The parameter `v1` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
108
|
+
* @param {VO | VertexKey} v2 - VO | VertexKey - This parameter can be either a vertex object (VO) or a vertex ID
|
|
109
109
|
* (VertexKey). It represents the second vertex of the edge that needs to be removed.
|
|
110
|
-
* @returns the removed edge (
|
|
110
|
+
* @returns the removed edge (EO) if it exists, or null if either of the vertices (VO) does not exist.
|
|
111
111
|
*/
|
|
112
112
|
deleteEdgeBetween(v1, v2) {
|
|
113
113
|
const vertex1 = this._getVertex(v1);
|
|
@@ -128,8 +128,8 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
128
128
|
}
|
|
129
129
|
/**
|
|
130
130
|
* The deleteEdge function removes an edge between two vertices in a graph.
|
|
131
|
-
* @param {
|
|
132
|
-
* @returns The method is returning either the removed edge (of type
|
|
131
|
+
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
132
|
+
* @returns The method is returning either the removed edge (of type EO) or null if the edge was not found.
|
|
133
133
|
*/
|
|
134
134
|
deleteEdge(edge) {
|
|
135
135
|
return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
@@ -137,7 +137,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
137
137
|
/**
|
|
138
138
|
* The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
|
|
139
139
|
* vertex.
|
|
140
|
-
* @param {VertexKey |
|
|
140
|
+
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`.
|
|
141
141
|
* @returns The function `degreeOf` returns the degree of a vertex in a graph. The degree of a vertex is the number of
|
|
142
142
|
* edges connected to that vertex.
|
|
143
143
|
*/
|
|
@@ -153,8 +153,8 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
153
153
|
}
|
|
154
154
|
/**
|
|
155
155
|
* The function returns the edges of a given vertex or vertex ID.
|
|
156
|
-
* @param {VertexKey |
|
|
157
|
-
* unique identifier for a vertex in a graph, while `
|
|
156
|
+
* @param {VertexKey | VO} vertexOrKey - The parameter `vertexOrKey` can be either a `VertexKey` or a `VO`. A `VertexKey` is a
|
|
157
|
+
* unique identifier for a vertex in a graph, while `VO` represents the type of the vertex.
|
|
158
158
|
* @returns an array of edges.
|
|
159
159
|
*/
|
|
160
160
|
edgesOf(vertexOrKey) {
|
|
@@ -168,7 +168,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
168
168
|
}
|
|
169
169
|
/**
|
|
170
170
|
* The function "edgeSet" returns an array of unique edges from a set of edges.
|
|
171
|
-
* @returns The method `edgeSet()` returns an array of type `
|
|
171
|
+
* @returns The method `edgeSet()` returns an array of type `EO[]`.
|
|
172
172
|
*/
|
|
173
173
|
edgeSet() {
|
|
174
174
|
const edgeSet = new Set();
|
|
@@ -181,9 +181,9 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
181
181
|
}
|
|
182
182
|
/**
|
|
183
183
|
* The function "getNeighbors" returns an array of neighboring vertices for a given vertex or vertex ID.
|
|
184
|
-
* @param {
|
|
184
|
+
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
185
185
|
* (`VertexKey`).
|
|
186
|
-
* @returns an array of vertices (
|
|
186
|
+
* @returns an array of vertices (VO[]).
|
|
187
187
|
*/
|
|
188
188
|
getNeighbors(vertexOrKey) {
|
|
189
189
|
const neighbors = [];
|
|
@@ -202,8 +202,8 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
202
202
|
/**
|
|
203
203
|
* The function "getEndsOfEdge" returns the vertices at the ends of an edge if the edge exists in the graph, otherwise
|
|
204
204
|
* it returns null.
|
|
205
|
-
* @param {
|
|
206
|
-
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[
|
|
205
|
+
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
206
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[VO, VO]` if the edge exists in the
|
|
207
207
|
* graph. If the edge does not exist, it returns `null`.
|
|
208
208
|
*/
|
|
209
209
|
getEndsOfEdge(edge) {
|
|
@@ -221,7 +221,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
221
221
|
}
|
|
222
222
|
/**
|
|
223
223
|
* The function adds an edge to the graph by updating the adjacency list with the vertices of the edge.
|
|
224
|
-
* @param {
|
|
224
|
+
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
225
225
|
* @returns a boolean value.
|
|
226
226
|
*/
|
|
227
227
|
_addEdgeOnly(edge) {
|
|
@@ -243,7 +243,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
243
243
|
}
|
|
244
244
|
/**
|
|
245
245
|
* The function sets the edges of a graph.
|
|
246
|
-
* @param v - A map where the keys are of type
|
|
246
|
+
* @param v - A map where the keys are of type VO and the values are arrays of type EO.
|
|
247
247
|
*/
|
|
248
248
|
_setEdges(v) {
|
|
249
249
|
this._edges = v;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"undirected-graph.js","sourceRoot":"","sources":["../../../../src/data-structures/graph/undirected-graph.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,uCAAwC;AACxC,qDAA6E;AAI7E,MAAa,gBAA0B,SAAQ,+BAAiB;IAC9D;;;;;;OAMG;IACH,YAAY,GAAc,EAAE,GAAO;QACjC,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAClB,CAAC;CACF;AAXD,4CAWC;AAED,MAAa,cAA2B,SAAQ,6BAAe;IAC7D;;;;;;;;;OASG;IACH,YAAY,EAAa,EAAE,EAAa,EAAE,MAAe,EAAE,GAAO;QAChE,KAAK,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QACnB,IAAI,CAAC,SAAS,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAC5B,CAAC;IAID,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,IAAI,QAAQ,CAAC,CAAyB;QACpC,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;IACrB,CAAC;CACF;AAzBD,wCAyBC;AAED,MAAa,
|
|
1
|
+
{"version":3,"file":"undirected-graph.js","sourceRoot":"","sources":["../../../../src/data-structures/graph/undirected-graph.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,uCAAwC;AACxC,qDAA6E;AAI7E,MAAa,gBAA0B,SAAQ,+BAAiB;IAC9D;;;;;;OAMG;IACH,YAAY,GAAc,EAAE,GAAO;QACjC,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAClB,CAAC;CACF;AAXD,4CAWC;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;gBACb,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;gBACzC,IAAI,KAAK,EAAE;oBACT,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;iBAClB;qBAAM;oBACL,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;iBACpC;aACF;SACF;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACO,SAAS,CAAC,CAAgB;QAClC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;CACF;AA/ND,0CA+NC"}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
import { SinglyLinkedList } from '../linked-list';
|
|
7
|
-
export declare class
|
|
7
|
+
export declare class SkipQueue<E = any> extends SinglyLinkedList<E> {
|
|
8
8
|
/**
|
|
9
9
|
* The enqueue function adds a value to the end of an array.
|
|
10
10
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Queue = exports.
|
|
3
|
+
exports.Queue = exports.SkipQueue = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* @license MIT
|
|
6
6
|
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
7
7
|
* @class
|
|
8
8
|
*/
|
|
9
9
|
const linked_list_1 = require("../linked-list");
|
|
10
|
-
class
|
|
10
|
+
class SkipQueue extends linked_list_1.SinglyLinkedList {
|
|
11
11
|
/**
|
|
12
12
|
* The enqueue function adds a value to the end of an array.
|
|
13
13
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -31,7 +31,7 @@ class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
|
31
31
|
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.val;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
exports.
|
|
34
|
+
exports.SkipQueue = SkipQueue;
|
|
35
35
|
class Queue {
|
|
36
36
|
/**
|
|
37
37
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../../src/data-structures/queue/queue.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,gDAAgD;AAEhD,MAAa,
|
|
1
|
+
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../../src/data-structures/queue/queue.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,gDAAgD;AAEhD,MAAa,SAAmB,SAAQ,8BAAmB;IACzD;;;OAGG;IACH,OAAO,CAAC,KAAQ;QACd,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED;;;OAGG;IACH,IAAI;;QACF,OAAO,MAAA,IAAI,CAAC,IAAI,0CAAE,GAAG,CAAC;IACxB,CAAC;CACF;AAxBD,8BAwBC;AAED,MAAa,KAAK;IAChB;;;;;OAKG;IACH,YAAY,QAAc;QACxB,IAAI,CAAC,MAAM,GAAG,QAAQ,IAAI,EAAE,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;IACnB,CAAC;IAID,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,IAAI,KAAK,CAAC,KAAU;QAClB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAID,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,IAAI,MAAM,CAAC,KAAa;QACtB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;IACvB,CAAC;IAED;;;OAGG;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;IACH,OAAO,CAAC,KAAQ;QACd,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,KAAa;QACjB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,IAAI,KAAK,CAAC,CAAC;IACzB,CAAC;IAED;;;OAGG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;IAED;;;OAGG;IACH,KAAK;QACH,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAClD,CAAC;IAED,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC;QAChB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,EAAE;YAC7B,MAAM,IAAI,CAAC;SACZ;IACH,CAAC;CACF;AA7JD,sBA6JC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { VertexKey } from '../types';
|
|
2
|
-
export interface IGraph<V, E> {
|
|
3
|
-
createVertex(key: VertexKey, val?: V):
|
|
4
|
-
createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E):
|
|
2
|
+
export interface IGraph<V, E, VO, EO> {
|
|
3
|
+
createVertex(key: VertexKey, val?: V): VO;
|
|
4
|
+
createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): EO;
|
|
5
5
|
}
|
|
@@ -50,7 +50,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
50
50
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
51
51
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
52
52
|
* included in the result. The `callback` parameter has a default value of
|
|
53
|
-
* `
|
|
53
|
+
* `((node: N) => node.key)`
|
|
54
54
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
55
55
|
*/
|
|
56
56
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
@@ -64,10 +64,12 @@ class AVLTree extends bst_1.BST {
|
|
|
64
64
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
65
65
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
66
66
|
* included in the result. The `callback` parameter has a default value of
|
|
67
|
-
* `
|
|
67
|
+
* `((node: N) => node.key)`
|
|
68
68
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
69
69
|
*/
|
|
70
|
-
delete(identifier, callback =
|
|
70
|
+
delete(identifier, callback = ((node) => node.key)) {
|
|
71
|
+
if (identifier instanceof AVLTreeNode)
|
|
72
|
+
callback = (node => node);
|
|
71
73
|
const deletedResults = super.delete(identifier, callback);
|
|
72
74
|
for (const { needBalanced } of deletedResults) {
|
|
73
75
|
if (needBalanced) {
|
|
@@ -180,8 +180,8 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
180
180
|
* @returns The method is returning a boolean value.
|
|
181
181
|
*/
|
|
182
182
|
isPerfectlyBalanced(beginRoot?: N | null): boolean;
|
|
183
|
-
getNodes<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback
|
|
184
|
-
getNodes<C extends BTNCallback<N, N>>(identifier: N | null, callback
|
|
183
|
+
getNodes<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
184
|
+
getNodes<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
185
185
|
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
186
186
|
has<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
187
187
|
has<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N, iterationType?: IterationType): boolean;
|
|
@@ -263,7 +263,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
263
263
|
* function on each node according to a specified order pattern.
|
|
264
264
|
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
265
265
|
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
266
|
-
* is `
|
|
266
|
+
* is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
|
|
267
267
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
268
268
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
269
269
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
@@ -279,7 +279,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
279
279
|
* function on each node.
|
|
280
280
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
281
281
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
282
|
-
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `
|
|
282
|
+
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
|
|
283
283
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
284
284
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
285
285
|
* will not be performed and an empty array will be returned.
|
|
@@ -315,7 +315,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
315
315
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
316
316
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
317
317
|
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
|
|
318
|
-
* default value for this parameter is `
|
|
318
|
+
* default value for this parameter is `((node: N) => node.key)`.
|
|
319
319
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
320
320
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
321
321
|
* following values:
|
|
@@ -342,14 +342,6 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
342
342
|
* @returns {N} - The destination node after the swap.
|
|
343
343
|
*/
|
|
344
344
|
protected _swap(srcNode: N, destNode: N): N;
|
|
345
|
-
/**
|
|
346
|
-
* Time complexity is O(n)
|
|
347
|
-
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
348
|
-
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
349
|
-
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
350
|
-
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
351
|
-
*/
|
|
352
|
-
protected _defaultCallbackByKey: BTNCallback<N, BTNKey>;
|
|
353
345
|
/**
|
|
354
346
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
355
347
|
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
@@ -198,7 +198,7 @@ class BinaryTree {
|
|
|
198
198
|
return;
|
|
199
199
|
}
|
|
200
200
|
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
|
|
201
|
-
const existNode = key !== undefined ? this.get(key,
|
|
201
|
+
const existNode = key !== undefined ? this.get(key, (node) => node.key) : undefined;
|
|
202
202
|
if (this.root) {
|
|
203
203
|
if (existNode) {
|
|
204
204
|
existNode.val = val;
|
|
@@ -268,9 +268,9 @@ class BinaryTree {
|
|
|
268
268
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
269
269
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
270
270
|
* included in the result. The `callback` parameter has a default value of
|
|
271
|
-
* `
|
|
271
|
+
* `((node: N) => node.key)`, which
|
|
272
272
|
*/
|
|
273
|
-
delete(identifier, callback =
|
|
273
|
+
delete(identifier, callback = ((node) => node.key)) {
|
|
274
274
|
const bstDeletedResult = [];
|
|
275
275
|
if (!this.root)
|
|
276
276
|
return bstDeletedResult;
|
|
@@ -460,7 +460,7 @@ class BinaryTree {
|
|
|
460
460
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
461
461
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
462
462
|
* included in the result. The `callback` parameter has a default value of
|
|
463
|
-
* `
|
|
463
|
+
* `((node: N) => node.key)`, which
|
|
464
464
|
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
465
465
|
* first node that matches the identifier. If set to true, the function will return an array with
|
|
466
466
|
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
@@ -472,7 +472,7 @@ class BinaryTree {
|
|
|
472
472
|
* traverse the binary tree. It can have two possible values:
|
|
473
473
|
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
474
474
|
*/
|
|
475
|
-
getNodes(identifier, callback =
|
|
475
|
+
getNodes(identifier, callback = ((node) => node.key), onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
476
476
|
if (!beginRoot)
|
|
477
477
|
return [];
|
|
478
478
|
if (identifier instanceof BinaryTreeNode)
|
|
@@ -517,7 +517,7 @@ class BinaryTree {
|
|
|
517
517
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
518
518
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
519
519
|
* whether the node matches the criteria or not. The default callback function
|
|
520
|
-
* `
|
|
520
|
+
* `((node: N) => node.key)` is used if no callback function is
|
|
521
521
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
522
522
|
* the node from which the search should begin. By default, it is set to `this.root`, which means the
|
|
523
523
|
* search will start from the root node of the binary tree. However, you can provide a different node
|
|
@@ -526,7 +526,7 @@ class BinaryTree {
|
|
|
526
526
|
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
527
527
|
* @returns a boolean value.
|
|
528
528
|
*/
|
|
529
|
-
has(identifier, callback =
|
|
529
|
+
has(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
530
530
|
if (identifier instanceof BinaryTreeNode)
|
|
531
531
|
callback = (node => node);
|
|
532
532
|
// TODO may support finding node by value equal
|
|
@@ -540,14 +540,14 @@ class BinaryTree {
|
|
|
540
540
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
541
541
|
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
542
542
|
* whether the node matches the criteria or not. The default callback function
|
|
543
|
-
* (`
|
|
543
|
+
* (`((node: N) => node.key)`) is used if no callback function is
|
|
544
544
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
545
545
|
* the root node from which the search should begin.
|
|
546
546
|
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
547
547
|
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
548
548
|
* @returns either the found node (of type N) or null if no node is found.
|
|
549
549
|
*/
|
|
550
|
-
get(identifier, callback =
|
|
550
|
+
get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
551
551
|
if (identifier instanceof BinaryTreeNode)
|
|
552
552
|
callback = (node => node);
|
|
553
553
|
// TODO may support finding node by value equal
|
|
@@ -709,7 +709,7 @@ class BinaryTree {
|
|
|
709
709
|
* performed on the binary tree. It can have two possible values:
|
|
710
710
|
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
711
711
|
*/
|
|
712
|
-
subTreeTraverse(callback =
|
|
712
|
+
subTreeTraverse(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
713
713
|
if (typeof beginRoot === 'number')
|
|
714
714
|
beginRoot = this.get(beginRoot);
|
|
715
715
|
const ans = [];
|
|
@@ -739,7 +739,7 @@ class BinaryTree {
|
|
|
739
739
|
* function on each node according to a specified order pattern.
|
|
740
740
|
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
741
741
|
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
742
|
-
* is `
|
|
742
|
+
* is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
|
|
743
743
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
744
744
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
745
745
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
@@ -749,7 +749,7 @@ class BinaryTree {
|
|
|
749
749
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
750
750
|
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
751
751
|
*/
|
|
752
|
-
dfs(callback =
|
|
752
|
+
dfs(callback = ((node) => node.key), pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
|
|
753
753
|
if (!beginRoot)
|
|
754
754
|
return [];
|
|
755
755
|
const ans = [];
|
|
@@ -824,7 +824,7 @@ class BinaryTree {
|
|
|
824
824
|
* function on each node.
|
|
825
825
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
826
826
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
827
|
-
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `
|
|
827
|
+
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
|
|
828
828
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
829
829
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
830
830
|
* will not be performed and an empty array will be returned.
|
|
@@ -832,7 +832,7 @@ class BinaryTree {
|
|
|
832
832
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
833
833
|
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
|
|
834
834
|
*/
|
|
835
|
-
bfs(callback =
|
|
835
|
+
bfs(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
836
836
|
if (!beginRoot)
|
|
837
837
|
return [];
|
|
838
838
|
const ans = [];
|
|
@@ -882,7 +882,7 @@ class BinaryTree {
|
|
|
882
882
|
* level in a binary tree. Each inner array contains the return type of the provided callback
|
|
883
883
|
* function `C` applied to the nodes at that level.
|
|
884
884
|
*/
|
|
885
|
-
listLevels(callback =
|
|
885
|
+
listLevels(callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
886
886
|
if (!beginRoot)
|
|
887
887
|
return [];
|
|
888
888
|
const levelsNodes = [];
|
|
@@ -939,7 +939,7 @@ class BinaryTree {
|
|
|
939
939
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
940
940
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
941
941
|
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
|
|
942
|
-
* default value for this parameter is `
|
|
942
|
+
* default value for this parameter is `((node: N) => node.key)`.
|
|
943
943
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
944
944
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
945
945
|
* following values:
|
|
@@ -948,7 +948,7 @@ class BinaryTree {
|
|
|
948
948
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
949
949
|
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
950
950
|
*/
|
|
951
|
-
morris(callback =
|
|
951
|
+
morris(callback = ((node) => node.key), pattern = 'in', beginRoot = this.root) {
|
|
952
952
|
if (beginRoot === null)
|
|
953
953
|
return [];
|
|
954
954
|
const ans = [];
|
|
@@ -1087,14 +1087,6 @@ class BinaryTree {
|
|
|
1087
1087
|
}
|
|
1088
1088
|
return destNode;
|
|
1089
1089
|
}
|
|
1090
|
-
/**
|
|
1091
|
-
* Time complexity is O(n)
|
|
1092
|
-
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
1093
|
-
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
1094
|
-
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
1095
|
-
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
1096
|
-
*/
|
|
1097
|
-
_defaultCallbackByKey = node => node.key;
|
|
1098
1090
|
/**
|
|
1099
1091
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1100
1092
|
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
@@ -97,7 +97,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
97
97
|
* generic type `N`.
|
|
98
98
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
99
99
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
100
|
-
* included in the result. The default value for `callback` is `
|
|
100
|
+
* included in the result. The default value for `callback` is `((node: N) => node.key)`, which is
|
|
101
101
|
* a
|
|
102
102
|
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
103
103
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
@@ -222,7 +222,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
222
222
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
223
223
|
* matching node is found.
|
|
224
224
|
*/
|
|
225
|
-
get(identifier, callback =
|
|
225
|
+
get(identifier, callback = ((node) => node.key), beginRoot = this.root, iterationType = this.iterationType) {
|
|
226
226
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
227
227
|
}
|
|
228
228
|
/**
|
|
@@ -256,7 +256,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
256
256
|
* generic type `N`.
|
|
257
257
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
258
258
|
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
259
|
-
* included in the result. The default value for `callback` is `
|
|
259
|
+
* included in the result. The default value for `callback` is `((node: N) => node.key)`, which is
|
|
260
260
|
* a
|
|
261
261
|
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
262
262
|
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
@@ -269,7 +269,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
269
269
|
* traverse the binary tree. It can have one of the following values:
|
|
270
270
|
* @returns an array of nodes (N[]).
|
|
271
271
|
*/
|
|
272
|
-
getNodes(identifier, callback =
|
|
272
|
+
getNodes(identifier, callback = ((node) => node.key), onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
273
273
|
if (!beginRoot)
|
|
274
274
|
return [];
|
|
275
275
|
const ans = [];
|
|
@@ -284,7 +284,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
284
284
|
if (!cur.left && !cur.right)
|
|
285
285
|
return;
|
|
286
286
|
// TODO potential bug
|
|
287
|
-
if (callback ===
|
|
287
|
+
if (callback === ((node) => node.key)) {
|
|
288
288
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
289
289
|
cur.left && _traverse(cur.left);
|
|
290
290
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
@@ -309,7 +309,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
309
309
|
return ans;
|
|
310
310
|
}
|
|
311
311
|
// TODO potential bug
|
|
312
|
-
if (callback ===
|
|
312
|
+
if (callback === ((node) => node.key)) {
|
|
313
313
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
314
314
|
cur.left && queue.push(cur.left);
|
|
315
315
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
@@ -342,7 +342,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
342
342
|
* done recursively or iteratively. It can have two possible values:
|
|
343
343
|
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
|
|
344
344
|
*/
|
|
345
|
-
lesserOrGreaterTraverse(callback =
|
|
345
|
+
lesserOrGreaterTraverse(callback = ((node) => node.key), lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
346
346
|
if (typeof targetNode === 'number')
|
|
347
347
|
targetNode = this.get(targetNode);
|
|
348
348
|
const ans = [];
|
|
@@ -98,7 +98,7 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
98
98
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
99
99
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
100
100
|
* included in the result. The `callback` parameter has a default value of
|
|
101
|
-
* `
|
|
101
|
+
* `((node: N) => node.key)`
|
|
102
102
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
103
103
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
104
104
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
@@ -252,14 +252,14 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
252
252
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
253
253
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
254
254
|
* included in the result. The `callback` parameter has a default value of
|
|
255
|
-
* `
|
|
255
|
+
* `((node: N) => node.key)`
|
|
256
256
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
257
257
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
258
258
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
259
259
|
* decremented by 1 and
|
|
260
260
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
261
261
|
*/
|
|
262
|
-
delete(identifier, callback =
|
|
262
|
+
delete(identifier, callback = ((node) => node.key), ignoreCount = false) {
|
|
263
263
|
const bstDeletedResult = [];
|
|
264
264
|
if (!this.root)
|
|
265
265
|
return bstDeletedResult;
|