Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tree-multiset-typed

Package Overview
Dependencies
Maintainers
1
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tree-multiset-typed - npm Package Compare versions

Comparing version 1.41.9 to 1.42.0

73

dist/data-structures/binary-tree/binary-tree.d.ts

@@ -241,64 +241,15 @@ /**

isBST(iterationType?: IterationType): boolean;
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
subTreeTraverse<C extends BTNCallback<N | null>>(callback?: C, beginRoot?: BTNKey | N | null, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
dfs<C extends BTNCallback<N | null>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
bfs<C extends BTNCallback<N | null>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][];
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[][];
listLevels<C extends BTNCallback<N | null>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
/**
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
* node, either recursively or iteratively.
* @param callback - The `callback` parameter is a function that will be called on each node in the
* subtree traversal. It takes a single argument, which is the current node being traversed, and
* returns a value. The return values from each callback invocation will be collected and returned as
* an array.
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
* start from the root of the tree.
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
* performed on the binary tree. It can have two possible values:
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
*/
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null, iterationType?: IterationType): ReturnType<C>[];
/**
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
* function on each node according to a specified order pattern.
* @param callback - The `callback` parameter is a function that will be called on each node during
* the depth-first search traversal. It takes a node as input and returns a value. The default value
* is `this.defaultOneParamCallback`, which is a callback function defined elsewhere in the code.
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
* search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
* is `null`, an empty array will be returned.
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
* iteration used in the depth-first search algorithm. It can have two possible values:
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
*/
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
/**
* The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
* function on each node.
* @param callback - The `callback` parameter is a function that will be called for each node in the
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this.defaultOneParamCallback
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
* will not be performed and an empty array will be returned.
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
* in the breadth-first search (BFS) algorithm. It can have two possible values:
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
*/
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
/**
* The `listLevels` function takes a binary tree node and a callback function, and returns an array
* of arrays representing the levels of the tree.
* @param {C} callback - The `callback` parameter is a function that will be called on each node in
* the tree. It takes a node as input and returns a value. The return type of the callback function
* is determined by the generic type `C`.
* @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
* traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
* from the root node of the binary tree.
* @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
* recursively or iteratively. It can have two possible values:
* @returns The function `listLevels` returns an array of arrays, where each inner array represents a
* level in a binary tree. Each inner array contains the return type of the provided callback
* function `C` applied to the nodes at that level.
*/
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[][];
/**
* The function returns the predecessor node of a given node in a binary tree.

@@ -305,0 +256,0 @@ * @param {N} node - The parameter "node" represents a node in a binary tree.

187

dist/data-structures/binary-tree/binary-tree.js

@@ -706,5 +706,6 @@ "use strict";

* performed on the binary tree. It can have two possible values:
* @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
*/
subTreeTraverse(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
subTreeTraverse(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
if (typeof beginRoot === 'number')

@@ -717,5 +718,13 @@ beginRoot = this.getNode(beginRoot);

const _traverse = (cur) => {
ans.push(callback(cur));
cur.left && _traverse(cur.left);
cur.right && _traverse(cur.right);
if (cur !== undefined) {
ans.push(callback(cur));
if (includeNull) {
cur !== null && cur.left !== undefined && _traverse(cur.left);
cur !== null && cur.right !== undefined && _traverse(cur.right);
}
else {
cur !== null && cur.left && _traverse(cur.left);
cur !== null && cur.right && _traverse(cur.right);
}
}
};

@@ -728,5 +737,13 @@ _traverse(beginRoot);

const cur = stack.pop();
ans.push(callback(cur));
cur.right && stack.push(cur.right);
cur.left && stack.push(cur.left);
if (cur !== undefined) {
ans.push(callback(cur));
if (includeNull) {
cur !== null && cur.right !== undefined && stack.push(cur.right);
cur !== null && cur.left !== undefined && stack.push(cur.left);
}
else {
cur !== null && cur.right && stack.push(cur.right);
cur !== null && cur.left && stack.push(cur.left);
}
}
}

@@ -749,5 +766,6 @@ }

