> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/breadth-first-search.md).

# Breadth First Search

![](/files/p3kr8AvjL1mENqCMOJ6t)

* V + E, V

```jsx
// Do not edit the class below except
// for the breadthFirstSearch method.
// Feel free to add new properties
// and methods to the class.
class Node {
  constructor(name) {
    this.name = name;
    this.children = [];
  }

  addChild(name) {
    this.children.push(new Node(name));
    return this;
  }

  breadthFirstSearch(arr) {
    const q = [this];
    while (q.length > 0) {
      const node = q.shift();

      for (const child of node.children) {
        q.push(child);
      }

      arr.push(node.name);
      node.visited = true;
    }
    
    return arr;
  }
}

// Do not edit the line below.
exports.Node = Node;
```
