Density of Binary Tree using Level Order Traversal
Given a Binary Tree, find the density of it by doing one traversal of it.
The density of binary tree is defined as:
Density of Binary Tree = Size / Height
Examples:
Input :
Root of following tree
10
/ \
20 30
Output : 1.5
Height of given tree = 2
Size of given tree = 3
Input :
Root of the following tree
10
/
20
/
30
Output : 1
Height of given tree = 3
Size of given tree = 3
The size and height of the tree can be found in single traversal using level order traversal.
To calculate the height of the binary tree the idea is to use a “NULL” pointer as a separator between two levels. Whenever “NULL” occurs during the traversal, height is incremented.
To calculate the size of the binary tree, increment the counter for every new node encountered during the level order traversal.
Finally, use the above formula to calculate the density of the Binary Tree.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // A binary tree node struct Node { int data; Node *left, *right; }; // Helper function to allocates a new node Node* newNode(int data) { Node* node = new Node; node->data = data; node->left = node->right = NULL; return node; } // Function to calculate density of Binary Tree float density(Node* root) { queue<Node*> q; // push root to queue first q.push(root); // push NULL as a seperator q.push(NULL); int height = 1, size = 0; while (!q.empty()) { Node* t = q.front(); q.pop(); if (t) size++; else { // If after popping NULL queue is // empty then get out of loop i.e // stop the level order traversal. if (q.empty()) break; q.push(NULL); height++; continue; } // if t has left child // then push it to queue if (t->left) { q.push(t->left); } // if t has right child // then push it to queue if (t->right) { q.push(t->right); } } return (float)size / height; } // Driver code int main() { Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); cout << density(root) << endl; return 0; } |
Java
// Java implementation of the above approach import java.util.*; class Solution { // A binary tree node static class Node { int data; Node left, right; } // Helper function to allocates a new node static Node newNode(int data) { Node node = new Node(); node.data = data; node.left = node.right = null; return node; } // Function to calculate density of Binary Tree static float density(Node root) { Queue<Node> q = new LinkedList<Node>(); // add root to queue first q.add(root); // add null as a seperator q.add(null); int height = 1, size = 0; while (q.size() > 0) { Node t = q.peek(); q.remove(); if (t != null) size++; else { // If after removeping null queue is // empty then get out of loop i.e // stop the level order traversal. if (q.size() == 0) break; q.add(null); height++; continue; } // if t has left child // then add it to queue if (t.left !=null) { q.add(t.left); } // if t has right child // then add it to queue if (t.right != null) { q.add(t.right); } } return ((float)size )/ height; } // Driver code public static void main(String args[]) { Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); System.out.println(density(root)); } } // This code is contributed by Arnab Kundu |
C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
class GFG
{
// A binary tree node
public class Node
{
public int data;
public Node left, right;
}
// Helper function to allocates a new node
static Node newNode(int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null;
return node;
}
// Function to calculate density of Binary Tree
static float density(Node root)
{
Queue
// add root to queue first
q.Enqueue(root);
// add null as a seperator
q.Enqueue(null);
int height = 1, size = 0;
while (q.Count > 0)
{
Node t = q.Peek();
q.Dequeue();
if (t != null)
size++;
else
{
// If after removeping null queue is
// empty then get out of loop i.e
// stop the level order traversal.
if (q.Count == 0)
break;
q.Enqueue(null);
height++;
continue;
}
// if t has left child
// then add it to queue
if (t.left !=null)
{
q.Enqueue(t.left);
}
// if t has right child
// then add it to queue
if (t.right != null)
{
q.Enqueue(t.right);
}
}
return ((float)size ) / height;
}
// Driver code
public static void Main(String []args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
Console.WriteLine(density(root));
}
}
// This code is contributed by PrinciRaj1992
1.5
Time Complexity : O(N)
Recommended Posts:
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Flatten Binary Tree in order of Level Order Traversal
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Density of Binary Tree in One Traversal
- Insertion in n-ary tree in given order and Level order traversal
- Level Order Tree Traversal
- Zig Zag Level order traversal of a tree using single queue
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Flatten binary tree in order of post-order traversal
- Insertion in a Binary Tree in level order
- Level Order Successor of a node in Binary Tree
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.