* iteration used in the depth-first search algorithm. It can have two possible values:
* @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
*/
dfs(callback = this.defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
dfs(callback = this.defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE, includeNull = false) {
if (!beginRoot)

@@ -760,21 +778,48 @@ return [];

case 'in':
if (node.left)
_traverse(node.left);
ans.push(callback(node));
if (node.right)
_traverse(node.right);
if (includeNull) {
if (node !== null && node.left !== undefined)
_traverse(node.left);
ans.push(callback(node));
if (node !== null && node.right !== undefined)
_traverse(node.right);
}
else {
if (node !== null && node.left)
_traverse(node.left);
ans.push(callback(node));
if (node !== null && node.right)
_traverse(node.right);
}
break;
case 'pre':
ans.push(callback(node));
if (node.left)
_traverse(node.left);
if (node.right)
_traverse(node.right);
if (includeNull) {
ans.push(callback(node));
if (node !== null && node.left !== undefined)
_traverse(node.left);
if (node !== null && node.right !== undefined)
_traverse(node.right);
}
else {
ans.push(callback(node));
if (node !== null && node.left)
_traverse(node.left);
if (node !== null && node.right)
_traverse(node.right);
}
break;
case 'post':
if (node.left)
_traverse(node.left);
if (node.right)
_traverse(node.right);
ans.push(callback(node));
if (includeNull) {
if (node !== null && node.left !== undefined)
_traverse(node.left);
if (node !== null && node.right !== undefined)
_traverse(node.right);
ans.push(callback(node));
}
else {
if (node !== null && node.left)
_traverse(node.left);
if (node !== null && node.right)
_traverse(node.right);
ans.push(callback(node));
}
break;

@@ -790,4 +835,12 @@ }

const cur = stack.pop();
if (!cur || !cur.node)
if (cur === undefined)
continue;
if (includeNull) {
if (cur.node === undefined)
continue;
}
else {
if (cur.node === null || cur.node === undefined)
continue;
}
if (cur.opt === 1) {

@@ -799,9 +852,9 @@ ans.push(callback(cur.node));

case 'in':
stack.push({ opt: 0, node: cur.node.right });
cur.node && stack.push({ opt: 0, node: cur.node.right });
stack.push({ opt: 1, node: cur.node });
stack.push({ opt: 0, node: cur.node.left });
cur.node && stack.push({ opt: 0, node: cur.node.left });
break;
case 'pre':
stack.push({ opt: 0, node: cur.node.right });
stack.push({ opt: 0, node: cur.node.left });
cur.node && stack.push({ opt: 0, node: cur.node.right });
cur.node && stack.push({ opt: 0, node: cur.node.left });
stack.push({ opt: 1, node: cur.node });

@@ -811,9 +864,9 @@ break;

stack.push({ opt: 1, node: cur.node });
stack.push({ opt: 0, node: cur.node.right });
stack.push({ opt: 0, node: cur.node.left });
cur.node && stack.push({ opt: 0, node: cur.node.right });
cur.node && stack.push({ opt: 0, node: cur.node.left });
break;
default:
stack.push({ opt: 0, node: cur.node.right });
cur.node && stack.push({ opt: 0, node: cur.node.right });
stack.push({ opt: 1, node: cur.node });
stack.push({ opt: 0, node: cur.node.left });
cur.node && stack.push({ opt: 0, node: cur.node.left });
break;

@@ -837,5 +890,6 @@ }

* in the breadth-first search (BFS) algorithm. It can have two possible values:
* @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
*/
bfs(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
bfs(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
if (!beginRoot)

@@ -851,6 +905,14 @@ return [];

ans.push(callback(current));
if (current.left)
queue.push(current.left);
if (current.right)
queue.push(current.right);
if (includeNull) {
if (current && current.left !== undefined)
queue.push(current.left);
if (current && current.right !== undefined)
queue.push(current.right);
}
else {
if (current.left)
queue.push(current.left);
if (current.right)
queue.push(current.right);
}
traverse(level + 1);

@@ -867,6 +929,14 @@ };

ans.push(callback(current));
if (current.left)
queue.push(current.left);
if (current.right)
queue.push(current.right);
if (includeNull) {
if (current !== null && current.left !== undefined)
queue.push(current.left);
if (current !== null && current.right !== undefined)
queue.push(current.right);
}
else {
if (current.left)
queue.push(current.left);
if (current.right)
queue.push(current.right);
}
}

@@ -888,2 +958,3 @@ }

* recursively or iteratively. It can have two possible values:
* @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `listLevels` returns an array of arrays, where each inner array represents a

@@ -893,3 +964,3 @@ * level in a binary tree. Each inner array contains the return type of the provided callback

