Iterative program to Calculate Size of a tree

Size of a tree is the number of elements present in the tree. Size of the below tree is 5.

Example Tree

Example Tree

Approach
The idea is to use Level Order Traversing



1) Create an empty queue q
2) temp_node = root /*start from root*/
3) Loop while temp_node is not NULL
    a) Enqueue temp_node’s children (first left then right children) to q
    b) Increase count with every enqueuing.
    c) Dequeue a node from q and assign it’s value to temp_node

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java programn to calculate
// Size of a tree
import java.util.LinkedList;
import java.util.Queue;
  
class Node
{
    int data;
    Node left, right;
  
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
}
  
class BinaryTree
{
    Node root;
          
    public int size()
    {
        if (root == null)
            return 0;
          
        // Using level order Traversal .
        Queue<Node> q = new LinkedList<Node>();
        q.offer(root);
          
        int count = 1
        while (!q.isEmpty())
        {
            Node tmp = q.poll();
      
            // when the queue is empty:
            // the poll() method returns null.
            if (tmp!=null)
            {
                if (tmp.left!=null)
                {
                    // Increment count
                    count++;
                      
                    // Enqueue left child 
                    q.offer(tmp.left);
                }
                if (tmp.right!=null)
                {
                    // Increment count
                    count++;
                      
                    // Enqueue left child 
                    q.offer(tmp.right);
                }
            }
        }
          
        return count;
    }
  
    public static void main(String args[])
    {
        /* creating a binary tree and entering 
          the nodes */
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
        tree.root.left.left = new Node(4);
        tree.root.left.right = new Node(5);
  
        System.out.println("The size of binary tree"
                         " is : " + tree.size());
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# programn to calculate
// Size of a tree
using System;
using System.Collections.Generic;
  
public class Node
{
    public int data;
    public Node left, right;
  
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
}
  
public class BinaryTree
{
    Node root;
          
    public int size()
    {
        if (root == null)
            return 0;
          
        // Using level order Traversal .
        Queue<Node> q = new Queue<Node>();
        q.Enqueue(root);
          
        int count = 1; 
        while (q.Count != 0)
        {
            Node tmp = q.Dequeue();
      
            // when the queue is empty:
            // the poll() method returns null.
            if (tmp != null)
            {
                if (tmp.left != null)
                {
                    // Increment count
                    count++;
                      
                    // Enqueue left child 
                    q.Enqueue(tmp.left);
                }
                if (tmp.right != null)
                {
                    // Increment count
                    count++;
                      
                    // Enqueue left child 
                    q.Enqueue(tmp.right);
                }
            }
        }
          
        return count;
    }
  
    // Driver code
    public static void Main(String []args)
    {
        /* creating a binary tree and entering 
        the nodes */
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
        tree.root.left.left = new Node(4);
        tree.root.left.right = new Node(5);
  
        Console.WriteLine("The size of binary tree"
                        " is : " + tree.size());
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right



Output:

Size of the tree is 5

Time Complexity: O(n)
Auxiliary Space : O(level_max) where level max is maximum number of node in any level.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : 29AjayKumar



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.