Created
December 25, 2022 05:30
-
-
Save thmain/f7dbdf07e5e584c0406b3c138f21f54a to your computer and use it in GitHub Desktop.
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
| var BST = new BinarySearchTree() | |
| BST.insertNode(8) | |
| BST.insertNode(3) | |
| BST.insertNode(10) | |
| BST.insertNode(1) | |
| BST.insertNode(6) | |
| BST.insertNode(14) | |
| BST.insertNode(4) | |
| BST.insertNode(7) | |
| BST.insertNode(13) | |
| function printTreeInLevelOrder (BST) { | |
| var temp, queue = [] | |
| queue.push(BST.root) | |
| while (queue.length) { | |
| // Deque the Queue | |
| temp = queue.splice(0, 1)[0] | |
| console.log(temp.data) | |
| // Enqueue the Queue | |
| if (temp.left) queue.push(temp.left) | |
| if (temp.right) queue.push(temp.right) | |
| } | |
| } | |
| printTreeInLevelOrder(BST) // 8, 3, 10, 1, 6, 14, 4, 7, 13 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment