# 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;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/breadth-first-search.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
