MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / Node

Class Node

graphs/breadth-first-search.js:5–27  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3// BREADTH-FIRST SEARCH
4
5class Node {
6 constructor(name) {
7 this.name = name;
8 this.children = [];
9 }
10
11 addChild(name) {
12 this.children.push(new Node(name));
13 return this;
14 }
15
16 breadthFirstSearch(array) {
17 const queue = [this];
18 while (queue.length > 0) {
19 const curretNode = queue.shift();
20 array.push(curretNode.name);
21 for (let child of curretNode.children) {
22 queue.push(child);
23 }
24 }
25 return array;
26 }
27}
28
29const graph = new Node("A");
30graph.addChild("B").addChild("C").addChild("D");

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected