Skip to content

Instantly share code, notes, and snippets.

@thmain
Created December 25, 2022 05:30
Show Gist options
  • Select an option

  • Save thmain/f7dbdf07e5e584c0406b3c138f21f54a to your computer and use it in GitHub Desktop.

Select an option

Save thmain/f7dbdf07e5e584c0406b3c138f21f54a to your computer and use it in GitHub Desktop.
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