# Breadth First Search

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-4636c7eab2254b858d387f359527fad1d5c83b08%2FScreenshot%202023-01-23%20at%2021.07.57.png?alt=media)

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