*/
listLevels(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
listLevels(callback = this.defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
if (!beginRoot)

@@ -903,6 +974,14 @@ return [];

levelsNodes[level].push(callback(node));
if (node.left)
_recursive(node.left, level + 1);
if (node.right)
_recursive(node.right, level + 1);
if (includeNull) {
if (node && node.left !== undefined)
_recursive(node.left, level + 1);
if (node && node.right !== undefined)
_recursive(node.right, level + 1);
}
else {
if (node && node.left)
_recursive(node.left, level + 1);
if (node && node.right)
_recursive(node.right, level + 1);
}
};

@@ -919,6 +998,14 @@ _recursive(beginRoot, 0);

levelsNodes[level].push(callback(node));
if (node.right)
stack.push([node.right, level + 1]);
if (node.left)
stack.push([node.left, level + 1]);
if (includeNull) {
if (node && node.right !== undefined)
stack.push([node.right, level + 1]);
if (node && node.left !== undefined)
stack.push([node.left, level + 1]);
}
else {
if (node && node.right)
stack.push([node.right, level + 1]);
if (node && node.left)
stack.push([node.left, level + 1]);
}
}

@@ -925,0 +1012,0 @@ }

{
"name": "tree-multiset-typed",
"version": "1.41.9",
"version": "1.42.0",
"description": "Tree Multiset, AVLTree, BST, Binary Tree. Javascript & Typescript Data Structure.",

@@ -182,4 +182,4 @@ "main": "dist/index.js",

"dependencies": {
"data-structure-typed": "^1.41.9"
"data-structure-typed": "^1.42.0"
}
}

