Created
September 24, 2016 16:26
-
-
Save hbarcelos/26110bc9472cdd41e1a9dbbd4533c23f to your computer and use it in GitHub Desktop.
Tree iterators using ES6 generators
This file contains 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 BinaryTree { | |
constructor(value, left=null, right=null) { | |
this.value = value | |
this.left = left | |
this.right = right | |
} | |
} |
This file contains 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 InOrderIterator extends TreeIterator { | |
* [Symbol.iterator] () { | |
if (this.tree.left) | |
yield* new InOrderIterator(this.tree.left) | |
yield this.tree.value | |
if (this.tree.right) | |
yield* new InOrderIterator(this.tree.right) | |
} | |
} |
This file contains 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 PostOrderIterator extends TreeIterator { | |
* [Symbol.iterator] () { | |
if (this.tree.left) | |
yield* new PostOrderIterator(this.tree.left) | |
if (this.tree.right) | |
yield* new PostOrderIterator(this.tree.right) | |
yield this.tree.value | |
} | |
} |
This file contains 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 PreOrderIterator extends TreeIterator { | |
* [Symbol.iterator] () { | |
yield this.tree.value | |
if (this.tree.left) | |
yield* new PreOrderIterator(this.tree.left) | |
if (this.tree.right) | |
yield* new PreOrderIterator(this.tree.right) | |
} | |
} |
This file contains 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
const tree = new BinaryTree('a', | |
new BinaryTree('b', | |
new BinaryTree('c'), | |
new BinaryTree('d')), | |
new BinaryTree('e')); | |
const preOrderIt = new PreOrderIterator(tree) | |
const inOrderIt = new InOrderIterator(tree) | |
const postOrderIt = new PostOrderIterator(tree) | |
console.log([...preOrderIt]) // [ 'a', 'b', 'c', 'd', 'e' ] | |
console.log([...inOrderIt]) // [ 'c', 'b', 'd', 'a', 'e' ] | |
console.log([...postOrderIt]) // [ 'c', 'd', 'b', 'e', 'a' ] |
This file contains 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 TreeIterator { | |
constructor(tree) { | |
this.tree = tree | |
} | |
* [Symbol.iterator] () { | |
throw new Error('Not implemented') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment