max-heap-typed 2.1.2 → 2.2.1
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/cjs/index.cjs +56 -53
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +998 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +56 -53
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +991 -0
- package/dist/esm-legacy/index.mjs.map +1 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +65 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +61 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +58 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +59 -4
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +66 -3
- package/dist/types/types/data-structures/base/base.d.ts +1 -1
- package/package.json +20 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +103 -12
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +116 -12
- package/src/data-structures/binary-tree/avl-tree.ts +109 -16
- package/src/data-structures/binary-tree/binary-tree.ts +3 -2
- package/src/data-structures/binary-tree/bst.ts +104 -12
- package/src/data-structures/binary-tree/red-black-tree.ts +110 -19
- package/src/data-structures/binary-tree/tree-counter.ts +102 -11
- package/src/data-structures/binary-tree/tree-multi-map.ts +124 -12
- package/src/data-structures/graph/abstract-graph.ts +8 -8
- package/src/data-structures/graph/directed-graph.ts +5 -5
- package/src/data-structures/graph/undirected-graph.ts +5 -5
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/types/data-structures/base/base.ts +1 -1
- package/tsup.node.config.js +40 -6
|
@@ -5,10 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BinaryTreeOptions, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
9
|
-
import { BST
|
|
8
|
+
import type { BinaryTreeDeleteResult, BinaryTreeOptions, CRUD, EntryCallback, FamilyPosition, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
9
|
+
import { BST } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
export declare class RedBlackTreeNode<K = any, V = any>
|
|
11
|
+
export declare class RedBlackTreeNode<K = any, V = any> {
|
|
12
|
+
key: K;
|
|
13
|
+
value?: V;
|
|
12
14
|
parent?: RedBlackTreeNode<K, V>;
|
|
13
15
|
/**
|
|
14
16
|
* Create a Red-Black Tree and optionally bulk-insert items.
|
|
@@ -47,6 +49,58 @@ export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
47
49
|
* @returns void
|
|
48
50
|
*/
|
|
49
51
|
set right(v: RedBlackTreeNode<K, V> | null | undefined);
|
|
52
|
+
_height: number;
|
|
53
|
+
/**
|
|
54
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
55
|
+
* @remarks Time O(1), Space O(1)
|
|
56
|
+
*
|
|
57
|
+
* @returns The height.
|
|
58
|
+
*/
|
|
59
|
+
get height(): number;
|
|
60
|
+
/**
|
|
61
|
+
* Sets the height of the node.
|
|
62
|
+
* @remarks Time O(1), Space O(1)
|
|
63
|
+
*
|
|
64
|
+
* @param value - The new height.
|
|
65
|
+
*/
|
|
66
|
+
set height(value: number);
|
|
67
|
+
_color: RBTNColor;
|
|
68
|
+
/**
|
|
69
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
70
|
+
* @remarks Time O(1), Space O(1)
|
|
71
|
+
*
|
|
72
|
+
* @returns The node's color.
|
|
73
|
+
*/
|
|
74
|
+
get color(): RBTNColor;
|
|
75
|
+
/**
|
|
76
|
+
* Sets the color of the node.
|
|
77
|
+
* @remarks Time O(1), Space O(1)
|
|
78
|
+
*
|
|
79
|
+
* @param value - The new color.
|
|
80
|
+
*/
|
|
81
|
+
set color(value: RBTNColor);
|
|
82
|
+
_count: number;
|
|
83
|
+
/**
|
|
84
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
85
|
+
* @remarks Time O(1), Space O(1)
|
|
86
|
+
*
|
|
87
|
+
* @returns The subtree node count.
|
|
88
|
+
*/
|
|
89
|
+
get count(): number;
|
|
90
|
+
/**
|
|
91
|
+
* Sets the count of nodes in the subtree.
|
|
92
|
+
* @remarks Time O(1), Space O(1)
|
|
93
|
+
*
|
|
94
|
+
* @param value - The new count.
|
|
95
|
+
*/
|
|
96
|
+
set count(value: number);
|
|
97
|
+
/**
|
|
98
|
+
* Gets the position of the node relative to its parent.
|
|
99
|
+
* @remarks Time O(1), Space O(1)
|
|
100
|
+
*
|
|
101
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
102
|
+
*/
|
|
103
|
+
get familyPosition(): FamilyPosition;
|
|
50
104
|
}
|
|
51
105
|
/**
|
|
52
106
|
* Represents a Red-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
|
|
@@ -54,8 +108,9 @@ export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
54
108
|
* @template K
|
|
55
109
|
* @template V
|
|
56
110
|
* @template R
|
|
57
|
-
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
|
|
111
|
+
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high, but the query efficiency is slightly lower.
|
|
58
112
|
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
113
|
+
*
|
|
59
114
|
* @example
|
|
60
115
|
* // using Red-Black Tree as a price-based index for stock data
|
|
61
116
|
* // Define the structure of individual stock records
|
|
@@ -5,18 +5,20 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
|
|
9
9
|
import { BSTOptions } from '../../types';
|
|
10
10
|
import { BSTNode } from './bst';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
|
-
import { RedBlackTree
|
|
12
|
+
import { RedBlackTree } from './red-black-tree';
|
|
13
13
|
/**
|
|
14
14
|
* RB-tree node with an extra 'count' field; keeps parent/child links.
|
|
15
15
|
* @remarks Time O(1), Space O(1)
|
|
16
16
|
* @template K
|
|
17
17
|
* @template V
|
|
18
18
|
*/
|
|
19
|
-
export declare class TreeCounterNode<K = any, V = any>
|
|
19
|
+
export declare class TreeCounterNode<K = any, V = any> {
|
|
20
|
+
key: K;
|
|
21
|
+
value?: V;
|
|
20
22
|
parent?: TreeCounterNode<K, V>;
|
|
21
23
|
/**
|
|
22
24
|
* Create a tree counter node.
|
|
@@ -56,6 +58,58 @@ export declare class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<
|
|
|
56
58
|
* @returns void
|
|
57
59
|
*/
|
|
58
60
|
set right(v: TreeCounterNode<K, V> | null | undefined);
|
|
61
|
+
_height: number;
|
|
62
|
+
/**
|
|
63
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
64
|
+
* @remarks Time O(1), Space O(1)
|
|
65
|
+
*
|
|
66
|
+
* @returns The height.
|
|
67
|
+
*/
|
|
68
|
+
get height(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Sets the height of the node.
|
|
71
|
+
* @remarks Time O(1), Space O(1)
|
|
72
|
+
*
|
|
73
|
+
* @param value - The new height.
|
|
74
|
+
*/
|
|
75
|
+
set height(value: number);
|
|
76
|
+
_color: RBTNColor;
|
|
77
|
+
/**
|
|
78
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
79
|
+
* @remarks Time O(1), Space O(1)
|
|
80
|
+
*
|
|
81
|
+
* @returns The node's color.
|
|
82
|
+
*/
|
|
83
|
+
get color(): RBTNColor;
|
|
84
|
+
/**
|
|
85
|
+
* Sets the color of the node.
|
|
86
|
+
* @remarks Time O(1), Space O(1)
|
|
87
|
+
*
|
|
88
|
+
* @param value - The new color.
|
|
89
|
+
*/
|
|
90
|
+
set color(value: RBTNColor);
|
|
91
|
+
_count: number;
|
|
92
|
+
/**
|
|
93
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
94
|
+
* @remarks Time O(1), Space O(1)
|
|
95
|
+
*
|
|
96
|
+
* @returns The subtree node count.
|
|
97
|
+
*/
|
|
98
|
+
get count(): number;
|
|
99
|
+
/**
|
|
100
|
+
* Sets the count of nodes in the subtree.
|
|
101
|
+
* @remarks Time O(1), Space O(1)
|
|
102
|
+
*
|
|
103
|
+
* @param value - The new count.
|
|
104
|
+
*/
|
|
105
|
+
set count(value: number);
|
|
106
|
+
/**
|
|
107
|
+
* Gets the position of the node relative to its parent.
|
|
108
|
+
* @remarks Time O(1), Space O(1)
|
|
109
|
+
*
|
|
110
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
111
|
+
*/
|
|
112
|
+
get familyPosition(): FamilyPosition;
|
|
59
113
|
}
|
|
60
114
|
/**
|
|
61
115
|
* Red-Black Tree–based counter map (key → value with per-node count). Supports O(log N) updates and map-like mode.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { ElemOf, EntryCallback, RedBlackTreeOptions, TreeMultiMapOptions } from '../../types';
|
|
8
|
+
import type { ElemOf, EntryCallback, FamilyPosition, RBTNColor, RedBlackTreeOptions, TreeMultiMapOptions } from '../../types';
|
|
9
9
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -14,7 +14,9 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
14
14
|
* @template K
|
|
15
15
|
* @template V
|
|
16
16
|
*/
|
|
17
|
-
export declare class TreeMultiMapNode<K = any, V = any>
|
|
17
|
+
export declare class TreeMultiMapNode<K = any, V = any> {
|
|
18
|
+
key: K;
|
|
19
|
+
value?: V[];
|
|
18
20
|
parent?: TreeMultiMapNode<K, V>;
|
|
19
21
|
/**
|
|
20
22
|
* Create a TreeMultiMap node with an optional value bucket.
|
|
@@ -23,7 +25,7 @@ export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode
|
|
|
23
25
|
* @param [value] - Initial array of values.
|
|
24
26
|
* @returns New TreeMultiMapNode instance.
|
|
25
27
|
*/
|
|
26
|
-
constructor(key: K, value?: V[]);
|
|
28
|
+
constructor(key: K, value?: V[], color?: RBTNColor);
|
|
27
29
|
_left?: TreeMultiMapNode<K, V> | null | undefined;
|
|
28
30
|
/**
|
|
29
31
|
* Get the left child pointer.
|
|
@@ -52,6 +54,58 @@ export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode
|
|
|
52
54
|
* @returns void
|
|
53
55
|
*/
|
|
54
56
|
set right(v: TreeMultiMapNode<K, V> | null | undefined);
|
|
57
|
+
_height: number;
|
|
58
|
+
/**
|
|
59
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
60
|
+
* @remarks Time O(1), Space O(1)
|
|
61
|
+
*
|
|
62
|
+
* @returns The height.
|
|
63
|
+
*/
|
|
64
|
+
get height(): number;
|
|
65
|
+
/**
|
|
66
|
+
* Sets the height of the node.
|
|
67
|
+
* @remarks Time O(1), Space O(1)
|
|
68
|
+
*
|
|
69
|
+
* @param value - The new height.
|
|
70
|
+
*/
|
|
71
|
+
set height(value: number);
|
|
72
|
+
_color: RBTNColor;
|
|
73
|
+
/**
|
|
74
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
75
|
+
* @remarks Time O(1), Space O(1)
|
|
76
|
+
*
|
|
77
|
+
* @returns The node's color.
|
|
78
|
+
*/
|
|
79
|
+
get color(): RBTNColor;
|
|
80
|
+
/**
|
|
81
|
+
* Sets the color of the node.
|
|
82
|
+
* @remarks Time O(1), Space O(1)
|
|
83
|
+
*
|
|
84
|
+
* @param value - The new color.
|
|
85
|
+
*/
|
|
86
|
+
set color(value: RBTNColor);
|
|
87
|
+
_count: number;
|
|
88
|
+
/**
|
|
89
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
90
|
+
* @remarks Time O(1), Space O(1)
|
|
91
|
+
*
|
|
92
|
+
* @returns The subtree node count.
|
|
93
|
+
*/
|
|
94
|
+
get count(): number;
|
|
95
|
+
/**
|
|
96
|
+
* Sets the count of nodes in the subtree.
|
|
97
|
+
* @remarks Time O(1), Space O(1)
|
|
98
|
+
*
|
|
99
|
+
* @param value - The new count.
|
|
100
|
+
*/
|
|
101
|
+
set count(value: number);
|
|
102
|
+
/**
|
|
103
|
+
* Gets the position of the node relative to its parent.
|
|
104
|
+
* @remarks Time O(1), Space O(1)
|
|
105
|
+
*
|
|
106
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
107
|
+
*/
|
|
108
|
+
get familyPosition(): FamilyPosition;
|
|
55
109
|
}
|
|
56
110
|
/**
|
|
57
111
|
* Red-Black Tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
|
|
@@ -59,6 +113,7 @@ export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode
|
|
|
59
113
|
* @template K
|
|
60
114
|
* @template V
|
|
61
115
|
* @template R
|
|
116
|
+
*
|
|
62
117
|
* @example
|
|
63
118
|
* // players ranked by score with their equipment
|
|
64
119
|
* type Equipment = {
|
|
@@ -234,6 +289,14 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
234
289
|
*/
|
|
235
290
|
constructor(keysNodesEntriesOrRaws?: Iterable<K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: TreeMultiMapOptions<K, V[], R>);
|
|
236
291
|
createNode(key: K, value?: V[]): TreeMultiMapNode<K, V>;
|
|
292
|
+
/**
|
|
293
|
+
* Checks if the given item is a `TreeMultiMapNode` instance.
|
|
294
|
+
* @remarks Time O(1), Space O(1)
|
|
295
|
+
*
|
|
296
|
+
* @param keyNodeOrEntry - The item to check.
|
|
297
|
+
* @returns True if it's a TreeMultiMapNode, false otherwise.
|
|
298
|
+
*/
|
|
299
|
+
isNode(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): keyNodeOrEntry is TreeMultiMapNode<K, V>;
|
|
237
300
|
add(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
|
|
238
301
|
add(key: K, value: V): boolean;
|
|
239
302
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IterableElementBase, IterableEntryBase } from '../../../data-structures';
|
|
2
2
|
import { LinearBase } from '../../../data-structures/base/linear-base';
|
|
3
|
-
export type EntryCallback<K, V, R> = (
|
|
3
|
+
export type EntryCallback<K, V, R> = (value: V, key: K, index: number, original: IterableEntryBase<K, V>) => R;
|
|
4
4
|
export type ElementCallback<E, R, RT> = (element: E, index: number, original: IterableElementBase<E, R>) => RT;
|
|
5
5
|
export type ReduceEntryCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, original: IterableEntryBase<K, V>) => R;
|
|
6
6
|
export type ReduceElementCallback<E, R, U = E> = (accumulator: U, value: E, index: number, self: IterableElementBase<E, R>) => U;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "max-heap-typed",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "Max Heap",
|
|
5
5
|
"browser": "dist/umd/max-heap-typed.min.js",
|
|
6
6
|
"umd:main": "dist/umd/max-heap-typed.min.js",
|
|
@@ -9,9 +9,27 @@
|
|
|
9
9
|
"types": "dist/types/index.d.ts",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
+
"types": "./dist/types/index.d.ts",
|
|
13
|
+
"node": {
|
|
14
|
+
"import": "./dist/esm/index.mjs",
|
|
15
|
+
"require": "./dist/cjs/index.cjs"
|
|
16
|
+
},
|
|
17
|
+
"browser": {
|
|
18
|
+
"import": "./dist/esm-legacy/index.mjs",
|
|
19
|
+
"require": "./dist/cjs-legacy/index.cjs"
|
|
20
|
+
},
|
|
21
|
+
"import": "./dist/esm-legacy/index.mjs",
|
|
22
|
+
"require": "./dist/cjs-legacy/index.cjs"
|
|
23
|
+
},
|
|
24
|
+
"./modern": {
|
|
12
25
|
"types": "./dist/types/index.d.ts",
|
|
13
26
|
"import": "./dist/esm/index.mjs",
|
|
14
27
|
"require": "./dist/cjs/index.cjs"
|
|
28
|
+
},
|
|
29
|
+
"./legacy": {
|
|
30
|
+
"types": "./dist/types/index.d.ts",
|
|
31
|
+
"import": "./dist/esm-legacy/index.mjs",
|
|
32
|
+
"require": "./dist/cjs-legacy/index.cjs"
|
|
15
33
|
}
|
|
16
34
|
},
|
|
17
35
|
"sideEffects": false,
|
|
@@ -152,6 +170,6 @@
|
|
|
152
170
|
"typescript": "^4.9.5"
|
|
153
171
|
},
|
|
154
172
|
"dependencies": {
|
|
155
|
-
"data-structure-typed": "^2.1
|
|
173
|
+
"data-structure-typed": "^2.2.1"
|
|
156
174
|
}
|
|
157
175
|
}
|
|
@@ -66,7 +66,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
66
66
|
every(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
67
67
|
let index = 0;
|
|
68
68
|
for (const item of this) {
|
|
69
|
-
if (!predicate.call(thisArg, item[
|
|
69
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
70
70
|
return false;
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -83,7 +83,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
83
83
|
some(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
84
84
|
let index = 0;
|
|
85
85
|
for (const item of this) {
|
|
86
|
-
if (predicate.call(thisArg, item[
|
|
86
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
87
87
|
return true;
|
|
88
88
|
}
|
|
89
89
|
}
|
|
@@ -100,7 +100,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
100
100
|
let index = 0;
|
|
101
101
|
for (const item of this) {
|
|
102
102
|
const [key, value] = item;
|
|
103
|
-
callbackfn.call(thisArg,
|
|
103
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
|
|
@@ -115,7 +115,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
115
115
|
let index = 0;
|
|
116
116
|
for (const item of this) {
|
|
117
117
|
const [key, value] = item;
|
|
118
|
-
if (callbackfn.call(thisArg,
|
|
118
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
119
119
|
}
|
|
120
120
|
return;
|
|
121
121
|
}
|
|
@@ -12,10 +12,12 @@ import type {
|
|
|
12
12
|
BinaryTreeOptions,
|
|
13
13
|
BSTNOptKeyOrNode,
|
|
14
14
|
EntryCallback,
|
|
15
|
-
|
|
15
|
+
FamilyPosition,
|
|
16
|
+
IterationType,
|
|
17
|
+
RBTNColor
|
|
16
18
|
} from '../../types';
|
|
17
19
|
import { IBinaryTree } from '../../interfaces';
|
|
18
|
-
import { AVLTree
|
|
20
|
+
import { AVLTree } from './avl-tree';
|
|
19
21
|
|
|
20
22
|
/**
|
|
21
23
|
* AVL node with an extra 'count' field; keeps parent/child links.
|
|
@@ -23,8 +25,10 @@ import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
|
23
25
|
* @template K
|
|
24
26
|
* @template V
|
|
25
27
|
*/
|
|
26
|
-
export class AVLTreeCounterNode<K = any, V = any>
|
|
27
|
-
|
|
28
|
+
export class AVLTreeCounterNode<K = any, V = any> {
|
|
29
|
+
key: K;
|
|
30
|
+
value?: V;
|
|
31
|
+
parent?: AVLTreeCounterNode<K, V> = undefined;
|
|
28
32
|
|
|
29
33
|
/**
|
|
30
34
|
* Create an AVL counter node.
|
|
@@ -35,18 +39,19 @@ export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
|
|
|
35
39
|
* @returns New AVLTreeCounterNode instance.
|
|
36
40
|
*/
|
|
37
41
|
constructor(key: K, value?: V, count = 1) {
|
|
38
|
-
|
|
42
|
+
this.key = key;
|
|
43
|
+
this.value = value;
|
|
39
44
|
this.count = count;
|
|
40
45
|
}
|
|
41
46
|
|
|
42
|
-
|
|
47
|
+
_left?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
|
|
43
48
|
|
|
44
49
|
/**
|
|
45
50
|
* Get the left child pointer.
|
|
46
51
|
* @remarks Time O(1), Space O(1)
|
|
47
52
|
* @returns Left child node, or null/undefined.
|
|
48
53
|
*/
|
|
49
|
-
|
|
54
|
+
get left(): AVLTreeCounterNode<K, V> | null | undefined {
|
|
50
55
|
return this._left;
|
|
51
56
|
}
|
|
52
57
|
|
|
@@ -56,21 +61,21 @@ export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
|
|
|
56
61
|
* @param v - New left child node, or null/undefined.
|
|
57
62
|
* @returns void
|
|
58
63
|
*/
|
|
59
|
-
|
|
64
|
+
set left(v: AVLTreeCounterNode<K, V> | null | undefined) {
|
|
60
65
|
if (v) {
|
|
61
66
|
v.parent = this;
|
|
62
67
|
}
|
|
63
68
|
this._left = v;
|
|
64
69
|
}
|
|
65
70
|
|
|
66
|
-
|
|
71
|
+
_right?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
|
|
67
72
|
|
|
68
73
|
/**
|
|
69
74
|
* Get the right child pointer.
|
|
70
75
|
* @remarks Time O(1), Space O(1)
|
|
71
76
|
* @returns Right child node, or null/undefined.
|
|
72
77
|
*/
|
|
73
|
-
|
|
78
|
+
get right(): AVLTreeCounterNode<K, V> | null | undefined {
|
|
74
79
|
return this._right;
|
|
75
80
|
}
|
|
76
81
|
|
|
@@ -80,12 +85,98 @@ export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
|
|
|
80
85
|
* @param v - New right child node, or null/undefined.
|
|
81
86
|
* @returns void
|
|
82
87
|
*/
|
|
83
|
-
|
|
88
|
+
set right(v: AVLTreeCounterNode<K, V> | null | undefined) {
|
|
84
89
|
if (v) {
|
|
85
90
|
v.parent = this;
|
|
86
91
|
}
|
|
87
92
|
this._right = v;
|
|
88
93
|
}
|
|
94
|
+
|
|
95
|
+
_height: number = 0;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
99
|
+
* @remarks Time O(1), Space O(1)
|
|
100
|
+
*
|
|
101
|
+
* @returns The height.
|
|
102
|
+
*/
|
|
103
|
+
get height(): number {
|
|
104
|
+
return this._height;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Sets the height of the node.
|
|
109
|
+
* @remarks Time O(1), Space O(1)
|
|
110
|
+
*
|
|
111
|
+
* @param value - The new height.
|
|
112
|
+
*/
|
|
113
|
+
set height(value: number) {
|
|
114
|
+
this._height = value;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
_color: RBTNColor = 'BLACK';
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
121
|
+
* @remarks Time O(1), Space O(1)
|
|
122
|
+
*
|
|
123
|
+
* @returns The node's color.
|
|
124
|
+
*/
|
|
125
|
+
get color(): RBTNColor {
|
|
126
|
+
return this._color;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Sets the color of the node.
|
|
131
|
+
* @remarks Time O(1), Space O(1)
|
|
132
|
+
*
|
|
133
|
+
* @param value - The new color.
|
|
134
|
+
*/
|
|
135
|
+
set color(value: RBTNColor) {
|
|
136
|
+
this._color = value;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
_count: number = 1;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
143
|
+
* @remarks Time O(1), Space O(1)
|
|
144
|
+
*
|
|
145
|
+
* @returns The subtree node count.
|
|
146
|
+
*/
|
|
147
|
+
get count(): number {
|
|
148
|
+
return this._count;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Sets the count of nodes in the subtree.
|
|
153
|
+
* @remarks Time O(1), Space O(1)
|
|
154
|
+
*
|
|
155
|
+
* @param value - The new count.
|
|
156
|
+
*/
|
|
157
|
+
set count(value: number) {
|
|
158
|
+
this._count = value;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Gets the position of the node relative to its parent.
|
|
163
|
+
* @remarks Time O(1), Space O(1)
|
|
164
|
+
*
|
|
165
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
166
|
+
*/
|
|
167
|
+
get familyPosition(): FamilyPosition {
|
|
168
|
+
if (!this.parent) {
|
|
169
|
+
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (this.parent.left === this) {
|
|
173
|
+
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
174
|
+
} else if (this.parent.right === this) {
|
|
175
|
+
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return 'MAL_NODE';
|
|
179
|
+
}
|
|
89
180
|
}
|
|
90
181
|
|
|
91
182
|
/**
|
|
@@ -320,7 +411,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
320
411
|
|
|
321
412
|
let index = 0;
|
|
322
413
|
for (const [key, value] of this) {
|
|
323
|
-
out.add(callback.call(thisArg,
|
|
414
|
+
out.add(callback.call(thisArg, value, key, index++, this));
|
|
324
415
|
}
|
|
325
416
|
return out;
|
|
326
417
|
}
|