data-structure-typed 1.39.0 → 1.39.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +96 -32
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +46 -8
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +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 +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/cjs/data-structures/matrix/matrix2d.js +3 -7
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.js +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/cjs/data-structures/queue/deque.js +22 -22
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +3 -3
- 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/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/cjs/types/helpers.d.ts +1 -4
- package/dist/cjs/types/helpers.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +96 -32
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +46 -8
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/map-graph.js +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/mjs/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/mjs/data-structures/matrix/matrix2d.js +3 -7
- package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/mjs/data-structures/matrix/vector2d.js +0 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/mjs/data-structures/queue/deque.js +22 -22
- package/dist/mjs/data-structures/queue/queue.d.ts +3 -3
- package/dist/mjs/data-structures/queue/queue.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/mjs/types/helpers.d.ts +1 -4
- 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 +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +76 -90
- package/src/data-structures/binary-tree/bst.ts +9 -16
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/graph/map-graph.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
- package/src/data-structures/matrix/matrix2d.ts +1 -3
- package/src/data-structures/matrix/vector2d.ts +0 -2
- package/src/data-structures/queue/deque.ts +22 -22
- package/src/data-structures/queue/queue.ts +3 -3
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
- package/src/types/helpers.ts +1 -7
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +141 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +289 -47
- package/test/unit/data-structures/binary-tree/bst.test.ts +391 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +73 -7
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +241 -186
- package/test/unit/data-structures/graph/directed-graph.test.ts +34 -4
- package/test/unit/data-structures/graph/map-graph.test.ts +82 -1
- package/test/unit/data-structures/graph/undirected-graph.test.ts +82 -0
- package/test/unit/data-structures/hash/coordinate-map.test.ts +20 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +25 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +3 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +69 -6
- package/test/unit/data-structures/linked-list/linked-list.test.ts +2 -2
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +59 -12
- package/test/unit/data-structures/matrix/matrix2d.test.ts +207 -0
- package/test/unit/data-structures/matrix/navigator.test.ts +166 -1
- package/test/unit/data-structures/matrix/vector2d.test.ts +171 -0
- package/test/unit/data-structures/queue/deque.test.ts +283 -20
- package/test/unit/data-structures/queue/queue.test.ts +10 -8
|
@@ -85,44 +85,44 @@ class ObjectDeque {
|
|
|
85
85
|
this._size++;
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
|
-
* The function `
|
|
88
|
+
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
89
89
|
* @returns The value of the first element in the data structure.
|
|
90
90
|
*/
|
|
91
|
-
|
|
91
|
+
popFirst() {
|
|
92
92
|
if (!this._size)
|
|
93
93
|
return;
|
|
94
|
-
const value = this.
|
|
94
|
+
const value = this.getFirst();
|
|
95
95
|
delete this._nodes[this._first];
|
|
96
96
|
this._first++;
|
|
97
97
|
this._size--;
|
|
98
98
|
return value;
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
101
|
-
* The `
|
|
101
|
+
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
102
102
|
* @returns The element at the first position of the `_nodes` array.
|
|
103
103
|
*/
|
|
104
|
-
|
|
104
|
+
getFirst() {
|
|
105
105
|
if (this._size)
|
|
106
106
|
return this._nodes[this._first];
|
|
107
107
|
}
|
|
108
108
|
/**
|
|
109
|
-
* The `
|
|
109
|
+
* The `popLast()` function removes and returns the last element in a data structure.
|
|
110
110
|
* @returns The value that was removed from the data structure.
|
|
111
111
|
*/
|
|
112
|
-
|
|
112
|
+
popLast() {
|
|
113
113
|
if (!this._size)
|
|
114
114
|
return;
|
|
115
|
-
const value = this.
|
|
115
|
+
const value = this.getLast();
|
|
116
116
|
delete this._nodes[this._last];
|
|
117
117
|
this._last--;
|
|
118
118
|
this._size--;
|
|
119
119
|
return value;
|
|
120
120
|
}
|
|
121
121
|
/**
|
|
122
|
-
* The `
|
|
122
|
+
* The `getLast()` function returns the last element in an array-like data structure.
|
|
123
123
|
* @returns The last element in the array "_nodes" is being returned.
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
getLast() {
|
|
126
126
|
if (this._size)
|
|
127
127
|
return this._nodes[this._last];
|
|
128
128
|
}
|
|
@@ -172,19 +172,19 @@ class ArrayDeque {
|
|
|
172
172
|
return this._nodes.push(value);
|
|
173
173
|
}
|
|
174
174
|
/**
|
|
175
|
-
* The function "
|
|
176
|
-
* @returns The method `
|
|
175
|
+
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
176
|
+
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
177
177
|
*/
|
|
178
|
-
|
|
178
|
+
popLast() {
|
|
179
179
|
var _a;
|
|
180
180
|
return (_a = this._nodes.pop()) !== null && _a !== void 0 ? _a : null;
|
|
181
181
|
}
|
|
182
182
|
/**
|
|
183
|
-
* The `
|
|
184
|
-
* @returns The `
|
|
183
|
+
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
184
|
+
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
185
185
|
* empty.
|
|
186
186
|
*/
|
|
187
|
-
|
|
187
|
+
popFirst() {
|
|
188
188
|
var _a;
|
|
189
189
|
return (_a = this._nodes.shift()) !== null && _a !== void 0 ? _a : null;
|
|
190
190
|
}
|
|
@@ -201,19 +201,19 @@ class ArrayDeque {
|
|
|
201
201
|
return this._nodes.unshift(value);
|
|
202
202
|
}
|
|
203
203
|
/**
|
|
204
|
-
* The `
|
|
205
|
-
* @returns The function `
|
|
204
|
+
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
205
|
+
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
206
206
|
* empty, it will return `null`.
|
|
207
207
|
*/
|
|
208
|
-
|
|
208
|
+
getFirst() {
|
|
209
209
|
var _a;
|
|
210
210
|
return (_a = this._nodes[0]) !== null && _a !== void 0 ? _a : null;
|
|
211
211
|
}
|
|
212
212
|
/**
|
|
213
|
-
* The `
|
|
214
|
-
* @returns The method `
|
|
213
|
+
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
214
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
215
215
|
*/
|
|
216
|
-
|
|
216
|
+
getLast() {
|
|
217
217
|
var _a;
|
|
218
218
|
return (_a = this._nodes[this._nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
219
219
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deque.js","sourceRoot":"","sources":["../../../../src/data-structures/queue/deque.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,gDAAgD;AAEhD,8CAA8C;AAC9C,8DAA8D;AAC9D,MAAa,KAAe,SAAQ,8BAAmB;CACtD;AADD,sBACC;AAED,8CAA8C;AAC9C,8DAA8D;AAC9D,0BAA0B;AAC1B,MAAa,WAAW;IACtB,YAAY,QAAiB;QAIrB,WAAM,GAAyB,EAAE,CAAC;QAMlC,cAAS,GAAG,MAAM,CAAC,gBAAgB,CAAC;QAUpC,WAAM,GAAG,CAAC,CAAC,CAAC;QAUZ,UAAK,GAAG,CAAC,CAAC,CAAC;QAUX,UAAK,GAAG,CAAC,CAAC;QAvChB,IAAI,QAAQ,KAAK,SAAS;YAAE,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;IACxD,CAAC;IAID,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAID,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,IAAI,QAAQ,CAAC,KAAa;QACxB,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;IACzB,CAAC;IAID,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,IAAI,KAAK,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
71
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
72
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
73
73
|
* array is empty, it returns `null`.
|
|
74
74
|
*/
|
|
75
|
-
|
|
75
|
+
getLast(): E | undefined;
|
|
76
76
|
/**
|
|
77
77
|
* The enqueue function adds a value to the end of a queue.
|
|
78
78
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -109,11 +109,11 @@ class Queue {
|
|
|
109
109
|
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
|
-
* The `
|
|
113
|
-
* @returns The method `
|
|
112
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
113
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
114
114
|
* array is empty, it returns `null`.
|
|
115
115
|
*/
|
|
116
|
-
|
|
116
|
+
getLast() {
|
|
117
117
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
118
118
|
}
|
|
119
119
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../../src/data-structures/queue/queue.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,gDAAgD;AAEhD,MAAa,eAAyB,SAAQ,8BAAmB;IAC/D;;;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,0CAwBC;AAED,MAAa,KAAK;IAChB;;;;;OAKG;IACH,YAAY,QAAc;QACxB,IAAI,CAAC,MAAM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f,CAAC;IAED;;;;OAIG;IACH,IAAI;QACF,OAAO,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC7D,CAAC;IAED;;;;OAIG;IACH,
|
|
1
|
+
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../../src/data-structures/queue/queue.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,gDAAgD;AAEhD,MAAa,eAAyB,SAAQ,8BAAmB;IAC/D;;;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,0CAwBC;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;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IACzC,CAAC;IAED;;;;;;;OAOG;IAC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AAI,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,CAAE,CAAC,MAAM,CAAC,QAAQ,CAAC;QACjB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,EAAE;YAC7B,MAAM,IAAI,CAAC;SACZ;IACH,CAAC;CACF;AA7JD,sBA6JC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested,
|
|
2
|
+
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, OneParamCallback } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
4
4
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
5
5
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
6
|
-
delete<C extends
|
|
6
|
+
delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
7
7
|
}
|
|
@@ -19,8 +19,6 @@ export declare enum FamilyPosition {
|
|
|
19
19
|
MAL_NODE = "MAL_NODE"
|
|
20
20
|
}
|
|
21
21
|
export type BinaryTreeNodeKey = number;
|
|
22
|
-
export type BFSCallback<N, D = any> = (node: N, level?: number) => D;
|
|
23
|
-
export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
|
|
24
22
|
export type BinaryTreeDeletedResult<N> = {
|
|
25
23
|
deleted: N | null | undefined;
|
|
26
24
|
needBalanced: N | null;
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { BinaryTreeNodeKey } from './data-structures';
|
|
2
1
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
3
2
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
4
|
-
export type
|
|
5
|
-
export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
|
|
6
|
-
export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
|
|
3
|
+
export type OneParamCallback<N, D = any> = (node: N) => D;
|
|
7
4
|
export declare enum CP {
|
|
8
5
|
lt = "lt",
|
|
9
6
|
eq = "eq",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/types/helpers.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/types/helpers.ts"],"names":[],"mappings":";;;AAMA,IAAY,EAIX;AAJD,WAAY,EAAE;IACZ,eAAS,CAAA;IACT,eAAS,CAAA;IACT,eAAS,CAAA;AACX,CAAC,EAJW,EAAE,kBAAF,EAAE,QAIb"}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
9
|
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../../types';
|
|
10
|
-
import {
|
|
10
|
+
import { OneParamCallback } from '../../types';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
13
13
|
height: number;
|
|
@@ -53,7 +53,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
53
53
|
* `this._defaultCallbackByKey`
|
|
54
54
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
55
55
|
*/
|
|
56
|
-
delete<C extends
|
|
56
|
+
delete<C extends OneParamCallback<N>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
57
57
|
/**
|
|
58
58
|
* The function swaps the key, value, and height properties between two nodes in a binary tree.
|
|
59
59
|
* @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
import { BinaryTreeDeletedResult,
|
|
8
|
+
import type { BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, OneParamCallback } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
12
12
|
* Represents a node in a binary tree.
|
|
@@ -132,8 +132,21 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
132
132
|
* @returns The method is returning a boolean value.
|
|
133
133
|
*/
|
|
134
134
|
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: Array<V>): boolean;
|
|
135
|
-
|
|
136
|
-
|
|
135
|
+
/**
|
|
136
|
+
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
137
|
+
* with the parent node that needs to be balanced.
|
|
138
|
+
* a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
|
|
139
|
+
* binary tree.
|
|
140
|
+
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
141
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
142
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
143
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
144
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
145
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
146
|
+
* included in the result. The `callback` parameter has a default value of
|
|
147
|
+
* `this._defaultCallbackByKey`, which
|
|
148
|
+
*/
|
|
149
|
+
delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
137
150
|
/**
|
|
138
151
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
139
152
|
* specified root node.
|
|
@@ -179,21 +192,62 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
179
192
|
* @returns The method is returning a boolean value.
|
|
180
193
|
*/
|
|
181
194
|
isPerfectlyBalanced(beginRoot?: N | null): boolean;
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
195
|
+
/**
|
|
196
|
+
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
197
|
+
* recursive or iterative traversal.
|
|
198
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
199
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
200
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
201
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
202
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
203
|
+
* included in the result. The `callback` parameter has a default value of
|
|
204
|
+
* `this._defaultCallbackByKey`, which
|
|
205
|
+
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
206
|
+
* first node that matches the identifier. If set to true, the function will return an array with
|
|
207
|
+
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
208
|
+
* function will continue searching for all
|
|
209
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
210
|
+
* traversal of the binary tree will begin. It is optional and defaults to the root of the binary
|
|
211
|
+
* tree.
|
|
212
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
213
|
+
* traverse the binary tree. It can have two possible values:
|
|
214
|
+
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
215
|
+
*/
|
|
216
|
+
getNodes<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
217
|
+
/**
|
|
218
|
+
* The function checks if a binary tree has a node with a given property or key.
|
|
219
|
+
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
220
|
+
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
|
|
221
|
+
* generic type `N`.
|
|
222
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
223
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
224
|
+
* whether the node matches the criteria or not. The default callback function
|
|
225
|
+
* `this._defaultCallbackByKey` is used if no callback function is
|
|
226
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
227
|
+
* the node from which the search should begin. By default, it is set to `this.root`, which means the
|
|
228
|
+
* search will start from the root node of the binary tree. However, you can provide a different node
|
|
229
|
+
* as
|
|
230
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
231
|
+
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
232
|
+
* @returns a boolean value.
|
|
233
|
+
*/
|
|
234
|
+
has<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): boolean;
|
|
235
|
+
/**
|
|
236
|
+
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
237
|
+
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
|
|
238
|
+
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
|
|
239
|
+
* type.
|
|
240
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
241
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
242
|
+
* whether the node matches the criteria or not. The default callback function
|
|
243
|
+
* (`this._defaultCallbackByKey`) is used if no callback function is
|
|
244
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
245
|
+
* the root node from which the search should begin.
|
|
246
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
247
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
248
|
+
* @returns either the found node (of type N) or null if no node is found.
|
|
249
|
+
*/
|
|
250
|
+
get<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
197
251
|
/**
|
|
198
252
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
199
253
|
* up to the root node, with the option to reverse the order of the nodes.
|
|
@@ -238,7 +292,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
238
292
|
* possible values:
|
|
239
293
|
* @returns The function `isSubtreeBST` returns a boolean value.
|
|
240
294
|
*/
|
|
241
|
-
isSubtreeBST(beginRoot: N, iterationType?: IterationType): boolean;
|
|
295
|
+
isSubtreeBST(beginRoot: N | null, iterationType?: IterationType): boolean;
|
|
242
296
|
/**
|
|
243
297
|
* The function checks if a binary tree is a binary search tree.
|
|
244
298
|
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
@@ -260,9 +314,9 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
260
314
|
* start from the root of the tree.
|
|
261
315
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
262
316
|
* performed on the binary tree. It can have two possible values:
|
|
263
|
-
* @returns The function `subTreeTraverse` returns an array of `
|
|
317
|
+
* @returns The function `subTreeTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
|
|
264
318
|
*/
|
|
265
|
-
subTreeTraverse<C extends
|
|
319
|
+
subTreeTraverse<C extends OneParamCallback<N>>(callback?: C, beginRoot?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[];
|
|
266
320
|
/**
|
|
267
321
|
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
268
322
|
* function on each node according to a specified order pattern.
|
|
@@ -276,23 +330,23 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
276
330
|
* is `null`, an empty array will be returned.
|
|
277
331
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
278
332
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
279
|
-
* @returns The function `dfs` returns an array of `
|
|
333
|
+
* @returns The function `dfs` returns an array of `ReturnType<OneParamCallback<N>>` values.
|
|
280
334
|
*/
|
|
281
|
-
dfs<C extends
|
|
335
|
+
dfs<C extends OneParamCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
|
|
282
336
|
/**
|
|
283
337
|
* The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
|
|
284
338
|
* function on each node.
|
|
285
339
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
286
340
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
287
|
-
* `
|
|
341
|
+
* `ReturnType<OneParamCallback<N>>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
288
342
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
289
343
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
290
344
|
* will not be performed and an empty array will be returned.
|
|
291
345
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
292
346
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
293
|
-
* @returns The function `bfs` returns an array of `
|
|
347
|
+
* @returns The function `bfs` returns an array of `ReturnType<OneParamCallback<N>>[]`.
|
|
294
348
|
*/
|
|
295
|
-
bfs<C extends
|
|
349
|
+
bfs<C extends OneParamCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
|
|
296
350
|
/**
|
|
297
351
|
* The `listLevels` function takes a binary tree node and a callback function, and returns an array
|
|
298
352
|
* of arrays representing the levels of the tree.
|
|
@@ -308,7 +362,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
308
362
|
* level in a binary tree. Each inner array contains the return type of the provided callback
|
|
309
363
|
* function `C` applied to the nodes at that level.
|
|
310
364
|
*/
|
|
311
|
-
listLevels<C extends
|
|
365
|
+
listLevels<C extends OneParamCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[][];
|
|
312
366
|
/**
|
|
313
367
|
* The function returns the predecessor node of a given node in a binary tree.
|
|
314
368
|
* @param {N} node - The parameter "node" represents a node in a binary tree.
|
|
@@ -319,7 +373,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
319
373
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
320
374
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
321
375
|
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
322
|
-
* tree. It takes a node of type `N` as input and returns a value of type `
|
|
376
|
+
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<OneParamCallback<N>>`. The
|
|
323
377
|
* default value for this parameter is `this._defaultCallbackByKey`.
|
|
324
378
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
325
379
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
@@ -327,9 +381,19 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
327
381
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
328
382
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
329
383
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
330
|
-
* @returns The `morris` function returns an array of `
|
|
384
|
+
* @returns The `morris` function returns an array of `ReturnType<OneParamCallback<N>>` values.
|
|
385
|
+
*/
|
|
386
|
+
morris<C extends OneParamCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null): ReturnType<C>[];
|
|
387
|
+
/**
|
|
388
|
+
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
389
|
+
* either an iterative or recursive manner.
|
|
390
|
+
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
391
|
+
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
392
|
+
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
393
|
+
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
394
|
+
* binary tree nodes in a specific order.
|
|
331
395
|
*/
|
|
332
|
-
|
|
396
|
+
[Symbol.iterator](node?: N | null): Generator<BinaryTreeNodeKey, void, undefined>;
|
|
333
397
|
/**
|
|
334
398
|
* Swap the data of two nodes in the binary tree.
|
|
335
399
|
* @param {N} srcNode - The source node to swap.
|
|
@@ -344,7 +408,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
344
408
|
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
345
409
|
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
346
410
|
*/
|
|
347
|
-
protected _defaultCallbackByKey:
|
|
411
|
+
protected _defaultCallbackByKey: OneParamCallback<N, BinaryTreeNodeKey>;
|
|
348
412
|
/**
|
|
349
413
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
350
414
|
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
@@ -283,8 +283,8 @@ class BinaryTree {
|
|
|
283
283
|
let needBalanced = null, orgCurrent = curr;
|
|
284
284
|
if (!curr.left) {
|
|
285
285
|
if (!parent) {
|
|
286
|
-
|
|
287
|
-
|
|
286
|
+
// Handle the case when there's only one root node
|
|
287
|
+
this._setRoot(null);
|
|
288
288
|
}
|
|
289
289
|
else {
|
|
290
290
|
const { familyPosition: fp } = curr;
|
|
@@ -707,7 +707,7 @@ class BinaryTree {
|
|
|
707
707
|
* start from the root of the tree.
|
|
708
708
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
709
709
|
* performed on the binary tree. It can have two possible values:
|
|
710
|
-
* @returns The function `subTreeTraverse` returns an array of `
|
|
710
|
+
* @returns The function `subTreeTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
|
|
711
711
|
*/
|
|
712
712
|
subTreeTraverse(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
713
713
|
if (typeof beginRoot === 'number')
|
|
@@ -747,7 +747,7 @@ class BinaryTree {
|
|
|
747
747
|
* is `null`, an empty array will be returned.
|
|
748
748
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
749
749
|
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
750
|
-
* @returns The function `dfs` returns an array of `
|
|
750
|
+
* @returns The function `dfs` returns an array of `ReturnType<OneParamCallback<N>>` values.
|
|
751
751
|
*/
|
|
752
752
|
dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
|
|
753
753
|
if (!beginRoot)
|
|
@@ -824,13 +824,13 @@ 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
|
-
* `
|
|
827
|
+
* `ReturnType<OneParamCallback<N>>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
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.
|
|
831
831
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
832
832
|
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
833
|
-
* @returns The function `bfs` returns an array of `
|
|
833
|
+
* @returns The function `bfs` returns an array of `ReturnType<OneParamCallback<N>>[]`.
|
|
834
834
|
*/
|
|
835
835
|
bfs(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
836
836
|
if (!beginRoot)
|
|
@@ -938,7 +938,7 @@ class BinaryTree {
|
|
|
938
938
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
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
|
-
* tree. It takes a node of type `N` as input and returns a value of type `
|
|
941
|
+
* tree. It takes a node of type `N` as input and returns a value of type `ReturnType<OneParamCallback<N>>`. The
|
|
942
942
|
* default value for this parameter is `this._defaultCallbackByKey`.
|
|
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
|
|
@@ -946,7 +946,7 @@ class BinaryTree {
|
|
|
946
946
|
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
947
947
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
948
948
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
949
|
-
* @returns The `morris` function returns an array of `
|
|
949
|
+
* @returns The `morris` function returns an array of `ReturnType<OneParamCallback<N>>` values.
|
|
950
950
|
*/
|
|
951
951
|
morris(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root) {
|
|
952
952
|
if (beginRoot === null)
|
|
@@ -1032,6 +1032,44 @@ class BinaryTree {
|
|
|
1032
1032
|
}
|
|
1033
1033
|
return ans;
|
|
1034
1034
|
}
|
|
1035
|
+
/**
|
|
1036
|
+
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1037
|
+
* either an iterative or recursive manner.
|
|
1038
|
+
* @param node - The `node` parameter represents the current node in the binary tree from which the
|
|
1039
|
+
* iteration starts. It is an optional parameter with a default value of `this.root`, which means
|
|
1040
|
+
* that if no node is provided, the iteration will start from the root of the binary tree.
|
|
1041
|
+
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
1042
|
+
* binary tree nodes in a specific order.
|
|
1043
|
+
*/
|
|
1044
|
+
*[Symbol.iterator](node = this.root) {
|
|
1045
|
+
if (!node) {
|
|
1046
|
+
return;
|
|
1047
|
+
}
|
|
1048
|
+
if (this.iterationType === types_1.IterationType.ITERATIVE) {
|
|
1049
|
+
const stack = [];
|
|
1050
|
+
let current = node;
|
|
1051
|
+
while (current || stack.length > 0) {
|
|
1052
|
+
while (current) {
|
|
1053
|
+
stack.push(current);
|
|
1054
|
+
current = current.left;
|
|
1055
|
+
}
|
|
1056
|
+
current = stack.pop();
|
|
1057
|
+
if (current)
|
|
1058
|
+
yield current.key;
|
|
1059
|
+
if (current)
|
|
1060
|
+
current = current.right;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
else {
|
|
1064
|
+
if (node.left) {
|
|
1065
|
+
yield* this[Symbol.iterator](node.left);
|
|
1066
|
+
}
|
|
1067
|
+
yield node.key;
|
|
1068
|
+
if (node.right) {
|
|
1069
|
+
yield* this[Symbol.iterator](node.right);
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1035
1073
|
/**
|
|
1036
1074
|
* Swap the data of two nodes in the binary tree.
|
|
1037
1075
|
* @param {N} srcNode - The source node to swap.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions,
|
|
8
|
+
import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, OneParamCallback } from '../../types';
|
|
9
9
|
import { CP, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -59,7 +59,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
59
59
|
* callback.
|
|
60
60
|
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
|
|
61
61
|
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
62
|
-
* value (`BinaryTreeNodeKey`) or a custom callback function (`
|
|
62
|
+
* value (`BinaryTreeNodeKey`) or a custom callback function (`OneParamCallback<N>`) that determines
|
|
63
63
|
* whether a node matches the desired property.
|
|
64
64
|
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
65
65
|
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
@@ -72,7 +72,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
72
72
|
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
73
73
|
* matching node is found.
|
|
74
74
|
*/
|
|
75
|
-
get<C extends
|
|
75
|
+
get<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
76
76
|
/**
|
|
77
77
|
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
78
78
|
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
@@ -110,7 +110,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
110
110
|
* traverse the binary tree. It can have one of the following values:
|
|
111
111
|
* @returns an array of nodes (N[]).
|
|
112
112
|
*/
|
|
113
|
-
getNodes<C extends
|
|
113
|
+
getNodes<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
114
114
|
/**
|
|
115
115
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
116
116
|
* nodes that have a key value lesser or greater than a target key value.
|
|
@@ -126,9 +126,9 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
126
126
|
* (`BinaryTreeNodeKey`), or `null` to
|
|
127
127
|
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
128
128
|
* done recursively or iteratively. It can have two possible values:
|
|
129
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of `
|
|
129
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
|
|
130
130
|
*/
|
|
131
|
-
lesserOrGreaterTraverse<C extends
|
|
131
|
+
lesserOrGreaterTraverse<C extends OneParamCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[];
|
|
132
132
|
/**
|
|
133
133
|
* Balancing Adjustment:
|
|
134
134
|
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|