Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Count 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 i.e. left children and right children. In this article, we are going to discuss 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 in which all the nodes are completely filled except possibly the last level of the tree, and the last node has all its nodes on the left side as possible.

Below are examples to demonstrate the above problem, clearly:

Example 1

Input:

        1  
       / \  
      2   3  
     / \    
    4   5

Output

5

Explanation: There are 6 nodes(1, 2, 3, 4, 5) in total in the given complete binary tree.

Example 2

Input:

        10
        / \
      20   30
     / \    /
    40 50  60

Output

6

Explanation: There are a total of 6 nodes in the above binary tree. The nodes are 10, 20, 30, 40, 50, and 60 in the given complete binary tree.

Using the Recursive Approach

This is the recursive approach. In this approach we recursively traverse the complete binary tree and count all the nodes of the given binary tree. We find the total number of nodes by counting the current node and then recursively counting the nodes of left and right subtree of binary tree. 

Implementation Code

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));

Output

The Total number of nodes: 5

Time Complexity: O(n)

Using the Iterative Approach

This is a recursive approach. In this approach, we count the total number of nodes in the given complete binary tree iteratively using queue. In tjhis approach, we use breadth-first search traversal method to traverse the complete binary tree level by level and count each node present in the binary tree.

Implementation Code

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.right.left = new Node(50);
root.right.right = new Node(60);

console.log("Total number of nodes:", countNodesIterative(root));

Output

The Total number of nodes in given binary tree: 6

Time Complexity: O(n)

Real-Life Applications of Counting Nodes in a Complete Binary Tree

We can count the total number of nodes, this is simple and basic traversal task used to calculate the height or depth of a binary tree.

There are several techniques and algorithm which can be optimized and designed by knowing how to count the number of nodes.

Complete binary tree are generally used in different applications such as heap structures. Counting nodes in binary trees is used while managing priority queues or balanced search trees.

Updated on: 2025-05-18T04:50:32+05:30

2 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements