binary-tree-typed 1.53.7 → 1.54.0
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/common/index.js +5 -0
- package/dist/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +34 -27
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +63 -53
- package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -14
- package/dist/data-structures/binary-tree/avl-tree.js +22 -25
- package/dist/data-structures/binary-tree/binary-tree.d.ts +33 -17
- package/dist/data-structures/binary-tree/binary-tree.js +45 -26
- package/dist/data-structures/binary-tree/bst.d.ts +67 -46
- package/dist/data-structures/binary-tree/bst.js +87 -54
- package/dist/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/data-structures/binary-tree/index.js +1 -1
- package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +75 -16
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +93 -60
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +20 -16
- package/dist/data-structures/binary-tree/tree-multi-map.js +41 -28
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +31 -1
- package/dist/data-structures/hash/hash-map.js +35 -5
- package/dist/data-structures/heap/heap.d.ts +20 -3
- package/dist/data-structures/heap/heap.js +31 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
- package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +44 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +70 -26
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +8 -3
- package/dist/data-structures/trie/trie.js +8 -3
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -4
- package/package.json +2 -2
- package/src/common/index.ts +7 -1
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +84 -65
- package/src/data-structures/binary-tree/avl-tree.ts +40 -34
- package/src/data-structures/binary-tree/binary-tree.ts +76 -32
- package/src/data-structures/binary-tree/bst.ts +121 -68
- package/src/data-structures/binary-tree/index.ts +1 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +116 -71
- package/src/data-structures/binary-tree/tree-multi-map.ts +59 -33
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +37 -7
- package/src/data-structures/heap/heap.ts +33 -10
- package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
- package/src/data-structures/linked-list/singly-linked-list.ts +77 -27
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +8 -3
- package/src/interfaces/binary-tree.ts +4 -1
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +3 -3
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -4
|
@@ -200,7 +200,7 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
|
|
|
200
200
|
* @returns The `addMany` method returns an array of boolean values indicating whether each word in
|
|
201
201
|
* the input iterable was successfully added to the data structure.
|
|
202
202
|
*/
|
|
203
|
-
addMany(words
|
|
203
|
+
addMany(words: Iterable<string> | Iterable<R>): boolean[];
|
|
204
204
|
/**
|
|
205
205
|
* Time Complexity: O(l), where l is the length of the input word.
|
|
206
206
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -235,9 +235,14 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
|
|
|
235
235
|
*/
|
|
236
236
|
delete(word: string): boolean;
|
|
237
237
|
/**
|
|
238
|
-
* Time Complexity: O(n)
|
|
239
|
-
* Space Complexity: O(1)
|
|
238
|
+
* Time Complexity: O(n)
|
|
239
|
+
* Space Complexity: O(1)
|
|
240
240
|
*
|
|
241
|
+
* The function `getHeight` calculates the height of a trie data structure starting from the root
|
|
242
|
+
* node.
|
|
243
|
+
* @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from
|
|
244
|
+
* the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie
|
|
245
|
+
* tree and returns the maximum depth found.
|
|
241
246
|
*/
|
|
242
247
|
getHeight(): number;
|
|
243
248
|
/**
|
|
@@ -243,7 +243,7 @@ class Trie extends base_1.IterableElementBase {
|
|
|
243
243
|
* @returns The `addMany` method returns an array of boolean values indicating whether each word in
|
|
244
244
|
* the input iterable was successfully added to the data structure.
|
|
245
245
|
*/
|
|
246
|
-
addMany(words
|
|
246
|
+
addMany(words) {
|
|
247
247
|
const ans = [];
|
|
248
248
|
for (const word of words) {
|
|
249
249
|
if (this.toElementFn) {
|
|
@@ -338,9 +338,14 @@ class Trie extends base_1.IterableElementBase {
|
|
|
338
338
|
return isDeleted;
|
|
339
339
|
}
|
|
340
340
|
/**
|
|
341
|
-
* Time Complexity: O(n)
|
|
342
|
-
* Space Complexity: O(1)
|
|
341
|
+
* Time Complexity: O(n)
|
|
342
|
+
* Space Complexity: O(1)
|
|
343
343
|
*
|
|
344
|
+
* The function `getHeight` calculates the height of a trie data structure starting from the root
|
|
345
|
+
* node.
|
|
346
|
+
* @returns The `getHeight` method returns the maximum depth or height of the trie tree starting from
|
|
347
|
+
* the root node. It calculates the depth using a breadth-first search (BFS) traversal of the trie
|
|
348
|
+
* tree and returns the maximum depth found.
|
|
344
349
|
*/
|
|
345
350
|
getHeight() {
|
|
346
351
|
const startNode = this.root;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
2
|
import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNRep, NodePredicate } from '../types';
|
|
3
|
-
export interface IBinaryTree<K = any, V = any, R = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTreeNested<K, V, R, NODE>> {
|
|
3
|
+
export interface IBinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTreeNested<K, V, R, MK, MV, MR, NODE>> {
|
|
4
4
|
createNode(key: K, value?: NODE['value']): NODE;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE;
|
|
6
6
|
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, NODE>, value?: V, count?: number): boolean;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IterableElementBase, IterableEntryBase } from '../../../data-structures';
|
|
2
|
-
export type EntryCallback<K, V, R> = (
|
|
2
|
+
export type EntryCallback<K, V, R> = (key: K, value: V, index: number, container: IterableEntryBase<K, V>) => R;
|
|
3
3
|
export type ElementCallback<E, R, RT, C> = (element: E, index: number, container: IterableElementBase<E, R, C>) => RT;
|
|
4
4
|
export type ReduceEntryCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, container: IterableEntryBase<K, V>) => R;
|
|
5
5
|
export type ReduceElementCallback<E, R, RT, C> = (accumulator: RT, element: E, index: number, container: IterableElementBase<E, R, C>) => RT;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AVLTreeMultiMap, AVLTreeMultiMapNode } from '../../../data-structures';
|
|
2
2
|
import type { AVLTreeOptions } from './avl-tree';
|
|
3
|
-
export type AVLTreeMultiMapNodeNested<K, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V,
|
|
4
|
-
export type AVLTreeMultiMapNested<K, V, R,
|
|
3
|
+
export type AVLTreeMultiMapNodeNested<K, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, any>>>;
|
|
4
|
+
export type AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE extends AVLTreeMultiMapNode<K, V, NODE>> = AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
5
5
|
export type AVLTreeMultiMapOptions<K, V, R> = AVLTreeOptions<K, V, R> & {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AVLTree, AVLTreeNode } from '../../../data-structures';
|
|
2
2
|
import { BSTOptions } from './bst';
|
|
3
|
-
export type AVLTreeNodeNested<K, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V,
|
|
4
|
-
export type AVLTreeNested<K, V, R,
|
|
3
|
+
export type AVLTreeNodeNested<K, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, any>>>;
|
|
4
|
+
export type AVLTreeNested<K, V, R, MK, MV, MR, NODE extends AVLTreeNode<K, V, NODE>> = AVLTree<K, V, R, MK, MV, MR, NODE, AVLTree<K, V, R, MK, MV, MR, NODE, AVLTree<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
5
5
|
export type AVLTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
|
|
2
2
|
import { IterationType, OptValue } from '../../common';
|
|
3
3
|
import { DFSOperation } from '../../../common';
|
|
4
|
-
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V,
|
|
5
|
-
export type BinaryTreeNested<K, V, R,
|
|
4
|
+
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>;
|
|
5
|
+
export type BinaryTreeNested<K, V, R, MK, MV, MR, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, R, MK, MV, MR, NODE, BinaryTree<K, V, R, MK, MV, MR, NODE, BinaryTree<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
6
6
|
export type ToEntryFn<K, V, R> = (rawElement: R) => BTNEntry<K, V>;
|
|
7
7
|
export type BinaryTreeOptions<K, V, R> = {
|
|
8
8
|
iterationType?: IterationType;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { BST, BSTNode } from '../../../data-structures';
|
|
2
2
|
import type { BinaryTreeOptions } from './binary-tree';
|
|
3
3
|
import { Comparable } from '../../utils';
|
|
4
|
-
export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V,
|
|
5
|
-
export type BSTNested<K, V, R,
|
|
4
|
+
export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>;
|
|
5
|
+
export type BSTNested<K, V, R, MK, MV, MR, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, MK, MV, MR, NODE, BST<K, V, R, MK, MV, MR, NODE, BST<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
6
6
|
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
|
|
7
|
-
|
|
7
|
+
specifyComparable?: (key: K) => Comparable;
|
|
8
8
|
isReverse?: boolean;
|
|
9
9
|
};
|
|
10
10
|
export type BSTNOptKey<K> = K | undefined;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures';
|
|
2
2
|
import type { BSTOptions } from "./bst";
|
|
3
3
|
export type RBTNColor = 'RED' | 'BLACK';
|
|
4
|
-
export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V,
|
|
5
|
-
export type RedBlackTreeNested<K, V, R,
|
|
6
|
-
export type
|
|
4
|
+
export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>;
|
|
5
|
+
export type RedBlackTreeNested<K, V, R, MK, MV, MR, NODE extends RedBlackTreeNode<K, V, NODE>> = RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTree<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
6
|
+
export type RedBlackTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TreeMultiMap, TreeMultiMapNode } from '../../../data-structures';
|
|
2
|
-
import type {
|
|
3
|
-
export type TreeMultiMapNodeNested<K, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V,
|
|
4
|
-
export type TreeMultiMapNested<K, V, R,
|
|
5
|
-
export type TreeMultiMapOptions<K, V, R> =
|
|
2
|
+
import type { RedBlackTreeOptions } from './rb-tree';
|
|
3
|
+
export type TreeMultiMapNodeNested<K, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, any>>>;
|
|
4
|
+
export type TreeMultiMapNested<K, V, R, MK, MV, MR, NODE extends TreeMultiMapNode<K, V, NODE>> = TreeMultiMap<K, V, R, MK, MV, MR, NODE, TreeMultiMap<K, V, R, MK, MV, MR, NODE, TreeMultiMap<K, V, R, MK, MV, MR, NODE, any>>>;
|
|
5
|
+
export type TreeMultiMapOptions<K, V, R> = RedBlackTreeOptions<K, V, R> & {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "binary-tree-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.54.0",
|
|
4
4
|
"description": "Binary Tree. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -140,6 +140,6 @@
|
|
|
140
140
|
"typescript": "^4.9.5"
|
|
141
141
|
},
|
|
142
142
|
"dependencies": {
|
|
143
|
-
"data-structure-typed": "^1.
|
|
143
|
+
"data-structure-typed": "^1.54.0"
|
|
144
144
|
}
|
|
145
145
|
}
|
package/src/common/index.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
|
+
import { isComparable } from '../utils';
|
|
2
|
+
|
|
1
3
|
export enum DFSOperation {
|
|
2
4
|
VISIT = 0,
|
|
3
5
|
PROCESS = 1
|
|
4
6
|
}
|
|
7
|
+
|
|
5
8
|
export class Range<K> {
|
|
6
9
|
constructor(
|
|
7
10
|
public low: K,
|
|
8
11
|
public high: K,
|
|
9
12
|
public includeLow: boolean = true,
|
|
10
13
|
public includeHigh: boolean = true
|
|
11
|
-
) {
|
|
14
|
+
) {
|
|
15
|
+
if (!(isComparable(low) && isComparable(high))) throw new RangeError('low or high is not comparable');
|
|
16
|
+
if (low > high) throw new RangeError('low must be less than or equal to high');
|
|
17
|
+
}
|
|
12
18
|
|
|
13
19
|
// Determine whether a key is within the range
|
|
14
20
|
isInRange(key: K, comparator: (a: K, b: K) => number): boolean {
|
|
@@ -70,7 +70,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
70
70
|
every(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
71
71
|
let index = 0;
|
|
72
72
|
for (const item of this) {
|
|
73
|
-
if (!predicate.call(thisArg, item[
|
|
73
|
+
if (!predicate.call(thisArg, item[0], item[1], index++, this)) {
|
|
74
74
|
return false;
|
|
75
75
|
}
|
|
76
76
|
}
|
|
@@ -95,7 +95,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
95
95
|
some(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
96
96
|
let index = 0;
|
|
97
97
|
for (const item of this) {
|
|
98
|
-
if (predicate.call(thisArg, item[
|
|
98
|
+
if (predicate.call(thisArg, item[0], item[1], index++, this)) {
|
|
99
99
|
return true;
|
|
100
100
|
}
|
|
101
101
|
}
|
|
@@ -119,7 +119,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
119
119
|
let index = 0;
|
|
120
120
|
for (const item of this) {
|
|
121
121
|
const [key, value] = item;
|
|
122
|
-
callbackfn.call(thisArg,
|
|
122
|
+
callbackfn.call(thisArg, key, value, index++, this);
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
|
|
@@ -144,7 +144,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
144
144
|
let index = 0;
|
|
145
145
|
for (const item of this) {
|
|
146
146
|
const [key, value] = item;
|
|
147
|
-
if (callbackfn.call(thisArg,
|
|
147
|
+
if (callbackfn.call(thisArg, key, value, index++, this)) return item;
|
|
148
148
|
}
|
|
149
149
|
return;
|
|
150
150
|
}
|
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
BinaryTreeDeleteResult,
|
|
13
13
|
BSTNOptKeyOrNode,
|
|
14
14
|
BTNRep,
|
|
15
|
+
EntryCallback,
|
|
15
16
|
IterationType
|
|
16
17
|
} from '../../types';
|
|
17
18
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -36,25 +37,6 @@ export class AVLTreeMultiMapNode<
|
|
|
36
37
|
super(key, value);
|
|
37
38
|
this.count = count;
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
-
protected _count: number = 1;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The function returns the value of the protected variable _count.
|
|
44
|
-
* @returns The count property of the object, which is of type number.
|
|
45
|
-
*/
|
|
46
|
-
get count(): number {
|
|
47
|
-
return this._count;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The above function sets the value of the count property.
|
|
52
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
53
|
-
* numeric value.
|
|
54
|
-
*/
|
|
55
|
-
set count(value: number) {
|
|
56
|
-
this._count = value;
|
|
57
|
-
}
|
|
58
40
|
}
|
|
59
41
|
|
|
60
42
|
/**
|
|
@@ -64,17 +46,23 @@ export class AVLTreeMultiMap<
|
|
|
64
46
|
K = any,
|
|
65
47
|
V = any,
|
|
66
48
|
R = object,
|
|
49
|
+
MK = any,
|
|
50
|
+
MV = any,
|
|
51
|
+
MR = object,
|
|
67
52
|
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
|
|
68
|
-
TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
|
|
53
|
+
TREE extends AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = AVLTreeMultiMap<
|
|
69
54
|
K,
|
|
70
55
|
V,
|
|
71
56
|
R,
|
|
57
|
+
MK,
|
|
58
|
+
MV,
|
|
59
|
+
MR,
|
|
72
60
|
NODE,
|
|
73
|
-
AVLTreeMultiMapNested<K, V, R, NODE>
|
|
61
|
+
AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE>
|
|
74
62
|
>
|
|
75
63
|
>
|
|
76
|
-
extends AVLTree<K, V, R, NODE, TREE>
|
|
77
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
64
|
+
extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
65
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
78
66
|
{
|
|
79
67
|
/**
|
|
80
68
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
@@ -140,10 +128,10 @@ export class AVLTreeMultiMap<
|
|
|
140
128
|
* object.
|
|
141
129
|
*/
|
|
142
130
|
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
|
|
143
|
-
return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
|
|
131
|
+
return new AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
144
132
|
iterationType: this.iterationType,
|
|
145
133
|
isMapMode: this._isMapMode,
|
|
146
|
-
|
|
134
|
+
specifyComparable: this._specifyComparable,
|
|
147
135
|
toEntryFn: this._toEntryFn,
|
|
148
136
|
isReverse: this._isReverse,
|
|
149
137
|
...options
|
|
@@ -161,44 +149,6 @@ export class AVLTreeMultiMap<
|
|
|
161
149
|
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
|
|
162
150
|
}
|
|
163
151
|
|
|
164
|
-
/**
|
|
165
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
166
|
-
* a node object.
|
|
167
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
168
|
-
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
169
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
170
|
-
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
171
|
-
* value is provided, it will default to `undefined`.
|
|
172
|
-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
173
|
-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
174
|
-
* @returns either a NODE object or undefined.
|
|
175
|
-
*/
|
|
176
|
-
override keyValueNodeEntryRawToNodeAndValue(
|
|
177
|
-
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
178
|
-
value?: V,
|
|
179
|
-
count = 1
|
|
180
|
-
): [NODE | undefined, V | undefined] {
|
|
181
|
-
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
|
|
182
|
-
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
183
|
-
|
|
184
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
185
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
186
|
-
if (key === undefined || key === null) return [undefined, undefined];
|
|
187
|
-
const finalValue = value ?? entryValue;
|
|
188
|
-
return [this.createNode(key, finalValue, count), finalValue];
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
192
|
-
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
193
|
-
const finalValue = value ?? entryValue;
|
|
194
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
198
|
-
|
|
199
|
-
return [undefined, undefined];
|
|
200
|
-
}
|
|
201
|
-
|
|
202
152
|
/**
|
|
203
153
|
* Time Complexity: O(log n)
|
|
204
154
|
* Space Complexity: O(1)
|
|
@@ -217,7 +167,7 @@ export class AVLTreeMultiMap<
|
|
|
217
167
|
* @returns a boolean value.
|
|
218
168
|
*/
|
|
219
169
|
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean {
|
|
220
|
-
const [newNode, newValue] = this.
|
|
170
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
221
171
|
if (newNode === undefined) return false;
|
|
222
172
|
|
|
223
173
|
const orgNodeCount = newNode?.count || 0;
|
|
@@ -264,7 +214,7 @@ export class AVLTreeMultiMap<
|
|
|
264
214
|
} else {
|
|
265
215
|
if (!curr.left) {
|
|
266
216
|
if (!parent) {
|
|
267
|
-
if (curr.right !== undefined) this._setRoot(curr.right);
|
|
217
|
+
if (curr.right !== undefined && curr.right !== null) this._setRoot(curr.right);
|
|
268
218
|
} else {
|
|
269
219
|
const { familyPosition: fp } = curr;
|
|
270
220
|
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
@@ -382,6 +332,75 @@ export class AVLTreeMultiMap<
|
|
|
382
332
|
return cloned;
|
|
383
333
|
}
|
|
384
334
|
|
|
335
|
+
/**
|
|
336
|
+
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
|
|
337
|
+
* with modified entries based on a provided callback.
|
|
338
|
+
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
339
|
+
* AVLTreeMultiMap. It takes four arguments:
|
|
340
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
341
|
+
* `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional
|
|
342
|
+
* configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function.
|
|
343
|
+
* These options
|
|
344
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
345
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
346
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
347
|
+
* or
|
|
348
|
+
* @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries
|
|
349
|
+
* transformed by the provided `callback` function. Each entry in the original tree is passed to the
|
|
350
|
+
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
351
|
+
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
|
|
352
|
+
*/
|
|
353
|
+
override map<MK, MV, MR>(
|
|
354
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
355
|
+
options?: AVLTreeMultiMapOptions<MK, MV, MR>,
|
|
356
|
+
thisArg?: any
|
|
357
|
+
): AVLTreeMultiMap<MK, MV, MR> {
|
|
358
|
+
const newTree = new AVLTreeMultiMap<MK, MV, MR>([], options);
|
|
359
|
+
let index = 0;
|
|
360
|
+
for (const [key, value] of this) {
|
|
361
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
362
|
+
}
|
|
363
|
+
return newTree;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
368
|
+
* a node object.
|
|
369
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
370
|
+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
371
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
372
|
+
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
373
|
+
* value is provided, it will default to `undefined`.
|
|
374
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
375
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
376
|
+
* @returns either a NODE object or undefined.
|
|
377
|
+
*/
|
|
378
|
+
protected override _keyValueNodeEntryRawToNodeAndValue(
|
|
379
|
+
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
380
|
+
value?: V,
|
|
381
|
+
count = 1
|
|
382
|
+
): [NODE | undefined, V | undefined] {
|
|
383
|
+
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
|
|
384
|
+
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
385
|
+
|
|
386
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
387
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
388
|
+
if (key === undefined || key === null) return [undefined, undefined];
|
|
389
|
+
const finalValue = value ?? entryValue;
|
|
390
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
394
|
+
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
395
|
+
const finalValue = value ?? entryValue;
|
|
396
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
400
|
+
|
|
401
|
+
return [undefined, undefined];
|
|
402
|
+
}
|
|
403
|
+
|
|
385
404
|
/**
|
|
386
405
|
* Time Complexity: O(1)
|
|
387
406
|
* Space Complexity: O(1)
|
|
@@ -12,7 +12,8 @@ import type {
|
|
|
12
12
|
AVLTreeOptions,
|
|
13
13
|
BinaryTreeDeleteResult,
|
|
14
14
|
BSTNOptKeyOrNode,
|
|
15
|
-
BTNRep
|
|
15
|
+
BTNRep,
|
|
16
|
+
EntryCallback
|
|
16
17
|
} from '../../types';
|
|
17
18
|
import { IBinaryTree } from '../../interfaces';
|
|
18
19
|
|
|
@@ -31,26 +32,6 @@ export class AVLTreeNode<
|
|
|
31
32
|
*/
|
|
32
33
|
constructor(key: K, value?: V) {
|
|
33
34
|
super(key, value);
|
|
34
|
-
this._height = 0;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
protected _height: number;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The function returns the value of the height property.
|
|
41
|
-
* @returns The height of the object.
|
|
42
|
-
*/
|
|
43
|
-
get height(): number {
|
|
44
|
-
return this._height;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* The above function sets the value of the height property.
|
|
49
|
-
* @param {number} value - The value parameter is a number that represents the new height value to be
|
|
50
|
-
* set.
|
|
51
|
-
*/
|
|
52
|
-
set height(value: number) {
|
|
53
|
-
this._height = value;
|
|
54
35
|
}
|
|
55
36
|
}
|
|
56
37
|
|
|
@@ -67,11 +48,23 @@ export class AVLTree<
|
|
|
67
48
|
K = any,
|
|
68
49
|
V = any,
|
|
69
50
|
R = object,
|
|
51
|
+
MK = any,
|
|
52
|
+
MV = any,
|
|
53
|
+
MR = object,
|
|
70
54
|
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
71
|
-
TREE extends AVLTree<K, V, R,
|
|
55
|
+
TREE extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> = AVLTree<
|
|
56
|
+
K,
|
|
57
|
+
V,
|
|
58
|
+
R,
|
|
59
|
+
MK,
|
|
60
|
+
MV,
|
|
61
|
+
MR,
|
|
62
|
+
NODE,
|
|
63
|
+
AVLTreeNested<K, V, R, MK, MV, MR, NODE>
|
|
64
|
+
>
|
|
72
65
|
>
|
|
73
|
-
extends BST<K, V, R, NODE, TREE>
|
|
74
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
66
|
+
extends BST<K, V, R, MK, MV, MR, NODE, TREE>
|
|
67
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
75
68
|
{
|
|
76
69
|
/**
|
|
77
70
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
@@ -110,10 +103,10 @@ export class AVLTree<
|
|
|
110
103
|
* @returns a new AVLTree object.
|
|
111
104
|
*/
|
|
112
105
|
override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
|
|
113
|
-
return new AVLTree<K, V, R, NODE, TREE>([], {
|
|
106
|
+
return new AVLTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
114
107
|
iterationType: this.iterationType,
|
|
115
108
|
isMapMode: this._isMapMode,
|
|
116
|
-
|
|
109
|
+
specifyComparable: this._specifyComparable,
|
|
117
110
|
toEntryFn: this._toEntryFn,
|
|
118
111
|
isReverse: this._isReverse,
|
|
119
112
|
...options
|
|
@@ -174,6 +167,19 @@ export class AVLTree<
|
|
|
174
167
|
return deletedResults;
|
|
175
168
|
}
|
|
176
169
|
|
|
170
|
+
override map(
|
|
171
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
172
|
+
options?: AVLTreeOptions<MK, MV, MR>,
|
|
173
|
+
thisArg?: any
|
|
174
|
+
): AVLTree<MK, MV, MR> {
|
|
175
|
+
const newTree = new AVLTree<MK, MV, MR>([], options);
|
|
176
|
+
let index = 0;
|
|
177
|
+
for (const [key, value] of this) {
|
|
178
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
179
|
+
}
|
|
180
|
+
return newTree;
|
|
181
|
+
}
|
|
182
|
+
|
|
177
183
|
/**
|
|
178
184
|
* Time Complexity: O(1)
|
|
179
185
|
* Space Complexity: O(1)
|
|
@@ -262,7 +268,7 @@ export class AVLTree<
|
|
|
262
268
|
protected _balanceLL(A: NODE): void {
|
|
263
269
|
const parentOfA = A.parent;
|
|
264
270
|
const B = A.left;
|
|
265
|
-
A.parent = B;
|
|
271
|
+
if (B !== null) A.parent = B;
|
|
266
272
|
if (B && B.right) {
|
|
267
273
|
B.right.parent = A;
|
|
268
274
|
}
|
|
@@ -299,12 +305,12 @@ export class AVLTree<
|
|
|
299
305
|
if (B) {
|
|
300
306
|
C = B.right;
|
|
301
307
|
}
|
|
302
|
-
if (A) A.parent = C;
|
|
303
|
-
if (B) B.parent = C;
|
|
308
|
+
if (A && C !== null) A.parent = C;
|
|
309
|
+
if (B && C !== null) B.parent = C;
|
|
304
310
|
|
|
305
311
|
if (C) {
|
|
306
312
|
if (C.left) {
|
|
307
|
-
C.left.parent = B;
|
|
313
|
+
if (B !== null) C.left.parent = B;
|
|
308
314
|
}
|
|
309
315
|
if (C.right) {
|
|
310
316
|
C.right.parent = A;
|
|
@@ -346,7 +352,7 @@ export class AVLTree<
|
|
|
346
352
|
protected _balanceRR(A: NODE): void {
|
|
347
353
|
const parentOfA = A.parent;
|
|
348
354
|
const B = A.right;
|
|
349
|
-
A.parent = B;
|
|
355
|
+
if (B !== null) A.parent = B;
|
|
350
356
|
if (B) {
|
|
351
357
|
if (B.left) {
|
|
352
358
|
B.left.parent = A;
|
|
@@ -389,15 +395,15 @@ export class AVLTree<
|
|
|
389
395
|
C = B.left;
|
|
390
396
|
}
|
|
391
397
|
|
|
392
|
-
A.parent = C;
|
|
393
|
-
if (B) B.parent = C;
|
|
398
|
+
if (C !== null) A.parent = C;
|
|
399
|
+
if (B && C !== null) B.parent = C;
|
|
394
400
|
|
|
395
401
|
if (C) {
|
|
396
402
|
if (C.left) {
|
|
397
403
|
C.left.parent = A;
|
|
398
404
|
}
|
|
399
405
|
if (C.right) {
|
|
400
|
-
C.right.parent = B;
|
|
406
|
+
if (B !== null) C.right.parent = B;
|
|
401
407
|
}
|
|
402
408
|
C.parent = parentOfA;
|
|
403
409
|
}
|