-
-
Save deanveloper/32d6c0c9b915f464cb917d5f76c34dd9 to your computer and use it in GitHub Desktop.
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 Tree { | |
constructor(arr) { | |
if (arr.length == 0) { | |
return | |
} | |
this.value = arr[0]; | |
const left = new Tree(arr.slice(1, arr.length / 2 + 1)); | |
const right = new Tree(arr.slice(arr.length / 2 + 1, arr.length)); | |
if (left.value !== undefined) { | |
this.left = left; | |
} | |
if (right.value !== undefined) { | |
this.right = right; | |
} | |
} | |
*inOrderIter() { | |
if (this.left !== undefined) { | |
yield* this.left.inOrderIter(); | |
} | |
yield this.value; | |
if (this.right !== undefined) { | |
yield* this.right.inOrderIter(); | |
} | |
} | |
} | |
const arr = []; | |
for (let i = 0; i < 10000; i++) { | |
arr[i] = i; | |
} | |
const tree = new Tree(arr); | |
const start = new Date(); | |
for (const i of tree.inOrderIter()) { | |
const _ = i; | |
} | |
console.log(new Date().getMilliseconds() - start.getMilliseconds()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment