linked-list-typed 1.51.5 → 1.51.8
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/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -12
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +2 -10
- package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/avl-tree.js +12 -14
- package/dist/data-structures/binary-tree/binary-tree.d.ts +7 -13
- package/dist/data-structures/binary-tree/binary-tree.js +46 -78
- package/dist/data-structures/binary-tree/bst.d.ts +51 -96
- package/dist/data-structures/binary-tree/bst.js +120 -218
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/data-structures/binary-tree/rb-tree.js +4 -2
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/data-structures/binary-tree/tree-multi-map.js +1 -0
- package/dist/data-structures/heap/heap.d.ts +1 -3
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
- package/dist/types/utils/utils.d.ts +10 -1
- package/dist/utils/utils.d.ts +2 -1
- package/dist/utils/utils.js +29 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +5 -12
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +56 -76
- package/src/data-structures/binary-tree/bst.ts +132 -224
- package/src/data-structures/binary-tree/rb-tree.ts +9 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +5 -3
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/interfaces/binary-tree.ts +4 -3
- package/src/types/common.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -2
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
|
@@ -14,12 +14,14 @@ import type {
|
|
|
14
14
|
BinaryTreePrintOptions,
|
|
15
15
|
BTNCallback,
|
|
16
16
|
BTNEntry,
|
|
17
|
+
Comparable,
|
|
17
18
|
DFSOrderPattern,
|
|
18
19
|
EntryCallback,
|
|
20
|
+
FamilyPosition,
|
|
21
|
+
IterationType,
|
|
19
22
|
KeyOrNodeOrEntry,
|
|
20
23
|
NodeDisplayLayout
|
|
21
24
|
} from '../../types';
|
|
22
|
-
import { FamilyPosition, IterationType } from '../../types';
|
|
23
25
|
import { IBinaryTree } from '../../interfaces';
|
|
24
26
|
import { trampoline } from '../../utils';
|
|
25
27
|
import { Queue } from '../queue';
|
|
@@ -31,7 +33,7 @@ import { IterableEntryBase } from '../base';
|
|
|
31
33
|
* @template NODE - The type of the family relationship in the binary tree.
|
|
32
34
|
*/
|
|
33
35
|
export class BinaryTreeNode<
|
|
34
|
-
K
|
|
36
|
+
K extends Comparable,
|
|
35
37
|
V = any,
|
|
36
38
|
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>
|
|
37
39
|
> {
|
|
@@ -129,7 +131,7 @@ export class BinaryTreeNode<
|
|
|
129
131
|
*/
|
|
130
132
|
|
|
131
133
|
export class BinaryTree<
|
|
132
|
-
K
|
|
134
|
+
K extends Comparable,
|
|
133
135
|
V = any,
|
|
134
136
|
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
|
|
135
137
|
TREE extends BinaryTree<K, V, NODE, TREE> = BinaryTree<K, V, NODE, BinaryTreeNested<K, V, NODE>>
|
|
@@ -147,12 +149,11 @@ export class BinaryTree<
|
|
|
147
149
|
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
148
150
|
* required.
|
|
149
151
|
*/
|
|
150
|
-
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: BinaryTreeOptions
|
|
152
|
+
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: BinaryTreeOptions) {
|
|
151
153
|
super();
|
|
152
154
|
if (options) {
|
|
153
|
-
const { iterationType
|
|
155
|
+
const { iterationType } = options;
|
|
154
156
|
if (iterationType) this.iterationType = iterationType;
|
|
155
|
-
if (extractor) this._extractor = extractor;
|
|
156
157
|
}
|
|
157
158
|
|
|
158
159
|
this._size = 0;
|
|
@@ -160,16 +161,6 @@ export class BinaryTree<
|
|
|
160
161
|
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
|
|
161
162
|
}
|
|
162
163
|
|
|
163
|
-
protected _extractor = (key: K) => (typeof key === 'number' ? key : Number(key));
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* The function returns the value of the `_extractor` property.
|
|
167
|
-
* @returns The `_extractor` property is being returned.
|
|
168
|
-
*/
|
|
169
|
-
get extractor() {
|
|
170
|
-
return this._extractor;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
164
|
protected _root?: NODE | null;
|
|
174
165
|
|
|
175
166
|
/**
|
|
@@ -218,7 +209,7 @@ export class BinaryTree<
|
|
|
218
209
|
* you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
|
|
219
210
|
* @returns a new instance of a binary tree.
|
|
220
211
|
*/
|
|
221
|
-
createTree(options?: Partial<BinaryTreeOptions
|
|
212
|
+
createTree(options?: Partial<BinaryTreeOptions>): TREE {
|
|
222
213
|
return new BinaryTree<K, V, NODE, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
|
|
223
214
|
}
|
|
224
215
|
|
|
@@ -278,17 +269,19 @@ export class BinaryTree<
|
|
|
278
269
|
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
279
270
|
iterationType: IterationType = 'ITERATIVE'
|
|
280
271
|
): NODE | null | undefined {
|
|
272
|
+
if (keyOrNodeOrEntry === this.NIL) return;
|
|
281
273
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
282
274
|
return keyOrNodeOrEntry;
|
|
283
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
284
|
-
if (keyOrNodeOrEntry[0] === null) return null;
|
|
285
|
-
if (keyOrNodeOrEntry[0] === undefined) return;
|
|
286
|
-
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
287
|
-
} else {
|
|
288
|
-
if (keyOrNodeOrEntry === null) return null;
|
|
289
|
-
if (keyOrNodeOrEntry === undefined) return;
|
|
290
|
-
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
291
275
|
}
|
|
276
|
+
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
277
|
+
const key = keyOrNodeOrEntry[0];
|
|
278
|
+
if (key === null) return null;
|
|
279
|
+
if (key === undefined) return;
|
|
280
|
+
return this.getNodeByKey(key, iterationType);
|
|
281
|
+
}
|
|
282
|
+
if (keyOrNodeOrEntry === null) return null;
|
|
283
|
+
if (keyOrNodeOrEntry === undefined) return;
|
|
284
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
292
285
|
}
|
|
293
286
|
|
|
294
287
|
/**
|
|
@@ -506,8 +499,7 @@ export class BinaryTree<
|
|
|
506
499
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
507
500
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
508
501
|
if (!this.root) return deletedResult;
|
|
509
|
-
|
|
510
|
-
callback = (node => node) as C;
|
|
502
|
+
callback = this._ensureCallback(identifier, callback);
|
|
511
503
|
|
|
512
504
|
const curr = this.getNode(identifier, callback);
|
|
513
505
|
if (!curr) return deletedResult;
|
|
@@ -610,10 +602,9 @@ export class BinaryTree<
|
|
|
610
602
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
611
603
|
iterationType: IterationType = this.iterationType
|
|
612
604
|
): NODE[] {
|
|
613
|
-
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
|
|
614
|
-
callback = (node => node) as C;
|
|
615
605
|
beginRoot = this.ensureNode(beginRoot);
|
|
616
606
|
if (!beginRoot) return [];
|
|
607
|
+
callback = this._ensureCallback(identifier, callback);
|
|
617
608
|
|
|
618
609
|
const ans: NODE[] = [];
|
|
619
610
|
|
|
@@ -722,29 +713,8 @@ export class BinaryTree<
|
|
|
722
713
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
723
714
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
724
715
|
*/
|
|
725
|
-
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
726
|
-
|
|
727
|
-
if (iterationType === 'RECURSIVE') {
|
|
728
|
-
const dfs = (cur: NODE): NODE | undefined => {
|
|
729
|
-
if (cur.key === key) return cur;
|
|
730
|
-
|
|
731
|
-
if (!cur.left && !cur.right) return;
|
|
732
|
-
if (cur.left) return dfs(cur.left);
|
|
733
|
-
if (cur.right) return dfs(cur.right);
|
|
734
|
-
};
|
|
735
|
-
|
|
736
|
-
return dfs(this.root);
|
|
737
|
-
} else {
|
|
738
|
-
const stack = [this.root];
|
|
739
|
-
while (stack.length > 0) {
|
|
740
|
-
const cur = stack.pop();
|
|
741
|
-
if (cur) {
|
|
742
|
-
if (cur.key === key) return cur;
|
|
743
|
-
cur.left && stack.push(cur.left);
|
|
744
|
-
cur.right && stack.push(cur.right);
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
}
|
|
716
|
+
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | null | undefined {
|
|
717
|
+
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
748
718
|
}
|
|
749
719
|
|
|
750
720
|
override get<C extends BTNCallback<NODE, K>>(
|
|
@@ -801,7 +771,7 @@ export class BinaryTree<
|
|
|
801
771
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
802
772
|
iterationType: IterationType = this.iterationType
|
|
803
773
|
): V | undefined {
|
|
804
|
-
return this.getNode(identifier, callback, beginRoot, iterationType)?.value
|
|
774
|
+
return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
|
|
805
775
|
}
|
|
806
776
|
|
|
807
777
|
override has<C extends BTNCallback<NODE, K>>(
|
|
@@ -857,8 +827,7 @@ export class BinaryTree<
|
|
|
857
827
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
858
828
|
iterationType: IterationType = this.iterationType
|
|
859
829
|
): boolean {
|
|
860
|
-
|
|
861
|
-
callback = (node => node) as C;
|
|
830
|
+
callback = this._ensureCallback(identifier, callback);
|
|
862
831
|
|
|
863
832
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
864
833
|
}
|
|
@@ -943,7 +912,7 @@ export class BinaryTree<
|
|
|
943
912
|
if (iterationType === 'RECURSIVE') {
|
|
944
913
|
const dfs = (cur: NODE | null | undefined, min: number, max: number): boolean => {
|
|
945
914
|
if (!this.isRealNode(cur)) return true;
|
|
946
|
-
const numKey =
|
|
915
|
+
const numKey = Number(cur.key);
|
|
947
916
|
if (numKey <= min || numKey >= max) return false;
|
|
948
917
|
return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
|
|
949
918
|
};
|
|
@@ -963,7 +932,7 @@ export class BinaryTree<
|
|
|
963
932
|
curr = curr.left;
|
|
964
933
|
}
|
|
965
934
|
curr = stack.pop()!;
|
|
966
|
-
const numKey =
|
|
935
|
+
const numKey = Number(curr.key);
|
|
967
936
|
if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey)) return false;
|
|
968
937
|
prev = numKey;
|
|
969
938
|
curr = curr.right;
|
|
@@ -995,15 +964,15 @@ export class BinaryTree<
|
|
|
995
964
|
* @returns the depth of the `dist` relative to the `beginRoot`.
|
|
996
965
|
*/
|
|
997
966
|
getDepth(dist: KeyOrNodeOrEntry<K, V, NODE>, beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root): number {
|
|
998
|
-
|
|
999
|
-
|
|
967
|
+
let distEnsured = this.ensureNode(dist);
|
|
968
|
+
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
1000
969
|
let depth = 0;
|
|
1001
|
-
while (
|
|
1002
|
-
if (
|
|
970
|
+
while (distEnsured?.parent) {
|
|
971
|
+
if (distEnsured === beginRootEnsured) {
|
|
1003
972
|
return depth;
|
|
1004
973
|
}
|
|
1005
974
|
depth++;
|
|
1006
|
-
|
|
975
|
+
distEnsured = distEnsured.parent;
|
|
1007
976
|
}
|
|
1008
977
|
return depth;
|
|
1009
978
|
}
|
|
@@ -1146,17 +1115,17 @@ export class BinaryTree<
|
|
|
1146
1115
|
getPathToRoot(beginNode: KeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
|
|
1147
1116
|
// TODO to support get path through passing key
|
|
1148
1117
|
const result: NODE[] = [];
|
|
1149
|
-
|
|
1118
|
+
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
1150
1119
|
|
|
1151
|
-
if (!
|
|
1120
|
+
if (!beginNodeEnsured) return result;
|
|
1152
1121
|
|
|
1153
|
-
while (
|
|
1122
|
+
while (beginNodeEnsured.parent) {
|
|
1154
1123
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
1155
1124
|
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
1156
|
-
result.push(
|
|
1157
|
-
|
|
1125
|
+
result.push(beginNodeEnsured);
|
|
1126
|
+
beginNodeEnsured = beginNodeEnsured.parent;
|
|
1158
1127
|
}
|
|
1159
|
-
result.push(
|
|
1128
|
+
result.push(beginNodeEnsured);
|
|
1160
1129
|
return isReverse ? result.reverse() : result;
|
|
1161
1130
|
}
|
|
1162
1131
|
|
|
@@ -1862,10 +1831,10 @@ export class BinaryTree<
|
|
|
1862
1831
|
console.log(`U for undefined
|
|
1863
1832
|
`);
|
|
1864
1833
|
if (opts.isShowNull)
|
|
1865
|
-
console.log(`
|
|
1834
|
+
console.log(`N for null
|
|
1866
1835
|
`);
|
|
1867
1836
|
if (opts.isShowRedBlackNIL)
|
|
1868
|
-
console.log(`S for Sentinel Node
|
|
1837
|
+
console.log(`S for Sentinel Node(NIL)
|
|
1869
1838
|
`);
|
|
1870
1839
|
|
|
1871
1840
|
const display = (root: NODE | null | undefined): void => {
|
|
@@ -1895,24 +1864,24 @@ export class BinaryTree<
|
|
|
1895
1864
|
let current: NODE | null | undefined = node;
|
|
1896
1865
|
|
|
1897
1866
|
while (current || stack.length > 0) {
|
|
1898
|
-
while (
|
|
1867
|
+
while (this.isRealNode(current)) {
|
|
1899
1868
|
stack.push(current);
|
|
1900
1869
|
current = current.left;
|
|
1901
1870
|
}
|
|
1902
1871
|
|
|
1903
1872
|
current = stack.pop();
|
|
1904
1873
|
|
|
1905
|
-
if (
|
|
1874
|
+
if (this.isRealNode(current)) {
|
|
1906
1875
|
yield [current.key, current.value];
|
|
1907
1876
|
current = current.right;
|
|
1908
1877
|
}
|
|
1909
1878
|
}
|
|
1910
1879
|
} else {
|
|
1911
|
-
if (node.left &&
|
|
1880
|
+
if (node.left && this.isRealNode(node)) {
|
|
1912
1881
|
yield* this[Symbol.iterator](node.left);
|
|
1913
1882
|
}
|
|
1914
1883
|
yield [node.key, node.value];
|
|
1915
|
-
if (node.right &&
|
|
1884
|
+
if (node.right && this.isRealNode(node)) {
|
|
1916
1885
|
yield* this[Symbol.iterator](node.right);
|
|
1917
1886
|
}
|
|
1918
1887
|
}
|
|
@@ -1941,13 +1910,13 @@ export class BinaryTree<
|
|
|
1941
1910
|
return emptyDisplayLayout;
|
|
1942
1911
|
} else if (node === undefined && !isShowUndefined) {
|
|
1943
1912
|
return emptyDisplayLayout;
|
|
1944
|
-
} else if (
|
|
1913
|
+
} else if (this.isNIL(node) && !isShowRedBlackNIL) {
|
|
1945
1914
|
return emptyDisplayLayout;
|
|
1946
1915
|
} else if (node !== null && node !== undefined) {
|
|
1947
1916
|
// Display logic of normal nodes
|
|
1948
1917
|
|
|
1949
1918
|
const key = node.key,
|
|
1950
|
-
line =
|
|
1919
|
+
line = this.isNIL(node) ? 'S' : key.toString(),
|
|
1951
1920
|
width = line.length;
|
|
1952
1921
|
|
|
1953
1922
|
return _buildNodeDisplay(
|
|
@@ -2070,4 +2039,15 @@ export class BinaryTree<
|
|
|
2070
2039
|
}
|
|
2071
2040
|
this._root = v;
|
|
2072
2041
|
}
|
|
2042
|
+
|
|
2043
|
+
protected _ensureCallback<C extends BTNCallback<NODE>>(
|
|
2044
|
+
identifier: ReturnType<C> | null | undefined,
|
|
2045
|
+
callback: C = this._DEFAULT_CALLBACK as C
|
|
2046
|
+
): C {
|
|
2047
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
|
|
2048
|
+
callback = (node => node) as C;
|
|
2049
|
+
}
|
|
2050
|
+
|
|
2051
|
+
return callback;
|
|
2052
|
+
}
|
|
2073
2053
|
}
|