min-heap-typed 1.42.3 → 1.42.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 +2 -2
- package/dist/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/data-structures/binary-tree/binary-tree.d.ts +56 -52
- package/dist/data-structures/binary-tree/binary-tree.js +115 -53
- package/dist/data-structures/binary-tree/bst.d.ts +42 -15
- package/dist/data-structures/binary-tree/bst.js +77 -21
- package/dist/data-structures/binary-tree/rb-tree.d.ts +28 -51
- package/dist/data-structures/binary-tree/rb-tree.js +148 -180
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +10 -10
- package/dist/data-structures/binary-tree/tree-multiset.js +20 -17
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/rb-tree.js +0 -5
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +5 -4
- package/src/data-structures/binary-tree/binary-tree.ts +201 -131
- package/src/data-structures/binary-tree/bst.ts +100 -34
- package/src/data-structures/binary-tree/rb-tree.ts +227 -236
- package/src/data-structures/binary-tree/tree-multiset.ts +24 -23
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +5 -5
|
@@ -52,23 +52,25 @@ 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 {BTNKey | N |
|
|
55
|
+
* @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can be either a
|
|
56
56
|
* `BTNKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
57
|
-
* node to be added), or `
|
|
57
|
+
* node to be added), or `undefined` (which represents a undefined node).
|
|
58
58
|
* @param [value] - The `value` parameter represents the value associated with the key that is being
|
|
59
59
|
* added to the binary tree.
|
|
60
60
|
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
|
61
61
|
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
|
62
62
|
* count is specified, the default count will be 1.
|
|
63
|
-
* @returns The function `add` returns a value of type `N |
|
|
63
|
+
* @returns The function `add` returns a value of type `N | undefined | undefined`.
|
|
64
64
|
*/
|
|
65
65
|
add(keyOrNode, value, count = 1) {
|
|
66
|
+
if (keyOrNode === null)
|
|
67
|
+
return undefined;
|
|
66
68
|
let inserted = undefined, newNode;
|
|
67
69
|
if (keyOrNode instanceof TreeMultisetNode) {
|
|
68
70
|
newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
|
|
69
71
|
}
|
|
70
|
-
else if (keyOrNode ===
|
|
71
|
-
newNode =
|
|
72
|
+
else if (keyOrNode === undefined) {
|
|
73
|
+
newNode = undefined;
|
|
72
74
|
}
|
|
73
75
|
else {
|
|
74
76
|
newNode = this.createNode(keyOrNode, value, count);
|
|
@@ -126,7 +128,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
126
128
|
}
|
|
127
129
|
}
|
|
128
130
|
else {
|
|
129
|
-
// TODO may need to support
|
|
131
|
+
// TODO may need to support undefined inserted
|
|
130
132
|
}
|
|
131
133
|
}
|
|
132
134
|
else {
|
|
@@ -140,8 +142,8 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
140
142
|
}
|
|
141
143
|
/**
|
|
142
144
|
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
143
|
-
* @param {N |
|
|
144
|
-
* the tree. It can be either a node object (`N`) or `
|
|
145
|
+
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be added to
|
|
146
|
+
* the tree. It can be either a node object (`N`) or `undefined`.
|
|
145
147
|
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
146
148
|
* be added as a child.
|
|
147
149
|
* @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
@@ -150,7 +152,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
150
152
|
if (parent) {
|
|
151
153
|
if (parent.left === undefined) {
|
|
152
154
|
parent.left = newNode;
|
|
153
|
-
if (newNode !==
|
|
155
|
+
if (newNode !== undefined) {
|
|
154
156
|
this._size = this.size + 1;
|
|
155
157
|
this._setCount(this.count + newNode.count);
|
|
156
158
|
}
|
|
@@ -158,7 +160,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
158
160
|
}
|
|
159
161
|
else if (parent.right === undefined) {
|
|
160
162
|
parent.right = newNode;
|
|
161
|
-
if (newNode !==
|
|
163
|
+
if (newNode !== undefined) {
|
|
162
164
|
this._size = this.size + 1;
|
|
163
165
|
this._setCount(this.count + newNode.count);
|
|
164
166
|
}
|
|
@@ -175,12 +177,12 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
175
177
|
/**
|
|
176
178
|
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
177
179
|
* inserted nodes.
|
|
178
|
-
* @param {(BTNKey |
|
|
180
|
+
* @param {(BTNKey | undefined)[] | (N | undefined)[]} keysOrNodes - An array of keys or nodes to be
|
|
179
181
|
* added to the multiset. Each element can be either a BTNKey or a TreeMultisetNode.
|
|
180
182
|
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
|
|
181
183
|
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
182
184
|
* each key or node.
|
|
183
|
-
* @returns The function `addMany` returns an array of `N`, `
|
|
185
|
+
* @returns The function `addMany` returns an array of `N`, `undefined`, or `undefined` values.
|
|
184
186
|
*/
|
|
185
187
|
addMany(keysOrNodes, data) {
|
|
186
188
|
const inserted = [];
|
|
@@ -190,7 +192,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
190
192
|
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
191
193
|
continue;
|
|
192
194
|
}
|
|
193
|
-
if (keyOrNode ===
|
|
195
|
+
if (keyOrNode === undefined) {
|
|
194
196
|
inserted.push(this.add(NaN, undefined, 0));
|
|
195
197
|
continue;
|
|
196
198
|
}
|
|
@@ -259,14 +261,15 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
259
261
|
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
260
262
|
*/
|
|
261
263
|
delete(identifier, callback = this.defaultOneParamCallback, ignoreCount = false) {
|
|
264
|
+
var _a;
|
|
262
265
|
const bstDeletedResult = [];
|
|
263
266
|
if (!this.root)
|
|
264
267
|
return bstDeletedResult;
|
|
265
|
-
const curr = this.getNode(identifier, callback);
|
|
268
|
+
const curr = (_a = this.getNode(identifier, callback)) !== null && _a !== void 0 ? _a : undefined;
|
|
266
269
|
if (!curr)
|
|
267
270
|
return bstDeletedResult;
|
|
268
|
-
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent :
|
|
269
|
-
let needBalanced =
|
|
271
|
+
const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : undefined;
|
|
272
|
+
let needBalanced = undefined, orgCurrent = curr;
|
|
270
273
|
if (curr.count > 1 && !ignoreCount) {
|
|
271
274
|
curr.count--;
|
|
272
275
|
this._setCount(this.count - 1);
|
|
@@ -289,7 +292,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
289
292
|
}
|
|
290
293
|
}
|
|
291
294
|
else {
|
|
292
|
-
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) :
|
|
295
|
+
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : undefined;
|
|
293
296
|
if (leftSubTreeRightMost) {
|
|
294
297
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
295
298
|
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
@@ -21,7 +21,7 @@ export declare enum FamilyPosition {
|
|
|
21
21
|
export type BTNKey = number;
|
|
22
22
|
export type BinaryTreeDeletedResult<N> = {
|
|
23
23
|
deleted: N | null | undefined;
|
|
24
|
-
needBalanced: N | null;
|
|
24
|
+
needBalanced: N | null | undefined;
|
|
25
25
|
};
|
|
26
26
|
export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
27
27
|
export type BinaryTreeOptions = {
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import { RBTreeNode } from '../../../data-structures';
|
|
2
|
+
import { BSTOptions } from "./bst";
|
|
1
3
|
export declare enum RBTNColor {
|
|
2
4
|
RED = 1,
|
|
3
5
|
BLACK = 0
|
|
4
6
|
}
|
|
7
|
+
export type RBTreeNodeNested<T> = RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
8
|
+
export type RBTreeOptions = BSTOptions & {};
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// import {BinaryTreeOptions} from './binary-tree';
|
|
3
|
-
// import {RBTreeNode} from '../../../data-structures';
|
|
4
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
3
|
exports.RBTNColor = void 0;
|
|
6
4
|
var RBTNColor;
|
|
@@ -8,6 +6,3 @@ var RBTNColor;
|
|
|
8
6
|
RBTNColor[RBTNColor["RED"] = 1] = "RED";
|
|
9
7
|
RBTNColor[RBTNColor["BLACK"] = 0] = "BLACK";
|
|
10
8
|
})(RBTNColor = exports.RBTNColor || (exports.RBTNColor = {}));
|
|
11
|
-
// export type RBTreeNodeNested<T> = RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
12
|
-
//
|
|
13
|
-
// export type RBTreeOptions = BinaryTreeOptions & {}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.42.
|
|
3
|
+
"version": "1.42.4",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -132,6 +132,6 @@
|
|
|
132
132
|
"typescript": "^4.9.5"
|
|
133
133
|
},
|
|
134
134
|
"dependencies": {
|
|
135
|
-
"data-structure-typed": "^1.42.
|
|
135
|
+
"data-structure-typed": "^1.42.4"
|
|
136
136
|
}
|
|
137
137
|
}
|
|
@@ -49,13 +49,14 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
49
49
|
/**
|
|
50
50
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
51
51
|
* a new node.
|
|
52
|
-
* @param {BTNKey | N |
|
|
52
|
+
* @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
53
53
|
* `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
54
54
|
* @param [value] - The `value` parameter is the value that you want to assign to the new node that you
|
|
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: BTNKey | N | null, value?: V): N |
|
|
58
|
+
override add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined {
|
|
59
|
+
if (keyOrNode === null) return undefined;
|
|
59
60
|
const inserted = super.add(keyOrNode, value);
|
|
60
61
|
if (inserted) this._balancePath(inserted);
|
|
61
62
|
return inserted;
|
|
@@ -226,7 +227,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
226
227
|
protected _balanceLR(A: N): void {
|
|
227
228
|
const parentOfA = A.parent;
|
|
228
229
|
const B = A.left;
|
|
229
|
-
let C =
|
|
230
|
+
let C = undefined;
|
|
230
231
|
if (B) {
|
|
231
232
|
C = B.right;
|
|
232
233
|
}
|
|
@@ -309,7 +310,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
309
310
|
protected _balanceRL(A: N): void {
|
|
310
311
|
const parentOfA = A.parent;
|
|
311
312
|
const B = A.right;
|
|
312
|
-
let C =
|
|
313
|
+
let C = undefined;
|
|
313
314
|
if (B) {
|
|
314
315
|
C = B.left;
|
|
315
316
|
}
|