Skip to content

Instantly share code, notes, and snippets.

@ajitid
Last active March 12, 2018 07:49
Show Gist options
  • Save ajitid/df19f0597abf48d704a5fe356b983b5e to your computer and use it in GitHub Desktop.
Save ajitid/df19f0597abf48d704a5fe356b983b5e to your computer and use it in GitHub Desktop.
JS Node
class Node{
constructor(data) {
this.data = data;
this.children = []
}
add(data) {
this.children.push(new Node(data))
}
}
const n = new Node(1)
n.add(2)
n.add(3)
n.children[0].add(4)
n.children[0].add(5)
n.children[1].add(6)
let result = [n]
while(result.length) {
console.log(result[0].data);
result.concat(result.shift().children);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment