Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 68 additions & 58 deletions Trees/BreadthFirstTreeTraversal.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,79 @@
/*
Breadth First Tree Traversal or level order traversal implementation in javascript
Author: @GerardUbuntu
*/
🌳 Breadth-First Traversal (BFT) in JavaScript
🚀 Overview

Breadth-First Traversal visits nodes level by level from top to bottom using a queue (FIFO).

🧠 Intuition
Start at the root
Visit all nodes at current level
Move to next level

👉 Example:

1
/ \
2 3
/ \ \
4 5 6

BFT Output:

1 → 2 → 3 → 4 → 5 → 6
💻 Implementation in JavaScript
function bft(root) {
if (!root) return;

let queue = [root];

while (queue.length > 0) {
let node = queue.shift();
console.log(node.val);

if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
}
🧪 Example Tree
class Node {
constructor(data) {
this.data = data
this.left = null
this.right = null
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}

class BinaryTree {
constructor() {
this.root = null
}
// Create tree
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);

breadthFirstIterative() {
const traversal = []
if (this.root) {
traversal.push(this.root)
}
for (let i = 0; i < traversal.length; i++) {
const currentNode = traversal[i]
if (currentNode.left) {
traversal.push(currentNode.left)
}
if (currentNode.right) {
traversal.push(currentNode.right)
}
traversal[i] = currentNode.data
}
return traversal
}
// Run
bft(root);
⚡ Optimized Version (Avoid shift)
function bftOptimized(root) {
if (!root) return;

breadthFirstRecursive() {
const traversal = []
const h = this.getHeight(this.root)
for (let i = 0; i !== h; i++) {
this.traverseLevel(this.root, i, traversal)
}
return traversal
}
let queue = [root];
let i = 0;

// Computing the height of the tree
getHeight(node) {
if (node === null) {
return 0
}
const lheight = this.getHeight(node.left)
const rheight = this.getHeight(node.right)
return lheight > rheight ? lheight + 1 : rheight + 1
}
while (i < queue.length) {
let node = queue[i++];
console.log(node.val);

traverseLevel(node, levelRemaining, traversal) {
if (node === null) {
return
}
if (levelRemaining === 0) {
traversal.push(node.data)
} else {
this.traverseLevel(node.left, levelRemaining - 1, traversal)
this.traverseLevel(node.right, levelRemaining - 1, traversal)
}
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
}

export { BinaryTree, Node }
⏱ Complexity
Time: O(n)
Space: O(n)
🎯 When to Use BFT
Level order traversal
Finding shortest path in trees
Problems like:
Binary Tree Level Order Traversal
Minimum depth of tree
Right/Left view of tree
Loading