linked-list-typed 1.38.5 → 1.38.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/dist/data-structures/binary-tree/avl-tree.d.ts +14 -8
- package/dist/data-structures/binary-tree/avl-tree.js +10 -5
- package/dist/data-structures/binary-tree/binary-tree.d.ts +59 -107
- package/dist/data-structures/binary-tree/binary-tree.js +72 -81
- package/dist/data-structures/binary-tree/bst.d.ts +13 -13
- package/dist/data-structures/binary-tree/bst.js +14 -14
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +15 -11
- package/dist/data-structures/binary-tree/tree-multiset.js +11 -7
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/helpers.d.ts +2 -0
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +22 -13
- package/src/data-structures/binary-tree/binary-tree.ts +155 -111
- package/src/data-structures/binary-tree/bst.ts +26 -26
- package/src/data-structures/binary-tree/rb-tree.ts +6 -9
- package/src/data-structures/binary-tree/tree-multiset.ts +24 -19
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/helpers.ts +4 -0
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
-
import { BinaryTreeDeletedResult, IterationType } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, IterationType, MapCallback } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
|
-
export declare class TreeMultisetNode<V = any,
|
|
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.
|
|
@@ -26,7 +26,7 @@ export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V
|
|
|
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.
|
|
28
28
|
*/
|
|
29
|
-
export declare class TreeMultiset<N extends TreeMultisetNode<
|
|
29
|
+
export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode> extends AVLTree<V, N> implements IBinaryTree<V, N> {
|
|
30
30
|
/**
|
|
31
31
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
32
32
|
* merge duplicated values.
|
|
@@ -45,7 +45,7 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
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: BinaryTreeNodeKey, val?:
|
|
48
|
+
createNode(key: BinaryTreeNodeKey, 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.
|
|
@@ -59,7 +59,7 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
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: BinaryTreeNodeKey | N | null, val?:
|
|
62
|
+
add(keyOrNode: BinaryTreeNodeKey | 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
|
|
@@ -74,12 +74,12 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
74
74
|
* inserted nodes.
|
|
75
75
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
76
76
|
* added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
|
|
77
|
-
* @param {
|
|
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: (BinaryTreeNodeKey | null)[] | (N | null)[], data?:
|
|
82
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | 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.
|
|
@@ -92,16 +92,20 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
92
92
|
/**
|
|
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
|
-
* @param {
|
|
96
|
-
*
|
|
97
|
-
*
|
|
95
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
96
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
97
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
98
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
99
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
100
|
+
* included in the result. The `callback` parameter has a default value of
|
|
101
|
+
* `this._defaultCallbackByKey`
|
|
98
102
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
99
103
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
100
104
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
101
105
|
* decremented by 1 and
|
|
102
106
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
103
107
|
*/
|
|
104
|
-
delete(
|
|
108
|
+
delete<C extends MapCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
105
109
|
/**
|
|
106
110
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
107
111
|
*/
|
|
@@ -177,7 +177,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
177
177
|
* inserted nodes.
|
|
178
178
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
179
179
|
* added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
|
|
180
|
-
* @param {
|
|
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.
|
|
183
183
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
@@ -191,7 +191,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
191
191
|
continue;
|
|
192
192
|
}
|
|
193
193
|
if (keyOrNode === null) {
|
|
194
|
-
inserted.push(this.add(NaN,
|
|
194
|
+
inserted.push(this.add(NaN, undefined, 0));
|
|
195
195
|
continue;
|
|
196
196
|
}
|
|
197
197
|
inserted.push(this.add(keyOrNode, data === null || data === void 0 ? void 0 : data[i], 1));
|
|
@@ -245,20 +245,24 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
245
245
|
/**
|
|
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
|
-
* @param {
|
|
249
|
-
*
|
|
250
|
-
*
|
|
248
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
249
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
250
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
251
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
252
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
253
|
+
* included in the result. The `callback` parameter has a default value of
|
|
254
|
+
* `this._defaultCallbackByKey`
|
|
251
255
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
252
256
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
253
257
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
254
258
|
* decremented by 1 and
|
|
255
259
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
256
260
|
*/
|
|
257
|
-
delete(
|
|
261
|
+
delete(identifier, callback = this._defaultCallbackByKey, ignoreCount = false) {
|
|
258
262
|
const bstDeletedResult = [];
|
|
259
263
|
if (!this.root)
|
|
260
264
|
return bstDeletedResult;
|
|
261
|
-
const curr = this.get(
|
|
265
|
+
const curr = this.get(identifier, callback);
|
|
262
266
|
if (!curr)
|
|
263
267
|
return bstDeletedResult;
|
|
264
268
|
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../types';
|
|
3
|
-
export interface IBinaryTree<N extends BinaryTreeNode<
|
|
2
|
+
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, MapCallback } from '../types';
|
|
3
|
+
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
4
4
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
5
5
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
6
|
-
delete(
|
|
6
|
+
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
|
|
7
7
|
}
|
package/dist/types/helpers.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { BinaryTreeNodeKey } from './data-structures';
|
|
1
2
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
2
3
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
3
4
|
export type MapCallback<N, D = any> = (node: N) => D;
|
|
5
|
+
export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
|
|
4
6
|
export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
|
|
5
7
|
export declare enum CP {
|
|
6
8
|
lt = "lt",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "linked-list-typed",
|
|
3
|
-
"version": "1.38.
|
|
3
|
+
"version": "1.38.7",
|
|
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.38.
|
|
69
|
+
"data-structure-typed": "^1.38.7"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -7,12 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import {BST, BSTNode} from './bst';
|
|
9
9
|
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../../types';
|
|
10
|
+
import {MapCallback} from '../../types';
|
|
10
11
|
import {IBinaryTree} from '../../interfaces';
|
|
11
12
|
|
|
12
|
-
export class AVLTreeNode<V = any,
|
|
13
|
-
V,
|
|
14
|
-
FAMILY
|
|
15
|
-
> {
|
|
13
|
+
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
16
14
|
height: number;
|
|
17
15
|
|
|
18
16
|
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
@@ -21,7 +19,10 @@ export class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTre
|
|
|
21
19
|
}
|
|
22
20
|
}
|
|
23
21
|
|
|
24
|
-
export class AVLTree<N extends AVLTreeNode<
|
|
22
|
+
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode>
|
|
23
|
+
extends BST<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
|
|
@@ -37,12 +38,12 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
37
38
|
* @param {BinaryTreeNodeKey} 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
|
-
* type `
|
|
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: BinaryTreeNodeKey, val?:
|
|
45
|
-
return new AVLTreeNode<
|
|
45
|
+
override createNode(key: BinaryTreeNodeKey, val?: V): N {
|
|
46
|
+
return new AVLTreeNode<V, N>(key, val) as N;
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
/**
|
|
@@ -54,7 +55,7 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
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: BinaryTreeNodeKey | N | null, val?:
|
|
58
|
+
override add(keyOrNode: BinaryTreeNodeKey | 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);
|
|
@@ -64,12 +65,20 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
64
65
|
/**
|
|
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
|
-
* @param {
|
|
68
|
-
*
|
|
68
|
+
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a
|
|
69
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
70
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
71
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
72
|
+
* value. This value is compared with the `identifier` parameter to determine if the node should be
|
|
73
|
+
* included in the result. The `callback` parameter has a default value of
|
|
74
|
+
* `this._defaultCallbackByKey`
|
|
69
75
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
70
76
|
*/
|
|
71
|
-
override delete
|
|
72
|
-
|
|
77
|
+
override delete<C extends MapCallback<N>>(
|
|
78
|
+
identifier: ReturnType<C>,
|
|
79
|
+
callback: C = this._defaultCallbackByKey as C
|
|
80
|
+
): BinaryTreeDeletedResult<N>[] {
|
|
81
|
+
const deletedResults = super.delete(identifier, callback);
|
|
73
82
|
for (const {needBalanced} of deletedResults) {
|
|
74
83
|
if (needBalanced) {
|
|
75
84
|
this._balancePath(needBalanced);
|