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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return node;
}
float density(Node* root)
{
queue<Node*> q;
q.push(root);
q.push(NULL);
int height = 1, size = 0;
while (!q.empty()) {
Node* t = q.front();
q.pop();
if (t)
size++;
else {
if (q.empty())
break ;
q.push(NULL);
height++;
continue ;
}
if (t->left) {
q.push(t->left);
}
if (t->right) {
q.push(t->right);
}
}
return ( float )size / height;
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
cout << density(root) << endl;
return 0;
}
|
Java
import java.util.*;
class Solution
{
static class Node
{
int data;
Node left, right;
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return node;
}
static float density(Node root)
{
Queue<Node> q = new LinkedList<Node>();
q.add(root);
q.add( null );
int height = 1 , size = 0 ;
while (q.size() > 0 )
{
Node t = q.peek();
q.remove();
if (t != null )
size++;
else
{
if (q.size() == 0 )
break ;
q.add( null );
height++;
continue ;
}
if (t.left != null )
{
q.add(t.left);
}
if (t.right != null )
{
q.add(t.right);
}
}
return (( float )size )/ height;
}
public static void main(String args[])
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
System.out.println(density(root));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def newNode( data) :
node = Node( 0 )
node.data = data
node.left = node.right = None
return node
def density(root) :
q = []
q.append(root)
q.append( None )
height = 1
size = 0
while ( len (q) > 0 ):
t = q[ 0 ]
q.pop( 0 )
if (t ! = None ):
size = size + 1
else :
if ( len (q) = = 0 ):
break
q.append( None )
height = height + 1
continue
if (t.left ! = None ) :
q.append(t.left)
if (t.right ! = None ):
q.append(t.right)
return (size ) / height
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
print (density(root))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node left, right;
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return node;
}
static float density(Node root)
{
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
q.Enqueue( null );
int height = 1, size = 0;
while (q.Count > 0)
{
Node t = q.Peek();
q.Dequeue();
if (t != null )
size++;
else
{
if (q.Count == 0)
break ;
q.Enqueue( null );
height++;
continue ;
}
if (t.left != null )
{
q.Enqueue(t.left);
}
if (t.right != null )
{
q.Enqueue(t.right);
}
}
return (( float )size ) / height;
}
public static void Main(String []args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
Console.WriteLine(density(root));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .left = null ;
this .right = null ;
}
}
function newNode(data)
{
var node = new Node();
node.data = data;
node.left = node.right = null ;
return node;
}
function density(root)
{
var q = [];
q.push(root);
q.push( null );
var height = 1, size = 0;
while (q.length > 0)
{
var t = q[0];
q.shift();
if (t != null )
size++;
else
{
if (q.length == 0)
break ;
q.push( null );
height++;
continue ;
}
if (t.left != null )
{
q.push(t.left);
}
if (t.right != null )
{
q.push(t.right);
}
}
return (size) / height;
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
document.write(density(root));
</script>
|
Time Complexity: O(N) where n is the number of nodes in the binary tree.
Auxiliary Space: O(N) where n is the number of nodes in the binary tree.