data-structure-typed 1.47.6 → 1.47.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +103 -28
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/cjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +6 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +133 -128
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/mjs/data-structures/binary-tree/bst.js +137 -154
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +59 -138
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +6 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/umd/data-structure-typed.js +422 -466
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +87 -0
- package/test/performance/data-structures/comparison/comparison.test.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +19 -19
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -51
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/unrestricted-interconversion.test.ts +61 -5
- package/tsconfig-cjs.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tree-multimap.js","sourceRoot":"","sources":["../../../../src/data-structures/binary-tree/tree-multimap.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"tree-multimap.js","sourceRoot":"","sources":["../../../../src/data-structures/binary-tree/tree-multimap.ts"],"names":[],"mappings":";;;AAcA,uCAAiH;AAEjH,yCAAkD;AAElD,MAAa,gBAGX,SAAQ,sBAAiB;IAGzB;;;;;;;;;OASG;IACH,YAAY,GAAW,EAAE,KAAS,EAAE,KAAK,GAAG,CAAC;QAC3C,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAClB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF;AApBD,4CAoBC;AAED;;GAEG;AACH,MAAa,YAEX,SAAQ,kBAAmB;IAG3B,YAAY,QAAyC,EAAE,OAAsC;QAC3F,KAAK,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;QAIb,WAAM,GAAG,CAAC,CAAC;QAHjB,IAAI,QAAQ;YAAE,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACvC,CAAC;IAID,6DAA6D;IAC7D,IAAI,KAAK;QACP,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG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K,GAAG,QAAQ,CAAC,KAAK,CAAC;gBAC/B,OAAO,CAAC,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC;gBAC/B,OAAO,CAAC,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC;aAClC;YAED,OAAO,QAAQ,CAAC;SACjB;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IAES,YAAY,CAAC,OAAU,EAAE,OAAU;QAC3C,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAA;QAC7C,OAAO,KAAK,CAAC,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC;CACF;AA3UD,oCA2UC"}
|
|
@@ -14,12 +14,7 @@ export declare class HashMap<K = any, V = any> {
|
|
|
14
14
|
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
15
15
|
protected _hashFn: (key: K) => string;
|
|
16
16
|
protected _objHashFn: (key: K) => object;
|
|
17
|
-
|
|
18
|
-
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
19
|
-
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
20
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
21
|
-
*/
|
|
22
|
-
constructor(options?: HashMapOptions<K, V>);
|
|
17
|
+
constructor(elements?: Iterable<[K, V]>, options?: HashMapOptions<K>);
|
|
23
18
|
protected _size: number;
|
|
24
19
|
get size(): number;
|
|
25
20
|
/**
|
|
@@ -172,6 +167,7 @@ export declare class HashMap<K = any, V = any> {
|
|
|
172
167
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
173
168
|
*/
|
|
174
169
|
[Symbol.iterator](): Generator<[K, V], void, unknown>;
|
|
170
|
+
print(): void;
|
|
175
171
|
/**
|
|
176
172
|
* Time Complexity: O(1)
|
|
177
173
|
* Space Complexity: O(1)
|
|
@@ -10,13 +10,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.HashMap = void 0;
|
|
11
11
|
const utils_1 = require("../../utils");
|
|
12
12
|
class HashMap {
|
|
13
|
-
|
|
14
|
-
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
15
|
-
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
16
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
17
|
-
*/
|
|
18
|
-
constructor(options = {
|
|
19
|
-
elements: [],
|
|
13
|
+
constructor(elements, options = {
|
|
20
14
|
hashFn: (key) => String(key),
|
|
21
15
|
objHashFn: (key) => key
|
|
22
16
|
}) {
|
|
@@ -25,7 +19,7 @@ class HashMap {
|
|
|
25
19
|
this._size = 0;
|
|
26
20
|
this._sentinel = {};
|
|
27
21
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
28
|
-
const {
|
|
22
|
+
const { hashFn, objHashFn } = options;
|
|
29
23
|
this._hashFn = hashFn;
|
|
30
24
|
this._objHashFn = objHashFn;
|
|
31
25
|
if (elements) {
|
|
@@ -347,6 +341,9 @@ class HashMap {
|
|
|
347
341
|
node = node.next;
|
|
348
342
|
}
|
|
349
343
|
}
|
|
344
|
+
print() {
|
|
345
|
+
console.log([...this]);
|
|
346
|
+
}
|
|
350
347
|
/**
|
|
351
348
|
* Time Complexity: O(1)
|
|
352
349
|
* Space Complexity: O(1)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash-map.js","sourceRoot":"","sources":["../../../../src/data-structures/hash/hash-map.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,uCAAoD;AAGpD,MAAa,OAAO;
|
|
1
|
+
{"version":3,"file":"hash-map.js","sourceRoot":"","sources":["../../../../src/data-structures/hash/hash-map.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,uCAAoD;AAGpD,MAAa,OAAO;IAWlB,YAAY,QAA2B,EAAE,UAA6B;QAEpE,MAAM,EAAE,C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AClB;IACH,CAAC;IAED,KAAK;QACH,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;IACzB,CAAC;IAED;;;;;;;;;OASG;IACO,WAAW,CAAC,IAAyC;QAC7D,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,IAAI,IAAI,KAAK,IAAI,CAAC,KAAK,EAAE;YACvB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;SACnB;QAED,IAAI,IAAI,KAAK,IAAI,CAAC,KAAK,EAAE;YACvB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;SACnB;QAED,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC;IAClB,CAAC;CACF;AA3YD,0BA2YC"}
|
|
@@ -20,6 +20,8 @@ export declare class TrieNode {
|
|
|
20
20
|
*/
|
|
21
21
|
export declare class Trie {
|
|
22
22
|
constructor(words?: string[], caseSensitive?: boolean);
|
|
23
|
+
protected _size: number;
|
|
24
|
+
get size(): number;
|
|
23
25
|
protected _caseSensitive: boolean;
|
|
24
26
|
get caseSensitive(): boolean;
|
|
25
27
|
protected _root: TrieNode;
|
|
@@ -145,6 +147,7 @@ export declare class Trie {
|
|
|
145
147
|
filter(predicate: (word: string, index: number, trie: this) => boolean): string[];
|
|
146
148
|
map(callback: (word: string, index: number, trie: this) => string): Trie;
|
|
147
149
|
reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T;
|
|
150
|
+
print(): void;
|
|
148
151
|
/**
|
|
149
152
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
150
153
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -27,12 +27,16 @@ class Trie {
|
|
|
27
27
|
constructor(words, caseSensitive = true) {
|
|
28
28
|
this._root = new TrieNode('');
|
|
29
29
|
this._caseSensitive = caseSensitive;
|
|
30
|
+
this._size = 0;
|
|
30
31
|
if (words) {
|
|
31
|
-
for (const
|
|
32
|
-
this.add(
|
|
32
|
+
for (const word of words) {
|
|
33
|
+
this.add(word);
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
36
|
}
|
|
37
|
+
get size() {
|
|
38
|
+
return this._size;
|
|
39
|
+
}
|
|
36
40
|
get caseSensitive() {
|
|
37
41
|
return this._caseSensitive;
|
|
38
42
|
}
|
|
@@ -54,6 +58,7 @@ class Trie {
|
|
|
54
58
|
add(word) {
|
|
55
59
|
word = this._caseProcess(word);
|
|
56
60
|
let cur = this.root;
|
|
61
|
+
let isNewWord = false;
|
|
57
62
|
for (const c of word) {
|
|
58
63
|
let nodeC = cur.children.get(c);
|
|
59
64
|
if (!nodeC) {
|
|
@@ -62,8 +67,12 @@ class Trie {
|
|
|
62
67
|
}
|
|
63
68
|
cur = nodeC;
|
|
64
69
|
}
|
|
65
|
-
cur.isEnd
|
|
66
|
-
|
|
70
|
+
if (!cur.isEnd) {
|
|
71
|
+
isNewWord = true;
|
|
72
|
+
cur.isEnd = true;
|
|
73
|
+
this._size++;
|
|
74
|
+
}
|
|
75
|
+
return isNewWord;
|
|
67
76
|
}
|
|
68
77
|
/**
|
|
69
78
|
* Time Complexity: O(M), where M is the length of the input word.
|
|
@@ -130,6 +139,9 @@ class Trie {
|
|
|
130
139
|
return false;
|
|
131
140
|
};
|
|
132
141
|
dfs(this.root, 0);
|
|
142
|
+
if (isDeleted) {
|
|
143
|
+
this._size--;
|
|
144
|
+
}
|
|
133
145
|
return isDeleted;
|
|
134
146
|
}
|
|
135
147
|
/**
|
|
@@ -352,6 +364,9 @@ class Trie {
|
|
|
352
364
|
}
|
|
353
365
|
return accumulator;
|
|
354
366
|
}
|
|
367
|
+
print() {
|
|
368
|
+
console.log([...this]);
|
|
369
|
+
}
|
|
355
370
|
/**
|
|
356
371
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
357
372
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trie.js","sourceRoot":"","sources":["../../../../src/data-structures/trie/trie.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH;;;GAGG;AACH,MAAa,QAAQ;IAKnB,YAAY,GAAW;QACrB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,QAAQ,GAAG,IAAI,GAAG,EAAoB,CAAC;IAC9C,CAAC;CACF;AAVD,4BAUC;AAED;;GAEG;AACH,MAAa,IAAI;IACf,YAAY,KAAgB,EAAE,aAAa,GAAG,IAAI;QAChD,IAAI,CAAC,KAAK,GAAG,IAAI,QAAQ,CAAC,EAAE,CAAC,CAAC;QAC9B,IAAI,CAAC,cAAc,GAAG,aAAa,CAAC;QACpC,IAAI,KAAK,EAAE;YACT,KAAK,MAAM,
|
|
1
|
+
{"version":3,"file":"trie.js","sourceRoot":"","sources":["../../../../src/data-structures/trie/trie.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH;;;GAGG;AACH,MAAa,QAAQ;IAKnB,YAAY,GAAW;QACrB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,QAAQ,GAAG,IAAI,GAAG,EAAoB,CAAC;IAC9C,CAAC;CACF;AAVD,4BAUC;AAED;;GAEG;AACH,MAAa,IAAI;IACf,YAAY,KAAgB,EAAE,aAAa,GAAG,IAAI;QAChD,IAAI,CAAC,KAAK,GAAG,IAAI,QAAQ,CAAC,EAAE,CAAC,CAAC;QAC9B,IAAI,CAAC,cAAc,GAAG,aAAa,CAAC;QACpC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;QACf,IAAI,KAAK,EAAE;YACT,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;gBACxB,IAAI,CAAC,GAAG,CAAC,IAAI,C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CAAC;IAED,GAAG,CAAC,QAA6D;QAC/D,MAAM,OAAO,GAAG,IAAI,IAAI,EAAE,CAAC;QAC3B,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE;YACvB,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;YACzC,KAAK,EAAE,CAAC;SACT;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,MAAM,CAAI,QAAwE,EAAE,YAAe;QACjG,IAAI,WAAW,GAAG,YAAY,CAAC;QAC/B,IAAI,KAAK,GAAG,CAAC,CAAC;QACd,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE;YACvB,WAAW,GAAG,QAAQ,CAAC,WAAW,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;YACvD,KAAK,EAAE,CAAC;SACT;QACD,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,KAAK;QACH,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;IACzB,CAAC;IAED;;;OAGG;IAEH;;;;;;OAMG;IACO,YAAY,CAAC,GAAW;QAChC,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE;YACxB,GAAG,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC,+CAA+C;SACzE;QACD,OAAO,GAAG,CAAC;IACb,CAAC;CACF;AArYD,oBAqYC"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey,
|
|
2
|
+
import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey, BTNodeExemplar } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
|
|
4
4
|
createNode(key: BTNKey, value?: N['value']): N;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | null | undefined;
|
|
7
|
+
addMany(nodes: Iterable<BTNodeExemplar<V, N>>): (N | null | undefined)[];
|
|
8
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
9
9
|
}
|
|
@@ -19,4 +19,9 @@ export type BinaryTreePrintOptions = {
|
|
|
19
19
|
isShowNull?: boolean;
|
|
20
20
|
isShowRedBlackNIL?: boolean;
|
|
21
21
|
};
|
|
22
|
-
export type
|
|
22
|
+
export type BTNodeEntry<T> = [BTNKey | null | undefined, T | undefined];
|
|
23
|
+
export type BTNodeKeyOrNode<N> = BTNKey | null | undefined | N;
|
|
24
|
+
export type BTNodeExemplar<T, N> = BTNodeEntry<T> | BTNodeKeyOrNode<N>;
|
|
25
|
+
export type BTNodePureExemplar<T, N> = [BTNKey, T | undefined] | BTNodePureKeyOrNode<N>;
|
|
26
|
+
export type BTNodePureKeyOrNode<N> = BTNKey | N;
|
|
27
|
+
export type BSTNodeKeyOrNode<N> = BTNKey | undefined | N;
|
|
@@ -4,8 +4,7 @@ export type HashMapLinkedNode<K, V> = {
|
|
|
4
4
|
next: HashMapLinkedNode<K, V>;
|
|
5
5
|
prev: HashMapLinkedNode<K, V>;
|
|
6
6
|
};
|
|
7
|
-
export type HashMapOptions<K
|
|
8
|
-
elements: Iterable<[K, V]>;
|
|
7
|
+
export type HashMapOptions<K> = {
|
|
9
8
|
hashFn: (key: K) => string;
|
|
10
9
|
objHashFn: (key: K) => object;
|
|
11
10
|
};
|
|
@@ -6,21 +6,34 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
|
|
10
|
-
import { BTNCallback
|
|
9
|
+
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BSTNodeKeyOrNode, BTNKey, BTNodeExemplar } from '../../types';
|
|
10
|
+
import { BTNCallback } 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;
|
|
14
14
|
constructor(key: BTNKey, value?: V);
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
18
|
+
* 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
|
|
19
|
+
* 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
|
|
20
|
+
* 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
|
|
21
|
+
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
22
|
+
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
23
|
+
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
24
|
+
* 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
|
|
25
|
+
*/
|
|
16
26
|
export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>, TREE extends AVLTree<V, N, TREE> = AVLTree<V, N, AVLTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
17
27
|
/**
|
|
18
|
-
*
|
|
19
|
-
* @param
|
|
20
|
-
*
|
|
21
|
-
*
|
|
28
|
+
* The constructor function initializes an AVLTree object with optional elements and options.
|
|
29
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
30
|
+
* objects. It represents a collection of elements that will be added to the AVL tree during
|
|
31
|
+
* initialization.
|
|
32
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
33
|
+
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
34
|
+
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
22
35
|
*/
|
|
23
|
-
constructor(elements?:
|
|
36
|
+
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<AVLTreeOptions>);
|
|
24
37
|
/**
|
|
25
38
|
* The function creates a new AVL tree node with the specified key and value.
|
|
26
39
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
@@ -31,20 +44,29 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
31
44
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
32
45
|
*/
|
|
33
46
|
createNode(key: BTNKey, value?: V): N;
|
|
47
|
+
/**
|
|
48
|
+
* The function creates a new AVL tree with the specified options and returns it.
|
|
49
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
50
|
+
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
51
|
+
* being created.
|
|
52
|
+
* @returns a new AVLTree object.
|
|
53
|
+
*/
|
|
34
54
|
createTree(options?: AVLTreeOptions): TREE;
|
|
55
|
+
/**
|
|
56
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
57
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
58
|
+
*/
|
|
35
59
|
/**
|
|
36
60
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
37
61
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
38
62
|
*
|
|
39
|
-
* The function overrides the add method of a
|
|
40
|
-
*
|
|
41
|
-
* @param
|
|
42
|
-
*
|
|
43
|
-
* @
|
|
44
|
-
* added to the binary search tree.
|
|
45
|
-
* @returns The method is returning either a node (N) or undefined.
|
|
63
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
64
|
+
* a new node.
|
|
65
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
|
|
66
|
+
* entry.
|
|
67
|
+
* @returns The method is returning either the inserted node or `undefined`.
|
|
46
68
|
*/
|
|
47
|
-
add(
|
|
69
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
|
|
48
70
|
/**
|
|
49
71
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
50
72
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -66,12 +88,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
66
88
|
*/
|
|
67
89
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C): BiTreeDeleteResult<N>[];
|
|
68
90
|
/**
|
|
69
|
-
*
|
|
70
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
71
|
-
*/
|
|
72
|
-
init(elements: IterableEntriesOrKeys<V>): void;
|
|
73
|
-
/**
|
|
74
|
-
* The `_swap` function swaps the key, value, and height properties between two nodes in a binary
|
|
91
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
75
92
|
* tree.
|
|
76
93
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
77
94
|
* needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
@@ -80,7 +97,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
80
97
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
81
98
|
* if either `srcNode` or `destNode` is undefined.
|
|
82
99
|
*/
|
|
83
|
-
protected
|
|
100
|
+
protected _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined;
|
|
84
101
|
/**
|
|
85
102
|
* Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
|
|
86
103
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -170,4 +187,5 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
|
|
|
170
187
|
* @param {N} A - A is a node in a binary tree.
|
|
171
188
|
*/
|
|
172
189
|
protected _balanceRL(A: N): void;
|
|
190
|
+
protected _replaceNode(oldNode: N, newNode: N): N;
|
|
173
191
|
}
|
|
@@ -13,17 +13,30 @@ export class AVLTreeNode extends BSTNode {
|
|
|
13
13
|
this.height = 0;
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
18
|
+
* 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
|
|
19
|
+
* 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
|
|
20
|
+
* 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
|
|
21
|
+
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
22
|
+
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
23
|
+
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
24
|
+
* 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
|
|
25
|
+
*/
|
|
16
26
|
export class AVLTree extends BST {
|
|
17
27
|
/**
|
|
18
|
-
*
|
|
19
|
-
* @param
|
|
20
|
-
*
|
|
21
|
-
*
|
|
28
|
+
* The constructor function initializes an AVLTree object with optional elements and options.
|
|
29
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
30
|
+
* objects. It represents a collection of elements that will be added to the AVL tree during
|
|
31
|
+
* initialization.
|
|
32
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
33
|
+
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
34
|
+
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
22
35
|
*/
|
|
23
36
|
constructor(elements, options) {
|
|
24
37
|
super([], options);
|
|
25
38
|
if (elements)
|
|
26
|
-
|
|
39
|
+
super.addMany(elements);
|
|
27
40
|
}
|
|
28
41
|
/**
|
|
29
42
|
* The function creates a new AVL tree node with the specified key and value.
|
|
@@ -37,28 +50,37 @@ export class AVLTree extends BST {
|
|
|
37
50
|
createNode(key, value) {
|
|
38
51
|
return new AVLTreeNode(key, value);
|
|
39
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* The function creates a new AVL tree with the specified options and returns it.
|
|
55
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
56
|
+
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
57
|
+
* being created.
|
|
58
|
+
* @returns a new AVLTree object.
|
|
59
|
+
*/
|
|
40
60
|
createTree(options) {
|
|
41
61
|
return new AVLTree([], {
|
|
42
62
|
iterationType: this.iterationType,
|
|
43
63
|
comparator: this.comparator, ...options
|
|
44
64
|
});
|
|
45
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
68
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
69
|
+
*/
|
|
46
70
|
/**
|
|
47
71
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
48
72
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
49
73
|
*
|
|
50
|
-
* The function overrides the add method of a
|
|
51
|
-
*
|
|
52
|
-
* @param
|
|
53
|
-
*
|
|
54
|
-
* @
|
|
55
|
-
* added to the binary search tree.
|
|
56
|
-
* @returns The method is returning either a node (N) or undefined.
|
|
74
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
75
|
+
* a new node.
|
|
76
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
|
|
77
|
+
* entry.
|
|
78
|
+
* @returns The method is returning either the inserted node or `undefined`.
|
|
57
79
|
*/
|
|
58
|
-
add(
|
|
59
|
-
if (
|
|
80
|
+
add(keyOrNodeOrEntry) {
|
|
81
|
+
if (keyOrNodeOrEntry === null)
|
|
60
82
|
return undefined;
|
|
61
|
-
const inserted = super.add(
|
|
83
|
+
const inserted = super.add(keyOrNodeOrEntry);
|
|
62
84
|
if (inserted)
|
|
63
85
|
this._balancePath(inserted);
|
|
64
86
|
return inserted;
|
|
@@ -94,24 +116,7 @@ export class AVLTree extends BST {
|
|
|
94
116
|
return deletedResults;
|
|
95
117
|
}
|
|
96
118
|
/**
|
|
97
|
-
*
|
|
98
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
99
|
-
*/
|
|
100
|
-
init(elements) {
|
|
101
|
-
if (elements) {
|
|
102
|
-
for (const entryOrKey of elements) {
|
|
103
|
-
if (Array.isArray(entryOrKey)) {
|
|
104
|
-
const [key, value] = entryOrKey;
|
|
105
|
-
this.add(key, value);
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
this.add(entryOrKey);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* The `_swap` function swaps the key, value, and height properties between two nodes in a binary
|
|
119
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
115
120
|
* tree.
|
|
116
121
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
117
122
|
* needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
@@ -120,9 +125,9 @@ export class AVLTree extends BST {
|
|
|
120
125
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
121
126
|
* if either `srcNode` or `destNode` is undefined.
|
|
122
127
|
*/
|
|
123
|
-
|
|
124
|
-
srcNode = this.
|
|
125
|
-
destNode = this.
|
|
128
|
+
_swapProperties(srcNode, destNode) {
|
|
129
|
+
srcNode = this.ensureNode(srcNode);
|
|
130
|
+
destNode = this.ensureNode(destNode);
|
|
126
131
|
if (srcNode && destNode) {
|
|
127
132
|
const { key, value, height } = destNode;
|
|
128
133
|
const tempNode = this.createNode(key, value);
|
|
@@ -433,4 +438,8 @@ export class AVLTree extends BST {
|
|
|
433
438
|
B && this._updateHeight(B);
|
|
434
439
|
C && this._updateHeight(C);
|
|
435
440
|
}
|
|
441
|
+
_replaceNode(oldNode, newNode) {
|
|
442
|
+
newNode.height = oldNode.height;
|
|
443
|
+
return super._replaceNode(oldNode, newNode);
|
|
444
|
+
}
|
|
436
445
|
}
|