Given a Binary Tree, the task is to find the density of it by doing one traversal of it. Density of Binary Tree = (Size / Height).
Examples:
Input:
Output: 1.5 Explanation: As the height of given tree is 2 and size of given tree is 3 , the density is (3/2) = 1.5
Input:
Output: 1 Explanation: As the height of given tree is 3 and size of given tree is 3 , the density is (3/3) = 1
Density of a Binary Tree indicates, how balanced Binary Tree is. For example density of a skewed tree is minimum and that of a perfect tree is maximum.
Approach:
Two traversal based approach is very simple. First find the height using one traversal, then find the size using another traversal. Finally return the ratio of two values. To do it in one traversal, the idea is to compute the size of the binary tree while finding the height of the binary tree. Then simply return the value of (size/ height).
Below is the implementation of the above approach:
C++
// C++ program to find the density// of given binary tree.#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// Function which finds the height// of binary tree. It also calculates// the size of binary tree.intfindHeight(Node*root,int&size){if(root==nullptr)return0;size++;// Find height of left subtree.intleft=findHeight(root->left,size);// Find height of right subtree.intright=findHeight(root->right,size);returnmax(left,right)+1;}// Given a binary tree, find its density.floatfindDensity(Node*root){// base caseif(root==nullptr)return0;intsize=0;// Find the size and height of treeintheight=findHeight(root,size);return(float)size/height;}intmain(){// hard coded binary tree.// 10// / \ // 20 30Node*root=newNode(10);root->left=newNode(20);root->right=newNode(30);cout<<findDensity(root)<<endl;return0;}
Java
// Java program to find the density// of given binary tree.classNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGfG{// Function which finds the height// of binary tree. It also calculates// the size of binary tree.staticintfindHeight(Noderoot,int[]size){if(root==null)return0;size[0]++;// Find height of left subtree.intleft=findHeight(root.left,size);// Find height of right subtree.intright=findHeight(root.right,size);returnMath.max(left,right)+1;}// Given a binary tree, find its density.staticfloatfindDensity(Noderoot){// base caseif(root==null)return0;int[]size={0};// Find the size and height of treeintheight=findHeight(root,size);return(float)size[0]/height;}publicstaticvoidmain(String[]args){// hard coded binary tree.// 10// / \// 20 30Noderoot=newNode(10);root.left=newNode(20);root.right=newNode(30);System.out.println(findDensity(root));}}
Python
# Python program to find the density# of given binary tree.classNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Function which finds the height# of binary tree. It also calculates# the size of binary tree.deffindHeight(root,size):ifrootisNone:return0size[0]+=1# Find height of left subtree.left=findHeight(root.left,size)# Find height of right subtree.right=findHeight(root.right,size)returnmax(left,right)+1# Given a binary tree, find its density.deffindDensity(root):# base caseifrootisNone:return0size=[0]# Find the size and height of treeheight=findHeight(root,size)returnfloat(size[0])/heightif__name__=="__main__":# hard coded binary tree.# 10# / \# 20 30root=Node(10)root.left=Node(20)root.right=Node(30)print(findDensity(root))
C#
// C# program to find the density// of given binary tree.usingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGfG{// Function which finds the height// of binary tree. It also calculates// the size of binary tree.staticintfindHeight(Noderoot,refintsize){if(root==null)return0;size++;// Find height of left subtree.intleft=findHeight(root.left,refsize);// Find height of right subtree.intright=findHeight(root.right,refsize);returnMath.Max(left,right)+1;}// Given a binary tree, find its density.staticfloatfindDensity(Noderoot){// base caseif(root==null)return0;intsize=0;// Find the size and height of treeintheight=findHeight(root,refsize);return(float)size/height;}staticvoidMain(string[]args){// hard coded binary tree.// 10// / \// 20 30Noderoot=newNode(10);root.left=newNode(20);root.right=newNode(30);Console.WriteLine(findDensity(root));}}
JavaScript
// JavaScript program to find the density// of given binary tree.classNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Function which finds the height// of binary tree. It also calculates// the size of binary tree.functionfindHeight(root,size){if(root===null)return0;size[0]++;// Find height of left subtree.letleft=findHeight(root.left,size);// Find height of right subtree.letright=findHeight(root.right,size);returnMath.max(left,right)+1;}// Given a binary tree, find its density.functionfindDensity(root){// base caseif(root===null)return0;letsize=[0];// Find the size and height of treeletheight=findHeight(root,size);returnsize[0]/height;}// hard coded binary tree.// 10// / \// 20 30letroot=newNode(10);root.left=newNode(20);root.right=newNode(30);console.log(findDensity(root));
Output
1.5
Time Complexity: O(n), where n is the number of nodes in the tree. Auxiliary Space : O(h), where h is the height of the tree.