Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Count the number of nodes in a complete Binary tree using JavaScript
A binary tree is a non-linear data structure where each node can have at most two children: left and right. In this article, we'll explore how to count nodes in a complete binary tree using JavaScript.
What is a Complete Binary Tree?
A complete binary tree is a binary tree where all levels are completely filled except possibly the last level, and the last level has all nodes filled from left to right.
Example 1
Input:
1
/ \
2 3
/ \
4 5
Output:
5
Explanation: There are 5 nodes (1, 2, 3, 4, 5) in the given complete binary tree.
Example 2
Input:
10
/ \
20 30
/ \ /
40 50 60
Output:
6
Explanation: There are 6 nodes (10, 20, 30, 40, 50, 60) in the given complete binary tree.
Using the Recursive Approach
This approach recursively traverses the binary tree and counts all nodes. We count the current node plus the nodes in left and right subtrees.
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
function countNodes(root) {
if (root === null) {
return 0;
}
const leftCount = countNodes(root.left);
const rightCount = countNodes(root.right);
return 1 + leftCount + rightCount;
}
// Creating a complete binary tree
const 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);
console.log("Total number of nodes:", countNodes(root));
Total number of nodes: 5
Time Complexity: O(n) where n is the number of nodes.
Using the Iterative Approach
This approach uses breadth-first search (BFS) with a queue to traverse the tree level by level and count each node.
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
function countNodesIterative(root) {
if (root === null) {
return 0;
}
const queue = [root];
let count = 0;
while (queue.length > 0) {
const node = queue.shift();
count++;
if (node.left !== null) {
queue.push(node.left);
}
if (node.right !== null) {
queue.push(node.right);
}
}
return count;
}
// Creating a complete binary tree
const root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
root.right.left = new Node(60);
console.log("Total number of nodes:", countNodesIterative(root));
Total number of nodes: 6
Time Complexity: O(n) where n is the number of nodes.
Comparison
| Approach | Space Complexity | Pros | Cons |
|---|---|---|---|
| Recursive | O(h) - height of tree | Simple and clean code | Stack overflow for deep trees |
| Iterative | O(w) - maximum width | No stack overflow risk | Requires explicit queue |
Real-Life Applications
Counting nodes in complete binary trees is fundamental for:
- Heap Operations: Binary heaps use complete binary trees, and node counting helps in array-based implementations.
- Memory Management: Calculating tree size for memory allocation in data structures.
- Tree Balancing: Determining when rebalancing is needed in balanced search trees.
Conclusion
Both recursive and iterative approaches effectively count nodes in O(n) time. Choose recursive for simplicity or iterative to avoid stack overflow in very deep trees.
