min-heap-typed 1.40.0 → 1.41.0
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/binary-tree.d.ts +8 -0
- package/dist/data-structures/binary-tree/binary-tree.js +22 -2
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +1 -1
- package/dist/data-structures/binary-tree/rb-tree.d.ts +95 -9
- package/dist/data-structures/binary-tree/rb-tree.js +379 -12
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -7
- package/dist/types/data-structures/binary-tree/rb-tree.js +11 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +23 -1
- package/src/data-structures/binary-tree/bst.ts +4 -4
- package/src/data-structures/binary-tree/rb-tree.ts +418 -350
- package/src/types/data-structures/binary-tree/rb-tree.ts +6 -6
|
@@ -950,7 +950,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
950
950
|
if (iterationType === IterationType.RECURSIVE) {
|
|
951
951
|
const queue = new Queue<N>([beginRoot]);
|
|
952
952
|
|
|
953
|
-
|
|
953
|
+
const traverse = (level: number) => {
|
|
954
954
|
if (queue.size === 0) return;
|
|
955
955
|
|
|
956
956
|
const current = queue.shift()!;
|
|
@@ -1048,6 +1048,26 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1048
1048
|
}
|
|
1049
1049
|
}
|
|
1050
1050
|
|
|
1051
|
+
/**
|
|
1052
|
+
* The function `getSuccessor` returns the next node in a binary tree given a node `x`, or `null` if
|
|
1053
|
+
* `x` is the last node.
|
|
1054
|
+
* @param {N} x - N - a node in a binary tree
|
|
1055
|
+
* @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
|
|
1056
|
+
* if there is no successor, or `undefined` if the input `x` is `undefined`.
|
|
1057
|
+
*/
|
|
1058
|
+
getSuccessor(x: N): N | null | undefined{
|
|
1059
|
+
if (x.right) {
|
|
1060
|
+
return this.getLeftMost(x.right);
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
let y: N | null | undefined = x.parent;
|
|
1064
|
+
while (y && y && x === y.right) {
|
|
1065
|
+
x = y;
|
|
1066
|
+
y = y.parent;
|
|
1067
|
+
}
|
|
1068
|
+
return y;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1051
1071
|
// --- start additional methods ---
|
|
1052
1072
|
|
|
1053
1073
|
/**
|
|
@@ -1180,10 +1200,12 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1180
1200
|
}
|
|
1181
1201
|
} else {
|
|
1182
1202
|
if (node.left) {
|
|
1203
|
+
// @ts-ignore
|
|
1183
1204
|
yield* this[Symbol.iterator](node.left);
|
|
1184
1205
|
}
|
|
1185
1206
|
yield node.key;
|
|
1186
1207
|
if (node.right) {
|
|
1208
|
+
// @ts-ignore
|
|
1187
1209
|
yield* this[Symbol.iterator](node.right);
|
|
1188
1210
|
}
|
|
1189
1211
|
}
|
|
@@ -127,7 +127,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
127
127
|
/**
|
|
128
128
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
129
129
|
* maintaining balance.
|
|
130
|
-
* @param {[BTNKey | N,
|
|
130
|
+
* @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
131
131
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
132
132
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
133
133
|
* `null
|
|
@@ -153,15 +153,15 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
153
153
|
return super.addMany(keysOrNodes, data);
|
|
154
154
|
}
|
|
155
155
|
const inserted: (N | null | undefined)[] = [];
|
|
156
|
-
const combinedArr: [BTNKey | N,
|
|
156
|
+
const combinedArr: [BTNKey | N, V][] = keysOrNodes.map((value:(BTNKey | N), index) => [value, data?.[index]] as [BTNKey | N, V]);
|
|
157
157
|
let sorted = [];
|
|
158
158
|
|
|
159
|
-
function isNodeOrNullTuple(arr: [BTNKey | N,
|
|
159
|
+
function isNodeOrNullTuple(arr: [BTNKey | N, V][]): arr is [N, V][] {
|
|
160
160
|
for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
|
|
161
161
|
return false;
|
|
162
162
|
}
|
|
163
163
|
|
|
164
|
-
function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N,
|
|
164
|
+
function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N, V][]): arr is [BTNKey, V][] {
|
|
165
165
|
for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
|
|
166
166
|
return false;
|
|
167
167
|
}
|