Iterative program to Calculate Size of a tree
Last Updated :
18 Jul, 2022
Size of a tree is the number of elements present in the tree. Size of the below tree is 5.
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
C++
#include<iostream>
#include<queue>
using namespace std;
struct Node
{
int data;
Node *left, *right;
};
Node *newNode( int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int sizeoftree(Node *root)
{
if (root == NULL)
return 0;
queue<Node *> q;
int count = 1;
q.push(root);
while (!q.empty())
{
Node *temp = q.front();
if (temp->left)
{
q.push(temp->left);
count++;
}
if (temp->right)
{
q.push(temp->right);
count++;
}
q.pop();
}
return count;
}
int main()
{
Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
cout << "Size of the tree is " <<
sizeoftree(root) << endl;
return 0;
}
|
Java
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 ;
Queue<Node> q = new LinkedList<Node>();
q.offer(root);
int count = 1 ;
while (!q.isEmpty())
{
Node tmp = q.poll();
if (tmp != null )
{
if (tmp.left != null )
{
count++;
q.offer(tmp.left);
}
if (tmp.right != null )
{
count++;
q.offer(tmp.right);
}
}
}
return count;
}
public static void main(String args[])
{
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());
}
}
|
Python3
class newNode:
def __init__( self ,data):
self .data = data
self .left = self .right = None
def sizeoftree(root):
if root = = None :
return 0
q = []
q.append(root)
count = 1
while ( len (q) ! = 0 ):
root = q.pop( 0 )
if (root.left):
q.append(root.left)
count + = 1
if (root.right):
q.append(root.right)
count + = 1
return count
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
print (sizeoftree(root))
|
C#
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;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
int count = 1;
while (q.Count != 0)
{
Node tmp = q.Dequeue();
if (tmp != null )
{
if (tmp.left != null )
{
count++;
q.Enqueue(tmp.left);
}
if (tmp.right != null )
{
count++;
q.Enqueue(tmp.right);
}
}
}
return count;
}
public static void Main(String []args)
{
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());
}
}
|
Javascript
<script>
class Node
{
constructor(item) {
this .left = null ;
this .right = null ;
this .data = item;
}
}
let root;
function size()
{
if (root == null )
return 0;
let q = [];
q.push(root);
let count = 1;
while (q.length > 0)
{
let tmp = q[0];
q.shift();
if (tmp != null )
{
if (tmp.left != null )
{
count++;
q.push(tmp.left);
}
if (tmp.right != null )
{
count++;
q.push(tmp.right);
}
}
}
return count;
}
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);
document.write( "Size of the tree is " + size());
</script>
|
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.
Share your thoughts in the comments
Please Login to comment...