Skip to content

Instantly share code, notes, and snippets.

@almost almost/tree-iterator.js
Last active Nov 22, 2018

Embed
What would you like to do?
class Tree {
constructor(value, left, right) {
this.left = left;
this.value = value;
this.right = right;
}
}
function* iterateTree(tree) {
// ?????
}
var myTree = new Tree(
1,
new Tree(2, new Tree(3), new Tree(4)),
new Tree(5, new Tree(6), new Tree(7))
);
for (let value of iterateTree(myTree)) {
console.log(value);
}
// Desired output:
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// // Bonus question: Can you make this work?:
// //
// for(let value of myTree) {
// console.log(value);
// }
// //
// // Hint1: The Symbol.iterator property of a class is meant to return an iterator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.