linked-list-typed 1.39.2 → 1.39.4
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.d.ts +11 -11
- package/dist/data-structures/binary-tree/avl-tree.js +8 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +50 -117
- package/dist/data-structures/binary-tree/binary-tree.js +43 -51
- package/dist/data-structures/binary-tree/bst.d.ts +23 -23
- package/dist/data-structures/binary-tree/bst.js +18 -18
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +15 -15
- package/dist/data-structures/binary-tree/tree-multiset.js +9 -9
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/helpers.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +16 -14
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +145 -84
- package/src/data-structures/binary-tree/bst.ts +37 -38
- package/src/data-structures/binary-tree/rb-tree.ts +7 -6
- package/src/data-structures/binary-tree/tree-multiset.ts +18 -17
- package/src/data-structures/graph/abstract-graph.ts +11 -10
- package/src/data-structures/graph/directed-graph.ts +2 -1
- package/src/data-structures/graph/undirected-graph.ts +5 -4
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- package/src/data-structures/heap/heap.ts +2 -2
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +1 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +4 -5
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +4 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -2
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/helpers.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
import { BinaryTreeDeletedResult, IterationType,
|
|
8
|
+
import type { BTNKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, IterationType, BTNCallback } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
13
13
|
count: number;
|
|
14
14
|
/**
|
|
15
15
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
16
|
-
* @param {
|
|
16
|
+
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
17
17
|
* of the binary tree node.
|
|
18
18
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
19
19
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -21,7 +21,7 @@ export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N>
|
|
|
21
21
|
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
22
22
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
23
23
|
*/
|
|
24
|
-
constructor(key:
|
|
24
|
+
constructor(key: BTNKey, val?: V, count?: number);
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
@@ -38,19 +38,19 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
38
38
|
get count(): number;
|
|
39
39
|
/**
|
|
40
40
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
41
|
-
* @param {
|
|
41
|
+
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
42
42
|
* distinguish one node from another in the tree.
|
|
43
43
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
44
44
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
45
45
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
46
46
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
47
47
|
*/
|
|
48
|
-
createNode(key:
|
|
48
|
+
createNode(key: BTNKey, val?: V, count?: number): N;
|
|
49
49
|
/**
|
|
50
50
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
51
51
|
* exists, and balancing the tree if necessary.
|
|
52
|
-
* @param {
|
|
53
|
-
* `
|
|
52
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
53
|
+
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
54
54
|
* node to be added), or `null` (which represents a null node).
|
|
55
55
|
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
56
56
|
* added to the binary tree.
|
|
@@ -59,7 +59,7 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
59
59
|
* count is specified, the default count will be 1.
|
|
60
60
|
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
61
61
|
*/
|
|
62
|
-
add(keyOrNode:
|
|
62
|
+
add(keyOrNode: BTNKey | N | null, val?: V, count?: number): N | null | undefined;
|
|
63
63
|
/**
|
|
64
64
|
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
65
65
|
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
|
|
@@ -72,14 +72,14 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
72
72
|
/**
|
|
73
73
|
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
74
74
|
* inserted nodes.
|
|
75
|
-
* @param {(
|
|
76
|
-
* added to the multiset. Each element can be either a
|
|
75
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
76
|
+
* added to the multiset. Each element can be either a BTNKey or a TreeMultisetNode.
|
|
77
77
|
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
78
78
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
79
79
|
* each key or node.
|
|
80
80
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
81
81
|
*/
|
|
82
|
-
addMany(keysOrNodes: (
|
|
82
|
+
addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[];
|
|
83
83
|
/**
|
|
84
84
|
* The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
|
|
85
85
|
* binary search tree using either a recursive or iterative approach.
|
|
@@ -93,19 +93,19 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
|
|
|
93
93
|
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
94
94
|
* node along with the parent node that needs to be balanced.
|
|
95
95
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
96
|
-
* `
|
|
96
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
97
97
|
* searching for. It can be a specific key value or any other property of the node.
|
|
98
98
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
99
99
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
100
100
|
* included in the result. The `callback` parameter has a default value of
|
|
101
|
-
* `
|
|
101
|
+
* `((node: N) => node.key)`
|
|
102
102
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
103
103
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
104
104
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
105
105
|
* decremented by 1 and
|
|
106
106
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
107
107
|
*/
|
|
108
|
-
delete<C extends
|
|
108
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
109
109
|
/**
|
|
110
110
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
111
111
|
*/
|
|
@@ -6,7 +6,7 @@ const avl_tree_1 = require("./avl-tree");
|
|
|
6
6
|
class TreeMultisetNode extends avl_tree_1.AVLTreeNode {
|
|
7
7
|
/**
|
|
8
8
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
9
|
-
* @param {
|
|
9
|
+
* @param {BTNKey} key - The `key` parameter is of type `BTNKey` and represents the unique identifier
|
|
10
10
|
* of the binary tree node.
|
|
11
11
|
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
12
12
|
* tree node. If no value is provided, it will be `undefined`.
|
|
@@ -39,7 +39,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
41
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
42
|
-
* @param {
|
|
42
|
+
* @param {BTNKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
43
43
|
* distinguish one node from another in the tree.
|
|
44
44
|
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
45
45
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
@@ -52,8 +52,8 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
52
52
|
/**
|
|
53
53
|
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
54
54
|
* exists, and balancing the tree if necessary.
|
|
55
|
-
* @param {
|
|
56
|
-
* `
|
|
55
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
56
|
+
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
57
57
|
* node to be added), or `null` (which represents a null node).
|
|
58
58
|
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
59
59
|
* added to the binary tree.
|
|
@@ -175,8 +175,8 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
175
175
|
/**
|
|
176
176
|
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
177
177
|
* inserted nodes.
|
|
178
|
-
* @param {(
|
|
179
|
-
* added to the multiset. Each element can be either a
|
|
178
|
+
* @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
179
|
+
* added to the multiset. Each element can be either a BTNKey or a TreeMultisetNode.
|
|
180
180
|
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
181
181
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
182
182
|
* each key or node.
|
|
@@ -246,19 +246,19 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
246
246
|
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
247
247
|
* node along with the parent node that needs to be balanced.
|
|
248
248
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
249
|
-
* `
|
|
249
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
250
250
|
* searching for. It can be a specific key value or any other property of the node.
|
|
251
251
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
252
252
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
253
253
|
* included in the result. The `callback` parameter has a default value of
|
|
254
|
-
* `
|
|
254
|
+
* `((node: N) => node.key)`
|
|
255
255
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
256
256
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
257
257
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
258
258
|
* decremented by 1 and
|
|
259
259
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
260
260
|
*/
|
|
261
|
-
delete(identifier, callback =
|
|
261
|
+
delete(identifier, callback = ((node) => node.key), ignoreCount = false) {
|
|
262
262
|
const bstDeletedResult = [];
|
|
263
263
|
if (!this.root)
|
|
264
264
|
return bstDeletedResult;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeletedResult,
|
|
2
|
+
import { BinaryTreeDeletedResult, BTNKey, BinaryTreeNodeNested, BTNCallback } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
4
|
-
createNode(key:
|
|
5
|
-
add(keyOrNode:
|
|
6
|
-
delete<C extends
|
|
4
|
+
createNode(key: BTNKey, val?: N['val']): N;
|
|
5
|
+
add(keyOrNode: BTNKey | N | null, val?: N['val']): N | null | undefined;
|
|
6
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
7
7
|
}
|
|
@@ -18,7 +18,7 @@ export declare enum FamilyPosition {
|
|
|
18
18
|
ISOLATED = "ISOLATED",
|
|
19
19
|
MAL_NODE = "MAL_NODE"
|
|
20
20
|
}
|
|
21
|
-
export type
|
|
21
|
+
export type BTNKey = number;
|
|
22
22
|
export type BinaryTreeDeletedResult<N> = {
|
|
23
23
|
deleted: N | null | undefined;
|
|
24
24
|
needBalanced: N | null;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BSTNode } from '../../../data-structures';
|
|
2
|
-
import type {
|
|
3
|
-
export type BSTComparator = (a:
|
|
2
|
+
import type { BTNKey, BinaryTreeOptions } from './binary-tree';
|
|
3
|
+
export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
|
|
4
4
|
export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
5
|
export type BSTOptions = BinaryTreeOptions & {
|
|
6
6
|
comparator?: BSTComparator;
|
package/dist/types/helpers.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
2
2
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
3
|
-
export type
|
|
3
|
+
export type BTNCallback<N, D = any> = (node: N) => D;
|
|
4
4
|
export declare enum CP {
|
|
5
5
|
lt = "lt",
|
|
6
6
|
eq = "eq",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "linked-list-typed",
|
|
3
|
-
"version": "1.39.
|
|
3
|
+
"version": "1.39.4",
|
|
4
4
|
"description": "Linked List, Doubly Linked List, Singly Linked List. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
"typescript": "^4.9.5"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"data-structure-typed": "^1.39.
|
|
69
|
+
"data-structure-typed": "^1.39.4"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -6,14 +6,14 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import {BST, BSTNode} from './bst';
|
|
9
|
-
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult,
|
|
10
|
-
import {
|
|
9
|
+
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BTNKey} from '../../types';
|
|
10
|
+
import {BTNCallback} from '../../types';
|
|
11
11
|
import {IBinaryTree} from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
14
14
|
height: number;
|
|
15
15
|
|
|
16
|
-
constructor(key:
|
|
16
|
+
constructor(key: BTNKey, val?: V) {
|
|
17
17
|
super(key, val);
|
|
18
18
|
this.height = 0;
|
|
19
19
|
}
|
|
@@ -21,7 +21,8 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|
|
21
21
|
|
|
22
22
|
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
|
|
23
23
|
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N>
|
|
24
|
+
implements IBinaryTree<V, N>
|
|
25
|
+
{
|
|
25
26
|
/**
|
|
26
27
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
27
28
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -34,27 +35,27 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
34
35
|
|
|
35
36
|
/**
|
|
36
37
|
* The function creates a new AVL tree node with the specified key and value.
|
|
37
|
-
* @param {
|
|
38
|
+
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
38
39
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
39
40
|
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
|
|
40
41
|
* type `V`, which means it can be any value that is assignable to the `val` property of the
|
|
41
42
|
* node type `N`.
|
|
42
43
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
43
44
|
*/
|
|
44
|
-
override createNode(key:
|
|
45
|
+
override createNode(key: BTNKey, val?: V): N {
|
|
45
46
|
return new AVLTreeNode<V, N>(key, val) as N;
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
/**
|
|
49
50
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
50
51
|
* a new node.
|
|
51
|
-
* @param {
|
|
52
|
-
* `
|
|
52
|
+
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
53
|
+
* `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
53
54
|
* @param [val] - The `val` parameter is the value that you want to assign to the new node that you
|
|
54
55
|
* are adding to the binary search tree.
|
|
55
56
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
56
57
|
*/
|
|
57
|
-
override add(keyOrNode:
|
|
58
|
+
override add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
|
|
58
59
|
// TODO support node as a param
|
|
59
60
|
const inserted = super.add(keyOrNode, val);
|
|
60
61
|
if (inserted) this._balancePath(inserted);
|
|
@@ -65,18 +66,19 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
65
66
|
* The function overrides the delete method of a binary tree and balances the tree after deleting a
|
|
66
67
|
* node if necessary.
|
|
67
68
|
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
68
|
-
* `
|
|
69
|
+
* `BTNKey` or a generic type `N`. It represents the property of the node that we are
|
|
69
70
|
* searching for. It can be a specific key value or any other property of the node.
|
|
70
71
|
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
71
72
|
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
72
73
|
* included in the result. The `callback` parameter has a default value of
|
|
73
|
-
* `
|
|
74
|
+
* `((node: N) => node.key)`
|
|
74
75
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
75
76
|
*/
|
|
76
|
-
override delete<C extends
|
|
77
|
+
override delete<C extends BTNCallback<N>>(
|
|
77
78
|
identifier: ReturnType<C>,
|
|
78
|
-
callback: C =
|
|
79
|
+
callback: C = ((node: N) => node.key) as C
|
|
79
80
|
): BinaryTreeDeletedResult<N>[] {
|
|
81
|
+
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
|
|
80
82
|
const deletedResults = super.delete(identifier, callback);
|
|
81
83
|
for (const {needBalanced} of deletedResults) {
|
|
82
84
|
if (needBalanced) {
|
|
@@ -160,7 +162,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
160
162
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
161
163
|
switch (
|
|
162
164
|
this._balanceFactor(A) // second O(1)
|
|
163
|
-
|
|
165
|
+
) {
|
|
164
166
|
case -2:
|
|
165
167
|
if (A && A.left) {
|
|
166
168
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
|
|
|
17
17
|
* @param - - `frequency`: The default frequency value. It is optional and has a default
|
|
18
18
|
* value of 0.
|
|
19
19
|
*/
|
|
20
|
-
constructor({frequency = 0, max}: {
|
|
20
|
+
constructor({frequency = 0, max}: {frequency?: number; max: number}) {
|
|
21
21
|
this._freq = frequency;
|
|
22
22
|
this._max = max;
|
|
23
23
|
this._freqMap = {0: 0};
|