Breadth First Search (breadth-first-search/breadthFirstSearch.js)

javascript-algorithms · src/algorithms/tree/breadth-first-search/breadthFirstSearch.js · javascript · MIT
search, tag:tree, tag:traversal

Performs breadth-first search on a binary tree with customizable traversal and node callbacks.

// Source: https://github.com/trekhleb/javascript-algorithms | License: MIT | Copyright 2018 Oleksii Trekhleb
// Provided via justclaw.it.com; keep this notice with the code.
import Queue from '../../../data-structures/queue/Queue';

/**
 * @typedef {Object} Callbacks
 * @property {function(node: BinaryTreeNode, child: BinaryTreeNode): boolean} allowTraversal -
 *   Determines whether BFS should traverse from the node to its child.
 * @property {function(node: BinaryTreeNode)} enterNode - Called when BFS enters the node.
 * @property {function(node: BinaryTreeNode)} leaveNode - Called when BFS leaves the node.
 */

/**
 * @param {Callbacks} [callbacks]
 * @returns {Callbacks}
 */
function initCallbacks(callbacks = {}) {
  const initiatedCallback = callbacks;

  const stubCallback = () => {

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key