linked-list-typed 1.38.6 → 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 +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +28 -28
- package/dist/data-structures/binary-tree/binary-tree.js +22 -22
- package/dist/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/data-structures/binary-tree/bst.js +1 -1
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +10 -10
- package/src/data-structures/binary-tree/binary-tree.ts +42 -42
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/rb-tree.ts +4 -7
- package/src/data-structures/binary-tree/tree-multiset.ts +10 -13
- package/src/interfaces/binary-tree.ts +2 -2
|
@@ -7,13 +7,13 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
9
|
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../../types';
|
|
10
|
-
import { IBinaryTree } from '../../interfaces';
|
|
11
10
|
import { MapCallback } from '../../types';
|
|
12
|
-
|
|
11
|
+
import { IBinaryTree } from '../../interfaces';
|
|
12
|
+
export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
13
13
|
height: number;
|
|
14
14
|
constructor(key: BinaryTreeNodeKey, val?: V);
|
|
15
15
|
}
|
|
16
|
-
export declare class AVLTree<N extends AVLTreeNode<
|
|
16
|
+
export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
17
17
|
/**
|
|
18
18
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
19
19
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -26,11 +26,11 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
|
|
|
26
26
|
* @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
|
|
27
27
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
28
28
|
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
|
|
29
|
-
* type `
|
|
29
|
+
* type `V`, which means it can be any value that is assignable to the `val` property of the
|
|
30
30
|
* node type `N`.
|
|
31
31
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
32
32
|
*/
|
|
33
|
-
createNode(key: BinaryTreeNodeKey, val?:
|
|
33
|
+
createNode(key: BinaryTreeNodeKey, val?: V): N;
|
|
34
34
|
/**
|
|
35
35
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
36
36
|
* a new node.
|
|
@@ -40,7 +40,7 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
|
|
|
40
40
|
* are adding to the binary search tree.
|
|
41
41
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
42
42
|
*/
|
|
43
|
-
add(keyOrNode: BinaryTreeNodeKey | N | null, val?:
|
|
43
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined;
|
|
44
44
|
/**
|
|
45
45
|
* The function overrides the delete method of a binary tree and balances the tree after deleting a
|
|
46
46
|
* node if necessary.
|
|
@@ -31,7 +31,7 @@ class AVLTree extends bst_1.BST {
|
|
|
31
31
|
* @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
|
|
32
32
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
33
33
|
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
|
|
34
|
-
* type `
|
|
34
|
+
* type `V`, which means it can be any value that is assignable to the `val` property of the
|
|
35
35
|
* node type `N`.
|
|
36
36
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
37
37
|
*/
|
|
@@ -11,9 +11,9 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
11
11
|
/**
|
|
12
12
|
* Represents a node in a binary tree.
|
|
13
13
|
* @template V - The type of data stored in the node.
|
|
14
|
-
* @template
|
|
14
|
+
* @template N - The type of the family relationship in the binary tree.
|
|
15
15
|
*/
|
|
16
|
-
export declare class BinaryTreeNode<V = any,
|
|
16
|
+
export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> {
|
|
17
17
|
/**
|
|
18
18
|
* The key associated with the node.
|
|
19
19
|
*/
|
|
@@ -25,7 +25,7 @@ export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FA
|
|
|
25
25
|
/**
|
|
26
26
|
* The parent node of the current node.
|
|
27
27
|
*/
|
|
28
|
-
parent:
|
|
28
|
+
parent: N | null | undefined;
|
|
29
29
|
/**
|
|
30
30
|
* Creates a new instance of BinaryTreeNode.
|
|
31
31
|
* @param {BinaryTreeNodeKey} key - The key associated with the node.
|
|
@@ -36,22 +36,22 @@ export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FA
|
|
|
36
36
|
/**
|
|
37
37
|
* Get the left child node.
|
|
38
38
|
*/
|
|
39
|
-
get left():
|
|
39
|
+
get left(): N | null | undefined;
|
|
40
40
|
/**
|
|
41
41
|
* Set the left child node.
|
|
42
|
-
* @param {
|
|
42
|
+
* @param {N | null | undefined} v - The left child node.
|
|
43
43
|
*/
|
|
44
|
-
set left(v:
|
|
44
|
+
set left(v: N | null | undefined);
|
|
45
45
|
private _right;
|
|
46
46
|
/**
|
|
47
47
|
* Get the right child node.
|
|
48
48
|
*/
|
|
49
|
-
get right():
|
|
49
|
+
get right(): N | null | undefined;
|
|
50
50
|
/**
|
|
51
51
|
* Set the right child node.
|
|
52
|
-
* @param {
|
|
52
|
+
* @param {N | null | undefined} v - The right child node.
|
|
53
53
|
*/
|
|
54
|
-
set right(v:
|
|
54
|
+
set right(v: N | null | undefined);
|
|
55
55
|
/**
|
|
56
56
|
* Get the position of the node within its family.
|
|
57
57
|
* @returns {FamilyPosition} - The family position of the node.
|
|
@@ -62,13 +62,22 @@ export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FA
|
|
|
62
62
|
* Represents a binary tree data structure.
|
|
63
63
|
* @template N - The type of the binary tree's nodes.
|
|
64
64
|
*/
|
|
65
|
-
export declare class BinaryTree<N extends BinaryTreeNode<
|
|
66
|
-
private _loopType;
|
|
65
|
+
export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode> implements IBinaryTree<V, N> {
|
|
67
66
|
/**
|
|
68
67
|
* Creates a new instance of BinaryTree.
|
|
69
68
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
70
69
|
*/
|
|
71
70
|
constructor(options?: BinaryTreeOptions);
|
|
71
|
+
private _iterationType;
|
|
72
|
+
/**
|
|
73
|
+
* Get the iteration type used in the binary tree.
|
|
74
|
+
*/
|
|
75
|
+
get iterationType(): IterationType;
|
|
76
|
+
/**
|
|
77
|
+
* Set the iteration type for the binary tree.
|
|
78
|
+
* @param {IterationType} v - The new iteration type to set.
|
|
79
|
+
*/
|
|
80
|
+
set iterationType(v: IterationType);
|
|
72
81
|
private _root;
|
|
73
82
|
/**
|
|
74
83
|
* Get the root node of the binary tree.
|
|
@@ -79,22 +88,13 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
79
88
|
* Get the number of nodes in the binary tree.
|
|
80
89
|
*/
|
|
81
90
|
get size(): number;
|
|
82
|
-
/**
|
|
83
|
-
* Get the iteration type used in the binary tree.
|
|
84
|
-
*/
|
|
85
|
-
get iterationType(): IterationType;
|
|
86
|
-
/**
|
|
87
|
-
* Set the iteration type for the binary tree.
|
|
88
|
-
* @param {IterationType} v - The new iteration type to set.
|
|
89
|
-
*/
|
|
90
|
-
set iterationType(v: IterationType);
|
|
91
91
|
/**
|
|
92
92
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
93
93
|
* @param {BinaryTreeNodeKey} key - The key for the new node.
|
|
94
|
-
* @param {
|
|
94
|
+
* @param {V} val - The value for the new node.
|
|
95
95
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
96
96
|
*/
|
|
97
|
-
createNode(key: BinaryTreeNodeKey, val?:
|
|
97
|
+
createNode(key: BinaryTreeNodeKey, val?: V): N;
|
|
98
98
|
/**
|
|
99
99
|
* Clear the binary tree, removing all nodes.
|
|
100
100
|
*/
|
|
@@ -107,31 +107,31 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
107
107
|
/**
|
|
108
108
|
* Add a node with the given key and value to the binary tree.
|
|
109
109
|
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
110
|
-
* @param {
|
|
110
|
+
* @param {V} val - The value for the new node (optional).
|
|
111
111
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
112
112
|
*/
|
|
113
|
-
add(keyOrNode: BinaryTreeNodeKey | N | null, val?:
|
|
113
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined;
|
|
114
114
|
/**
|
|
115
115
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
116
116
|
* values, and adds them to the binary tree.
|
|
117
117
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
118
118
|
* objects, or null values.
|
|
119
|
-
* @param {
|
|
119
|
+
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
|
|
120
120
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
121
121
|
* the value of the nodes will be `undefined`.
|
|
122
122
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
123
123
|
*/
|
|
124
|
-
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?:
|
|
124
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[];
|
|
125
125
|
/**
|
|
126
126
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
127
127
|
* @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
128
128
|
* `BinaryTreeNodeKey` or `N` values.
|
|
129
|
-
* @param {N[] | Array<
|
|
129
|
+
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
130
130
|
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
131
131
|
* array. Each value in the `data` array will be assigned to the
|
|
132
132
|
* @returns The method is returning a boolean value.
|
|
133
133
|
*/
|
|
134
|
-
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?:
|
|
134
|
+
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: Array<V>): boolean;
|
|
135
135
|
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N): BinaryTreeDeletedResult<N>[];
|
|
136
136
|
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
|
|
137
137
|
/**
|
|
@@ -14,7 +14,7 @@ const queue_1 = require("../queue");
|
|
|
14
14
|
/**
|
|
15
15
|
* Represents a node in a binary tree.
|
|
16
16
|
* @template V - The type of data stored in the node.
|
|
17
|
-
* @template
|
|
17
|
+
* @template N - The type of the family relationship in the binary tree.
|
|
18
18
|
*/
|
|
19
19
|
class BinaryTreeNode {
|
|
20
20
|
/**
|
|
@@ -34,7 +34,7 @@ class BinaryTreeNode {
|
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
36
36
|
* Set the left child node.
|
|
37
|
-
* @param {
|
|
37
|
+
* @param {N | null | undefined} v - The left child node.
|
|
38
38
|
*/
|
|
39
39
|
set left(v) {
|
|
40
40
|
if (v) {
|
|
@@ -50,7 +50,7 @@ class BinaryTreeNode {
|
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* Set the right child node.
|
|
53
|
-
* @param {
|
|
53
|
+
* @param {N | null | undefined} v - The right child node.
|
|
54
54
|
*/
|
|
55
55
|
set right(v) {
|
|
56
56
|
if (v) {
|
|
@@ -87,7 +87,7 @@ class BinaryTree {
|
|
|
87
87
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
88
88
|
*/
|
|
89
89
|
constructor(options) {
|
|
90
|
-
this.
|
|
90
|
+
this._iterationType = types_1.IterationType.ITERATIVE;
|
|
91
91
|
this._root = null;
|
|
92
92
|
this._size = 0;
|
|
93
93
|
/**
|
|
@@ -100,38 +100,38 @@ class BinaryTree {
|
|
|
100
100
|
this._defaultCallbackByKey = node => node.key;
|
|
101
101
|
if (options !== undefined) {
|
|
102
102
|
const { iterationType = types_1.IterationType.ITERATIVE } = options;
|
|
103
|
-
this.
|
|
103
|
+
this._iterationType = iterationType;
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
/**
|
|
107
|
-
* Get the
|
|
107
|
+
* Get the iteration type used in the binary tree.
|
|
108
108
|
*/
|
|
109
|
-
get
|
|
110
|
-
return this.
|
|
109
|
+
get iterationType() {
|
|
110
|
+
return this._iterationType;
|
|
111
111
|
}
|
|
112
112
|
/**
|
|
113
|
-
*
|
|
113
|
+
* Set the iteration type for the binary tree.
|
|
114
|
+
* @param {IterationType} v - The new iteration type to set.
|
|
114
115
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
set iterationType(v) {
|
|
117
|
+
this._iterationType = v;
|
|
117
118
|
}
|
|
118
119
|
/**
|
|
119
|
-
* Get the
|
|
120
|
+
* Get the root node of the binary tree.
|
|
120
121
|
*/
|
|
121
|
-
get
|
|
122
|
-
return this.
|
|
122
|
+
get root() {
|
|
123
|
+
return this._root;
|
|
123
124
|
}
|
|
124
125
|
/**
|
|
125
|
-
*
|
|
126
|
-
* @param {IterationType} v - The new iteration type to set.
|
|
126
|
+
* Get the number of nodes in the binary tree.
|
|
127
127
|
*/
|
|
128
|
-
|
|
129
|
-
this.
|
|
128
|
+
get size() {
|
|
129
|
+
return this._size;
|
|
130
130
|
}
|
|
131
131
|
/**
|
|
132
132
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
133
133
|
* @param {BinaryTreeNodeKey} key - The key for the new node.
|
|
134
|
-
* @param {
|
|
134
|
+
* @param {V} val - The value for the new node.
|
|
135
135
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
136
136
|
*/
|
|
137
137
|
createNode(key, val) {
|
|
@@ -154,7 +154,7 @@ class BinaryTree {
|
|
|
154
154
|
/**
|
|
155
155
|
* Add a node with the given key and value to the binary tree.
|
|
156
156
|
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
157
|
-
* @param {
|
|
157
|
+
* @param {V} val - The value for the new node (optional).
|
|
158
158
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
159
159
|
*/
|
|
160
160
|
add(keyOrNode, val) {
|
|
@@ -219,7 +219,7 @@ class BinaryTree {
|
|
|
219
219
|
* values, and adds them to the binary tree.
|
|
220
220
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
221
221
|
* objects, or null values.
|
|
222
|
-
* @param {
|
|
222
|
+
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
|
|
223
223
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
224
224
|
* the value of the nodes will be `undefined`.
|
|
225
225
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
@@ -241,7 +241,7 @@ class BinaryTree {
|
|
|
241
241
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
242
242
|
* @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
243
243
|
* `BinaryTreeNodeKey` or `N` values.
|
|
244
|
-
* @param {N[] | Array<
|
|
244
|
+
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
245
245
|
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
246
246
|
* array. Each value in the `data` array will be assigned to the
|
|
247
247
|
* @returns The method is returning a boolean value.
|
|
@@ -9,10 +9,10 @@ import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, MapCa
|
|
|
9
9
|
import { CP, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
|
-
export declare class BSTNode<V = any,
|
|
12
|
+
export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
13
13
|
constructor(key: BinaryTreeNodeKey, val?: V);
|
|
14
14
|
}
|
|
15
|
-
export declare class BST<N extends BSTNode<
|
|
15
|
+
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
16
16
|
/**
|
|
17
17
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
18
18
|
* function.
|
|
@@ -28,7 +28,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
28
28
|
* represents the value associated with the node in a binary search tree.
|
|
29
29
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
30
30
|
*/
|
|
31
|
-
createNode(key: BinaryTreeNodeKey, val?:
|
|
31
|
+
createNode(key: BinaryTreeNodeKey, val?: V): N;
|
|
32
32
|
/**
|
|
33
33
|
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
34
34
|
* into the tree.
|
|
@@ -39,7 +39,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
39
39
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
40
40
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
41
41
|
*/
|
|
42
|
-
add(keyOrNode: BinaryTreeNodeKey | N | null, val?:
|
|
42
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined;
|
|
43
43
|
/**
|
|
44
44
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
45
45
|
* maintaining balance.
|
|
@@ -47,13 +47,13 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
47
47
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
48
48
|
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
49
49
|
* `null
|
|
50
|
-
* @param {
|
|
50
|
+
* @param {V[]} data - The values of tree nodes
|
|
51
51
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
52
52
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
53
53
|
* It can have two possible values:
|
|
54
54
|
* @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
|
|
55
55
|
*/
|
|
56
|
-
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?:
|
|
56
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[];
|
|
57
57
|
/**
|
|
58
58
|
* The function returns the first node in the binary tree that matches the given node property and
|
|
59
59
|
* callback.
|
|
@@ -130,7 +130,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
130
130
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
131
131
|
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
132
132
|
* `null
|
|
133
|
-
* @param {
|
|
133
|
+
* @param {V[]} data - The values of tree nodes
|
|
134
134
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
135
135
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
136
136
|
* It can have two possible values:
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
|
|
2
2
|
import { IBinaryTree } from '../../interfaces';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
|
-
export declare class RBTreeNode<V = any,
|
|
4
|
+
export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
5
5
|
constructor(key: BinaryTreeNodeKey, val?: V);
|
|
6
6
|
private _color;
|
|
7
7
|
get color(): RBColor;
|
|
8
8
|
set color(value: RBColor);
|
|
9
9
|
}
|
|
10
|
-
export declare class RBTree<N extends RBTreeNode<
|
|
10
|
+
export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
11
11
|
constructor(options?: RBTreeOptions);
|
|
12
|
-
createNode(key: BinaryTreeNodeKey, val?:
|
|
12
|
+
createNode(key: BinaryTreeNodeKey, val?: V): N;
|
|
13
13
|
}
|
|
@@ -9,7 +9,7 @@ import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } f
|
|
|
9
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.
|
|
@@ -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));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, MapCallback } 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
6
|
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
|
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,13 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import {BST, BSTNode} from './bst';
|
|
9
9
|
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../../types';
|
|
10
|
-
import {IBinaryTree} from '../../interfaces';
|
|
11
10
|
import {MapCallback} from '../../types';
|
|
11
|
+
import {IBinaryTree} from '../../interfaces';
|
|
12
12
|
|
|
13
|
-
export class AVLTreeNode<V = any,
|
|
14
|
-
V,
|
|
15
|
-
FAMILY
|
|
16
|
-
> {
|
|
13
|
+
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
17
14
|
height: number;
|
|
18
15
|
|
|
19
16
|
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
@@ -22,7 +19,10 @@ export class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTre
|
|
|
22
19
|
}
|
|
23
20
|
}
|
|
24
21
|
|
|
25
|
-
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
|
+
{
|
|
26
26
|
/**
|
|
27
27
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
28
28
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -38,12 +38,12 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
38
38
|
* @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
|
|
39
39
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
40
40
|
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
|
|
41
|
-
* type `
|
|
41
|
+
* type `V`, which means it can be any value that is assignable to the `val` property of the
|
|
42
42
|
* node type `N`.
|
|
43
43
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
44
44
|
*/
|
|
45
|
-
override createNode(key: BinaryTreeNodeKey, val?:
|
|
46
|
-
return new AVLTreeNode<
|
|
45
|
+
override createNode(key: BinaryTreeNodeKey, val?: V): N {
|
|
46
|
+
return new AVLTreeNode<V, N>(key, val) as N;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
@@ -55,7 +55,7 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
55
55
|
* are adding to the binary search tree.
|
|
56
56
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
57
57
|
*/
|
|
58
|
-
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?:
|
|
58
|
+
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
|
|
59
59
|
// TODO support node as a param
|
|
60
60
|
const inserted = super.add(keyOrNode, val);
|
|
61
61
|
if (inserted) this._balancePath(inserted);
|
|
@@ -23,9 +23,9 @@ import {Queue} from '../queue';
|
|
|
23
23
|
/**
|
|
24
24
|
* Represents a node in a binary tree.
|
|
25
25
|
* @template V - The type of data stored in the node.
|
|
26
|
-
* @template
|
|
26
|
+
* @template N - The type of the family relationship in the binary tree.
|
|
27
27
|
*/
|
|
28
|
-
export class BinaryTreeNode<V = any,
|
|
28
|
+
export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> {
|
|
29
29
|
/**
|
|
30
30
|
* The key associated with the node.
|
|
31
31
|
*/
|
|
@@ -39,7 +39,7 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
|
|
|
39
39
|
/**
|
|
40
40
|
* The parent node of the current node.
|
|
41
41
|
*/
|
|
42
|
-
parent:
|
|
42
|
+
parent: N | null | undefined;
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* Creates a new instance of BinaryTreeNode.
|
|
@@ -51,42 +51,42 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
|
|
|
51
51
|
this.val = val;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
private _left:
|
|
54
|
+
private _left: N | null | undefined;
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
57
|
* Get the left child node.
|
|
58
58
|
*/
|
|
59
|
-
get left():
|
|
59
|
+
get left(): N | null | undefined {
|
|
60
60
|
return this._left;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
64
|
* Set the left child node.
|
|
65
|
-
* @param {
|
|
65
|
+
* @param {N | null | undefined} v - The left child node.
|
|
66
66
|
*/
|
|
67
|
-
set left(v:
|
|
67
|
+
set left(v: N | null | undefined) {
|
|
68
68
|
if (v) {
|
|
69
|
-
v.parent = this as unknown as
|
|
69
|
+
v.parent = this as unknown as N;
|
|
70
70
|
}
|
|
71
71
|
this._left = v;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
private _right:
|
|
74
|
+
private _right: N | null | undefined;
|
|
75
75
|
|
|
76
76
|
/**
|
|
77
77
|
* Get the right child node.
|
|
78
78
|
*/
|
|
79
|
-
get right():
|
|
79
|
+
get right(): N | null | undefined {
|
|
80
80
|
return this._right;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
84
|
* Set the right child node.
|
|
85
|
-
* @param {
|
|
85
|
+
* @param {N | null | undefined} v - The right child node.
|
|
86
86
|
*/
|
|
87
|
-
set right(v:
|
|
87
|
+
set right(v: N | null | undefined) {
|
|
88
88
|
if (v) {
|
|
89
|
-
v.parent = this as unknown as
|
|
89
|
+
v.parent = this as unknown as N;
|
|
90
90
|
}
|
|
91
91
|
this._right = v;
|
|
92
92
|
}
|
|
@@ -96,7 +96,7 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
|
|
|
96
96
|
* @returns {FamilyPosition} - The family position of the node.
|
|
97
97
|
*/
|
|
98
98
|
get familyPosition(): FamilyPosition {
|
|
99
|
-
const that = this as unknown as
|
|
99
|
+
const that = this as unknown as N;
|
|
100
100
|
if (!this.parent) {
|
|
101
101
|
return this.left || this.right ? FamilyPosition.ROOT : FamilyPosition.ISOLATED;
|
|
102
102
|
}
|
|
@@ -115,9 +115,7 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
|
|
|
115
115
|
* Represents a binary tree data structure.
|
|
116
116
|
* @template N - The type of the binary tree's nodes.
|
|
117
117
|
*/
|
|
118
|
-
export class BinaryTree<N extends BinaryTreeNode<
|
|
119
|
-
private _loopType: IterationType = IterationType.ITERATIVE;
|
|
120
|
-
|
|
118
|
+
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode> implements IBinaryTree<V, N> {
|
|
121
119
|
/**
|
|
122
120
|
* Creates a new instance of BinaryTree.
|
|
123
121
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
@@ -125,10 +123,27 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
125
123
|
constructor(options?: BinaryTreeOptions) {
|
|
126
124
|
if (options !== undefined) {
|
|
127
125
|
const {iterationType = IterationType.ITERATIVE} = options;
|
|
128
|
-
this.
|
|
126
|
+
this._iterationType = iterationType;
|
|
129
127
|
}
|
|
130
128
|
}
|
|
131
129
|
|
|
130
|
+
private _iterationType: IterationType = IterationType.ITERATIVE;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Get the iteration type used in the binary tree.
|
|
134
|
+
*/
|
|
135
|
+
get iterationType(): IterationType {
|
|
136
|
+
return this._iterationType;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Set the iteration type for the binary tree.
|
|
141
|
+
* @param {IterationType} v - The new iteration type to set.
|
|
142
|
+
*/
|
|
143
|
+
set iterationType(v: IterationType) {
|
|
144
|
+
this._iterationType = v;
|
|
145
|
+
}
|
|
146
|
+
|
|
132
147
|
private _root: N | null = null;
|
|
133
148
|
|
|
134
149
|
/**
|
|
@@ -147,29 +162,14 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
147
162
|
return this._size;
|
|
148
163
|
}
|
|
149
164
|
|
|
150
|
-
/**
|
|
151
|
-
* Get the iteration type used in the binary tree.
|
|
152
|
-
*/
|
|
153
|
-
get iterationType(): IterationType {
|
|
154
|
-
return this._loopType;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Set the iteration type for the binary tree.
|
|
159
|
-
* @param {IterationType} v - The new iteration type to set.
|
|
160
|
-
*/
|
|
161
|
-
set iterationType(v: IterationType) {
|
|
162
|
-
this._loopType = v;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
165
|
/**
|
|
166
166
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
167
167
|
* @param {BinaryTreeNodeKey} key - The key for the new node.
|
|
168
|
-
* @param {
|
|
168
|
+
* @param {V} val - The value for the new node.
|
|
169
169
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
170
170
|
*/
|
|
171
|
-
createNode(key: BinaryTreeNodeKey, val?:
|
|
172
|
-
return new BinaryTreeNode<
|
|
171
|
+
createNode(key: BinaryTreeNodeKey, val?: V): N {
|
|
172
|
+
return new BinaryTreeNode<V, N>(key, val) as N;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
175
|
/**
|
|
@@ -191,10 +191,10 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
191
191
|
/**
|
|
192
192
|
* Add a node with the given key and value to the binary tree.
|
|
193
193
|
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
194
|
-
* @param {
|
|
194
|
+
* @param {V} val - The value for the new node (optional).
|
|
195
195
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
196
196
|
*/
|
|
197
|
-
add(keyOrNode: BinaryTreeNodeKey | N | null, val?:
|
|
197
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
|
|
198
198
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
199
199
|
const queue = new Queue<N | null>([root]);
|
|
200
200
|
while (queue.size > 0) {
|
|
@@ -249,12 +249,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
249
249
|
* values, and adds them to the binary tree.
|
|
250
250
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
251
251
|
* objects, or null values.
|
|
252
|
-
* @param {
|
|
252
|
+
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
|
|
253
253
|
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
254
254
|
* the value of the nodes will be `undefined`.
|
|
255
255
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
256
256
|
*/
|
|
257
|
-
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?:
|
|
257
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[] {
|
|
258
258
|
// TODO not sure addMany not be run multi times
|
|
259
259
|
return keysOrNodes.map((keyOrNode, i) => {
|
|
260
260
|
if (keyOrNode instanceof BinaryTreeNode) {
|
|
@@ -274,12 +274,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
274
274
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
275
275
|
* @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
276
276
|
* `BinaryTreeNodeKey` or `N` values.
|
|
277
|
-
* @param {N[] | Array<
|
|
277
|
+
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
|
|
278
278
|
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
279
279
|
* array. Each value in the `data` array will be assigned to the
|
|
280
280
|
* @returns The method is returning a boolean value.
|
|
281
281
|
*/
|
|
282
|
-
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?:
|
|
282
|
+
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: Array<V>): boolean {
|
|
283
283
|
this.clear();
|
|
284
284
|
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
285
285
|
}
|
|
@@ -18,13 +18,13 @@ import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
|
18
18
|
import {IBinaryTree} from '../../interfaces';
|
|
19
19
|
import {Queue} from '../queue';
|
|
20
20
|
|
|
21
|
-
export class BSTNode<V = any,
|
|
21
|
+
export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
22
22
|
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
23
23
|
super(key, val);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export class BST<N extends BSTNode<
|
|
27
|
+
export class BST<V = any, N extends BSTNode<V, N> = BSTNode> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
28
28
|
/**
|
|
29
29
|
* The constructor function initializes a binary search tree object with an optional comparator
|
|
30
30
|
* function.
|
|
@@ -49,8 +49,8 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
49
49
|
* represents the value associated with the node in a binary search tree.
|
|
50
50
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
51
51
|
*/
|
|
52
|
-
override createNode(key: BinaryTreeNodeKey, val?:
|
|
53
|
-
return new BSTNode<
|
|
52
|
+
override createNode(key: BinaryTreeNodeKey, val?: V): N {
|
|
53
|
+
return new BSTNode<V, N>(key, val) as N;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/**
|
|
@@ -63,7 +63,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
63
63
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
64
64
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
65
65
|
*/
|
|
66
|
-
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?:
|
|
66
|
+
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
|
|
67
67
|
// TODO support node as a parameter
|
|
68
68
|
let inserted: N | null = null;
|
|
69
69
|
let newNode: N | null = null;
|
|
@@ -136,7 +136,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
136
136
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
137
137
|
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
138
138
|
* `null
|
|
139
|
-
* @param {
|
|
139
|
+
* @param {V[]} data - The values of tree nodes
|
|
140
140
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
141
141
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
142
142
|
* It can have two possible values:
|
|
@@ -145,7 +145,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
145
145
|
|
|
146
146
|
override addMany(
|
|
147
147
|
keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
|
|
148
|
-
data?:
|
|
148
|
+
data?: V[],
|
|
149
149
|
isBalanceAdd = true,
|
|
150
150
|
iterationType = this.iterationType
|
|
151
151
|
): (N | null | undefined)[] {
|
|
@@ -174,7 +174,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
174
174
|
}
|
|
175
175
|
|
|
176
176
|
let sortedKeysOrNodes: (number | N | null)[] = [],
|
|
177
|
-
sortedData: (
|
|
177
|
+
sortedData: (V | undefined)[] | undefined = [];
|
|
178
178
|
|
|
179
179
|
if (isNodeOrNullTuple(combinedArr)) {
|
|
180
180
|
sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
|
|
@@ -185,7 +185,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|
|
185
185
|
}
|
|
186
186
|
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
187
187
|
sortedData = sorted.map(([, val]) => val);
|
|
188
|
-
const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?:
|
|
188
|
+
const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?: (V | undefined)[]) => {
|
|
189
189
|
if (arr.length === 0) return;
|
|
190
190
|
|
|
191
191
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
@@ -2,10 +2,7 @@ import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../..
|
|
|
2
2
|
import {IBinaryTree} from '../../interfaces';
|
|
3
3
|
import {BST, BSTNode} from './bst';
|
|
4
4
|
|
|
5
|
-
export class RBTreeNode<V = any,
|
|
6
|
-
V,
|
|
7
|
-
FAMILY
|
|
8
|
-
> {
|
|
5
|
+
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
9
6
|
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
10
7
|
super(key, val);
|
|
11
8
|
this._color = RBColor.RED;
|
|
@@ -22,16 +19,16 @@ export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNo
|
|
|
22
19
|
}
|
|
23
20
|
}
|
|
24
21
|
|
|
25
|
-
export class RBTree<N extends RBTreeNode<
|
|
22
|
+
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
26
23
|
constructor(options?: RBTreeOptions) {
|
|
27
24
|
super(options);
|
|
28
25
|
}
|
|
29
26
|
|
|
30
|
-
override createNode(key: BinaryTreeNodeKey, val?:
|
|
27
|
+
override createNode(key: BinaryTreeNodeKey, val?: V): N {
|
|
31
28
|
return new RBTreeNode(key, val) as N;
|
|
32
29
|
}
|
|
33
30
|
|
|
34
|
-
// override add(keyOrNode: BinaryTreeNodeKey | N | null, val?:
|
|
31
|
+
// override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
|
|
35
32
|
// const inserted = super.add(keyOrNode, val);
|
|
36
33
|
// if (inserted) this._fixInsertViolation(inserted);
|
|
37
34
|
// return inserted;
|
|
@@ -12,8 +12,8 @@ import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
|
12
12
|
|
|
13
13
|
export class TreeMultisetNode<
|
|
14
14
|
V = any,
|
|
15
|
-
|
|
16
|
-
> extends AVLTreeNode<V,
|
|
15
|
+
N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>
|
|
16
|
+
> extends AVLTreeNode<V, N> {
|
|
17
17
|
count: number;
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -35,9 +35,9 @@ export class TreeMultisetNode<
|
|
|
35
35
|
/**
|
|
36
36
|
* 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.
|
|
37
37
|
*/
|
|
38
|
-
export class TreeMultiset<N extends TreeMultisetNode<
|
|
39
|
-
extends AVLTree<N>
|
|
40
|
-
implements IBinaryTree<N>
|
|
38
|
+
export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode>
|
|
39
|
+
extends AVLTree<V, N>
|
|
40
|
+
implements IBinaryTree<V, N>
|
|
41
41
|
{
|
|
42
42
|
/**
|
|
43
43
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
@@ -64,7 +64,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
64
64
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
65
65
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
66
66
|
*/
|
|
67
|
-
override createNode(key: BinaryTreeNodeKey, val?:
|
|
67
|
+
override createNode(key: BinaryTreeNodeKey, val?: V, count?: number): N {
|
|
68
68
|
return new TreeMultisetNode(key, val, count) as N;
|
|
69
69
|
}
|
|
70
70
|
|
|
@@ -81,7 +81,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
81
81
|
* count is specified, the default count will be 1.
|
|
82
82
|
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
83
83
|
*/
|
|
84
|
-
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?:
|
|
84
|
+
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V, count = 1): N | null | undefined {
|
|
85
85
|
let inserted: N | null | undefined = undefined,
|
|
86
86
|
newNode: N | null;
|
|
87
87
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
@@ -187,15 +187,12 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
187
187
|
* inserted nodes.
|
|
188
188
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
189
189
|
* added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
|
|
190
|
-
* @param {
|
|
190
|
+
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
191
191
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
192
192
|
* each key or node.
|
|
193
193
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
194
194
|
*/
|
|
195
|
-
override addMany(
|
|
196
|
-
keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
|
|
197
|
-
data?: N['val'][]
|
|
198
|
-
): (N | null | undefined)[] {
|
|
195
|
+
override addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[] {
|
|
199
196
|
const inserted: (N | null | undefined)[] = [];
|
|
200
197
|
|
|
201
198
|
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
@@ -207,7 +204,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|
|
207
204
|
}
|
|
208
205
|
|
|
209
206
|
if (keyOrNode === null) {
|
|
210
|
-
inserted.push(this.add(NaN,
|
|
207
|
+
inserted.push(this.add(NaN, undefined, 0));
|
|
211
208
|
continue;
|
|
212
209
|
}
|
|
213
210
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {BinaryTreeNode} from '../data-structures';
|
|
2
|
-
import {BinaryTreeDeletedResult, BinaryTreeNodeKey, MapCallback} from '../types';
|
|
2
|
+
import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, MapCallback} from '../types';
|
|
3
3
|
|
|
4
|
-
export interface IBinaryTree<N extends BinaryTreeNode<
|
|
4
|
+
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
5
5
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
6
6
|
|
|
7
7
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|