Skip to content

Commit 27b9c04

Browse files
Refactor Breadth-First Tree Traversal implementation
Updated the Breadth-First Tree Traversal implementation with improved structure and added comments for clarity.
1 parent 5c39e87 commit 27b9c04

1 file changed

Lines changed: 68 additions & 58 deletions

File tree

Trees/BreadthFirstTreeTraversal.js

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,79 @@
1-
/*
2-
Breadth First Tree Traversal or level order traversal implementation in javascript
3-
Author: @GerardUbuntu
4-
*/
1+
🌳 Breadth-First Traversal (BFT) in JavaScript
2+
🚀 Overview
53

4+
Breadth-First Traversal visits nodes level by level from top to bottom using a queue (FIFO).
5+
6+
🧠 Intuition
7+
Start at the root
8+
Visit all nodes at current level
9+
Move to next level
10+
11+
👉 Example:
12+
13+
1
14+
/ \
15+
2 3
16+
/ \ \
17+
4 5 6
18+
19+
BFT Output:
20+
21+
1 2 3 4 5 6
22+
💻 Implementation in JavaScript
23+
function bft(root) {
24+
if (!root) return;
25+
26+
let queue = [root];
27+
28+
while (queue.length > 0) {
29+
let node = queue.shift();
30+
console.log(node.val);
31+
32+
if (node.left) queue.push(node.left);
33+
if (node.right) queue.push(node.right);
34+
}
35+
}
36+
🧪 Example Tree
637
class Node {
7-
constructor(data) {
8-
this.data = data
9-
this.left = null
10-
this.right = null
38+
constructor(val) {
39+
this.val = val;
40+
this.left = null;
41+
this.right = null;
1142
}
1243
}
1344

14-
class BinaryTree {
15-
constructor() {
16-
this.root = null
17-
}
45+
// Create tree
46+
let root = new Node(1);
47+
root.left = new Node(2);
48+
root.right = new Node(3);
49+
root.left.left = new Node(4);
50+
root.left.right = new Node(5);
51+
root.right.right = new Node(6);
1852

19-
breadthFirstIterative() {
20-
const traversal = []
21-
if (this.root) {
22-
traversal.push(this.root)
23-
}
24-
for (let i = 0; i < traversal.length; i++) {
25-
const currentNode = traversal[i]
26-
if (currentNode.left) {
27-
traversal.push(currentNode.left)
28-
}
29-
if (currentNode.right) {
30-
traversal.push(currentNode.right)
31-
}
32-
traversal[i] = currentNode.data
33-
}
34-
return traversal
35-
}
53+
// Run
54+
bft(root);
55+
Optimized Version (Avoid shift)
56+
function bftOptimized(root) {
57+
if (!root) return;
3658

37-
breadthFirstRecursive() {
38-
const traversal = []
39-
const h = this.getHeight(this.root)
40-
for (let i = 0; i !== h; i++) {
41-
this.traverseLevel(this.root, i, traversal)
42-
}
43-
return traversal
44-
}
59+
let queue = [root];
60+
let i = 0;
4561

46-
// Computing the height of the tree
47-
getHeight(node) {
48-
if (node === null) {
49-
return 0
50-
}
51-
const lheight = this.getHeight(node.left)
52-
const rheight = this.getHeight(node.right)
53-
return lheight > rheight ? lheight + 1 : rheight + 1
54-
}
62+
while (i < queue.length) {
63+
let node = queue[i++];
64+
console.log(node.val);
5565

56-
traverseLevel(node, levelRemaining, traversal) {
57-
if (node === null) {
58-
return
59-
}
60-
if (levelRemaining === 0) {
61-
traversal.push(node.data)
62-
} else {
63-
this.traverseLevel(node.left, levelRemaining - 1, traversal)
64-
this.traverseLevel(node.right, levelRemaining - 1, traversal)
65-
}
66+
if (node.left) queue.push(node.left);
67+
if (node.right) queue.push(node.right);
6668
}
6769
}
68-
69-
export { BinaryTree, Node }
70+
Complexity
71+
Time: O(n)
72+
Space: O(n)
73+
🎯 When to Use BFT
74+
Level order traversal
75+
Finding shortest path in trees
76+
Problems like:
77+
Binary Tree Level Order Traversal
78+
Minimum depth of tree
79+
Right/Left view of tree

0 commit comments

Comments
 (0)