@@ -111,4 +111,3 @@ /**

export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
implements IBinaryTree<V, N>
{
implements IBinaryTree<V, N> {
iterationType: IterationType = IterationType.ITERATIVE;

@@ -395,3 +394,3 @@

const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
let maxHeight = 0;

@@ -851,2 +850,23 @@

subTreeTraverse<C extends BTNCallback<N>>(
callback?: C,
beginRoot?: BTNKey | N | null,
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[]
subTreeTraverse<C extends BTNCallback<N>>(
callback?: C,
beginRoot?: BTNKey | N | null,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType<C>[]
subTreeTraverse<C extends BTNCallback<N | null>>(
callback?: C,
beginRoot?: BTNKey | N | null,
iterationType?: IterationType,
includeNull?: true
): ReturnType<C>[]
/**

@@ -864,19 +884,28 @@ * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each

* performed on the binary tree. It can have two possible values:
* @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
*/
subTreeTraverse<C extends BTNCallback<N>>(
subTreeTraverse<C extends BTNCallback<N | null>>(
callback: C = this.defaultOneParamCallback as C,
beginRoot: BTNKey | N | null = this.root,
iterationType = this.iterationType
iterationType = this.iterationType,
includeNull = false
): ReturnType<C>[] {
if (typeof beginRoot === 'number') beginRoot = this.getNode(beginRoot);
const ans: ReturnType<BTNCallback<N>>[] = [];
const ans: (ReturnType<BTNCallback<N>> | null)[] = [];
if (!beginRoot) return ans;
if (iterationType === IterationType.RECURSIVE) {
const _traverse = (cur: N) => {
ans.push(callback(cur));
cur.left && _traverse(cur.left);
cur.right && _traverse(cur.right);
const _traverse = (cur: N | null) => {
if (cur !== undefined) {
ans.push(callback(cur));
if (includeNull) {
cur !== null && cur.left !== undefined && _traverse(cur.left);
cur !== null && cur.right !== undefined && _traverse(cur.right);
} else {
cur !== null && cur.left && _traverse(cur.left);
cur !== null && cur.right && _traverse(cur.right);
}
}
};

@@ -886,10 +915,16 @@

} else {
const stack: N[] = [beginRoot];
const stack: (N| null)[] = [beginRoot];
while (stack.length > 0) {
const cur = stack.pop()!;
ans.push(callback(cur));
cur.right && stack.push(cur.right);
cur.left && stack.push(cur.left);
const cur = stack.pop();
if (cur !== undefined) {
ans.push(callback(cur));
if (includeNull) {
cur !== null && cur.right !== undefined && stack.push(cur.right);
cur !== null && cur.left !== undefined && stack.push(cur.left);
} else {
cur !== null && cur.right && stack.push(cur.right);
cur !== null && cur.left && stack.push(cur.left);
}
}
}

@@ -900,2 +935,26 @@ }

dfs<C extends BTNCallback<N>>(
callback?: C,
pattern?: DFSOrderPattern,
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[]
dfs<C extends BTNCallback<N>>(
callback?: C,
pattern?: DFSOrderPattern,
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType<C>[]
dfs<C extends BTNCallback<N | null>>(
callback?: C,
pattern?: DFSOrderPattern,
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: true
): ReturnType<C>[]
/**

@@ -914,30 +973,49 @@ * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback

* iteration used in the depth-first search algorithm. It can have two possible values:
* @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
*/
dfs<C extends BTNCallback<N>>(
dfs<C extends BTNCallback<N | null>>(
callback: C = this.defaultOneParamCallback as C,
pattern: DFSOrderPattern = 'in',
beginRoot: N | null = this.root,
iterationType: IterationType = IterationType.ITERATIVE
iterationType: IterationType = IterationType.ITERATIVE,
includeNull = false
): ReturnType<C>[] {
if (!beginRoot) return [];
const ans: ReturnType<BTNCallback<N>>[] = [];
const ans: ReturnType<C>[] = [];
if (iterationType === IterationType.RECURSIVE) {
const _traverse = (node: N) => {
const _traverse = (node: N | null) => {
switch (pattern) {
case 'in':
if (node.left) _traverse(node.left);
ans.push(callback(node));
if (node.right) _traverse(node.right);
if (includeNull) {
if (node !== null && node.left !== undefined) _traverse(node.left);
ans.push(callback(node));
if (node !== null && node.right !== undefined) _traverse(node.right);
} else {
if (node !== null && node.left) _traverse(node.left);
ans.push(callback(node));
if (node !== null && node.right) _traverse(node.right);
}
break;
case 'pre':
ans.push(callback(node));
if (node.left) _traverse(node.left);
if (node.right) _traverse(node.right);
if (includeNull) {
ans.push(callback(node));
if (node !== null && node.left !== undefined) _traverse(node.left);
if (node !== null && node.right !== undefined) _traverse(node.right);
} else {
ans.push(callback(node));
if (node !== null && node.left) _traverse(node.left);
if (node !== null && node.right) _traverse(node.right);
}
break;
case 'post':
if (node.left) _traverse(node.left);
if (node.right) _traverse(node.right);
ans.push(callback(node));
if (includeNull) {
if (node !== null && node.left !== undefined) _traverse(node.left);
if (node !== null && node.right !== undefined) _traverse(node.right);
ans.push(callback(node));
} else {
if (node !== null && node.left) _traverse(node.left);
if (node !== null && node.right) _traverse(node.right);
ans.push(callback(node));
}

@@ -951,7 +1029,12 @@ break;

// 0: visit, 1: print
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
while (stack.length > 0) {
const cur = stack.pop();
if (!cur || !cur.node) continue;
if (cur === undefined) continue;
if (includeNull) {
if (cur.node === undefined) continue;
} else {
if (cur.node === null || cur.node === undefined) continue;
}
if (cur.opt === 1) {

@@ -962,9 +1045,9 @@ ans.push(callback(cur.node));

case 'in':
stack.push({opt: 0, node: cur.node.right});
cur.node && stack.push({opt: 0, node: cur.node.right});
stack.push({opt: 1, node: cur.node});
stack.push({opt: 0, node: cur.node.left});
cur.node && stack.push({opt: 0, node: cur.node.left});
break;
case 'pre':
stack.push({opt: 0, node: cur.node.right});
stack.push({opt: 0, node: cur.node.left});
cur.node && stack.push({opt: 0, node: cur.node.right});
cur.node && stack.push({opt: 0, node: cur.node.left});
stack.push({opt: 1, node: cur.node});

@@ -974,9 +1057,9 @@ break;

stack.push({opt: 1, node: cur.node});
stack.push({opt: 0, node: cur.node.right});
stack.push({opt: 0, node: cur.node.left});
cur.node && stack.push({opt: 0, node: cur.node.right});
cur.node && stack.push({opt: 0, node: cur.node.left});
break;
default:
stack.push({opt: 0, node: cur.node.right});
cur.node && stack.push({opt: 0, node: cur.node.right});
stack.push({opt: 1, node: cur.node});
stack.push({opt: 0, node: cur.node.left});
cur.node && stack.push({opt: 0, node: cur.node.left});
break;

@@ -991,2 +1074,23 @@ }

bfs<C extends BTNCallback<N>>(
callback?: C,
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[]
bfs<C extends BTNCallback<N>>(
callback?: C,
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType<C>[]
bfs<C extends BTNCallback<N | null>>(
callback?: C,
beginRoot?: N | null,
iterationType?: IterationType,
includeNull?: true
): ReturnType<C>[]
/**

@@ -1003,8 +1107,10 @@ * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback

* in the breadth-first search (BFS) algorithm. It can have two possible values:
* @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
*/
bfs<C extends BTNCallback<N>>(
bfs<C extends BTNCallback<N | null>>(
callback: C = this.defaultOneParamCallback as C,
beginRoot: N | null = this.root,
iterationType = this.iterationType
iterationType = this.iterationType,
includeNull = false
): ReturnType<C>[] {

@@ -1016,3 +1122,3 @@ if (!beginRoot) return [];

if (iterationType === IterationType.RECURSIVE) {
const queue = new Queue<N>([beginRoot]);
const queue: Queue<N | null> = new Queue<N | null>([beginRoot]);

@@ -1025,5 +1131,11 @@ const traverse = (level: number) => {

if (current.left) queue.push(current.left);
if (current.right) queue.push(current.right);
if (includeNull) {
if (current && current.left !== undefined) queue.push(current.left);
if (current && current.right !== undefined) queue.push(current.right);
} else {
if (current.left) queue.push(current.left);
if (current.right) queue.push(current.right);
}
traverse(level + 1);

@@ -1034,3 +1146,3 @@ };

} else {
const queue = new Queue<N>([beginRoot]);
const queue = new Queue<N | null>([beginRoot]);
while (queue.size > 0) {

@@ -1043,4 +1155,10 @@ const levelSize = queue.size;

if (current.left) queue.push(current.left);
if (current.right) queue.push(current.right);
if (includeNull) {
if (current !== null && current.left !== undefined) queue.push(current.left);
if (current !== null && current.right !== undefined) queue.push(current.right);
} else {
if (current.left) queue.push(current.left);
if (current.right) queue.push(current.right);
}
}

@@ -1052,2 +1170,23 @@ }

listLevels<C extends BTNCallback<N>>(
callback?: C ,
beginRoot?: N | null ,
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[][]
listLevels<C extends BTNCallback<N>>(
callback?: C ,
beginRoot?: N | null ,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType<C>[][]
listLevels<C extends BTNCallback<N | null>>(
callback?: C ,
beginRoot?: N | null ,
iterationType?: IterationType,
includeNull?: true
): ReturnType<C>[][]
/**

@@ -1064,2 +1203,3 @@ * The `listLevels` function takes a binary tree node and a callback function, and returns an array

* recursively or iteratively. It can have two possible values:
* @param includeNull - The choice to output null values during binary tree traversal should be provided.
* @returns The function `listLevels` returns an array of arrays, where each inner array represents a

@@ -1069,6 +1209,7 @@ * level in a binary tree. Each inner array contains the return type of the provided callback

*/
listLevels<C extends BTNCallback<N>>(
listLevels<C extends BTNCallback<N | null>>(
callback: C = this.defaultOneParamCallback as C,
beginRoot: N | null = this.root,
iterationType = this.iterationType
iterationType = this.iterationType,
includeNull = false
): ReturnType<C>[][] {

@@ -1079,7 +1220,12 @@ if (!beginRoot) return [];

if (iterationType === IterationType.RECURSIVE) {
const _recursive = (node: N, level: number) => {
const _recursive = (node: N | null, level: number) => {
if (!levelsNodes[level]) levelsNodes[level] = [];
levelsNodes[level].push(callback(node));
if (node.left) _recursive(node.left, level + 1);
if (node.right) _recursive(node.right, level + 1);
if (includeNull) {
if (node && node.left !== undefined) _recursive(node.left, level + 1);
if (node && node.right !== undefined) _recursive(node.right, level + 1);
} else {
if (node && node.left) _recursive(node.left, level + 1);
if (node && node.right) _recursive(node.right, level + 1);
}
};

@@ -1089,3 +1235,3 @@

} else {
const stack: [N, number][] = [[beginRoot, 0]];
const stack: [N | null, number][] = [[beginRoot, 0]];

@@ -1098,4 +1244,10 @@ while (stack.length > 0) {

levelsNodes[level].push(callback(node));
if (node.right) stack.push([node.right, level + 1]);
if (node.left) stack.push([node.left, level + 1]);
if (includeNull) {
if (node && node.right !== undefined) stack.push([node.right, level + 1]);
if (node && node.left !== undefined) stack.push([node.left, level + 1]);
} else {
if (node && node.right) stack.push([node.right, level + 1]);
if (node && node.left) stack.push([node.left, level + 1]);
}
}

@@ -1256,3 +1408,3 @@ }

*/
*[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
* [Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
if (!node) {

@@ -1259,0 +1411,0 @@ return;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc