Last active
March 12, 2018 07:49
-
-
Save ajitid/df19f0597abf48d704a5fe356b983b5e to your computer and use it in GitHub Desktop.
JS Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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