Density of Binary Tree in One Traversal
Last Updated :
19 Oct, 2024
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>
using namespace std;
class Node {
public:
int data;
Node* left, *right;
Node (int x) {
data = x;
left = nullptr;
right = nullptr;
}
};
// Function which finds the height
// of binary tree. It also calculates
// the size of binary tree.
int findHeight(Node* root, int &size) {
if (root == nullptr) return 0;
size++;
// Find height of left subtree.
int left = findHeight(root->left, size);
// Find height of right subtree.
int right = findHeight(root->right, size);
return max(left, right)+1;
}
// Given a binary tree, find its density.
float findDensity(Node* root) {
// base case
if (root == nullptr) return 0;
int size = 0;
// Find the size and height of tree
int height = findHeight(root, size);
return (float) size/height;
}
int main() {
// hard coded binary tree.
// 10
// / \
// 20 30
Node* root = new Node(10);
root->left = new Node(20);
root->right = new Node(30);
cout << findDensity(root) << endl;
return 0;
}
Java
// Java program to find the density
// of given binary tree.
class Node {
int data;
Node left, right;
Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// Function which finds the height
// of binary tree. It also calculates
// the size of binary tree.
static int findHeight(Node root, int[] size) {
if (root == null) return 0;
size[0]++;
// Find height of left subtree.
int left = findHeight(root.left, size);
// Find height of right subtree.
int right = findHeight(root.right, size);
return Math.max(left, right) + 1;
}
// Given a binary tree, find its density.
static float findDensity(Node root) {
// base case
if (root == null) return 0;
int[] size = {0};
// Find the size and height of tree
int height = findHeight(root, size);
return (float) size[0] / height;
}
public static void main(String[] args) {
// hard coded binary tree.
// 10
// / \
// 20 30
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
System.out.println(findDensity(root));
}
}
Python
# Python program to find the density
# of given binary tree.
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# Function which finds the height
# of binary tree. It also calculates
# the size of binary tree.
def findHeight(root, size):
if root is None:
return 0
size[0] += 1
# Find height of left subtree.
left = findHeight(root.left, size)
# Find height of right subtree.
right = findHeight(root.right, size)
return max(left, right) + 1
# Given a binary tree, find its density.
def findDensity(root):
# base case
if root is None:
return 0
size = [0]
# Find the size and height of tree
height = findHeight(root, size)
return float(size[0]) / height
if __name__ == "__main__":
# hard coded binary tree.
# 10
# / \
# 20 30
root = Node(10)
root.left = Node(20)
root.right = Node(30)
print(findDensity(root))
C#
// C# program to find the density
// of given binary tree.
using System;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = null;
right = null;
}
}
class GfG {
// Function which finds the height
// of binary tree. It also calculates
// the size of binary tree.
static int findHeight(Node root, ref int size) {
if (root == null) return 0;
size++;
// Find height of left subtree.
int left = findHeight(root.left, ref size);
// Find height of right subtree.
int right = findHeight(root.right, ref size);
return Math.Max(left, right) + 1;
}
// Given a binary tree, find its density.
static float findDensity(Node root) {
// base case
if (root == null) return 0;
int size = 0;
// Find the size and height of tree
int height = findHeight(root, ref size);
return (float)size / height;
}
static void Main(string[] args) {
// hard coded binary tree.
// 10
// / \
// 20 30
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
Console.WriteLine(findDensity(root));
}
}
JavaScript
// JavaScript program to find the density
// of given binary tree.
class Node {
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.
function findHeight(root, size) {
if (root === null) return 0;
size[0]++;
// Find height of left subtree.
let left = findHeight(root.left, size);
// Find height of right subtree.
let right = findHeight(root.right, size);
return Math.max(left, right) + 1;
}
// Given a binary tree, find its density.
function findDensity(root) {
// base case
if (root === null) return 0;
let size = [0];
// Find the size and height of tree
let height = findHeight(root, size);
return size[0] / height;
}
// hard coded binary tree.
// 10
// / \
// 20 30
let root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
console.log(findDensity(root));
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.
Related article:
Similar Reads
Density of Binary Tree using Level Order Traversal 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.5Explanation: As the height of given tree is 2 and size of given tree is 3 , the density is (3/2) = 1.5Input: Output: 1Explanation: As the heigh
5 min read
Mix Order Traversal of a Binary Tree Given a Binary Tree consisting of N nodes, the task is to print its Mix Order Traversal. Mix Order Traversal is a tree traversal technique, which involves any two of the existing traversal techniques like Inorder, Preorder and Postorder Traversal. Any two of them can be performed or alternate levels
13 min read
Middle To Up-Down Order traversal of a Binary Tree Given a binary tree, the task is to traverse this binary tree from the middle to the up-down order. In Middle to up-down order traversal, the following steps are performed: First, print the middle level of the tree.Then, print the elements at one level above the middle level of the tree.Then, print
15+ min read
DFS traversal of a Tree Depth-First Search (DFS) is a method used to explore all the nodes in a tree by going as deep as possible along each branch before moving to the next one. It starts at the root node and visits every node in the tree. Depth-First Search (DFS) can be classified into three main types based on the order
13 min read
Binary Tree Traversal Binary trees are fundamental data structures in computer science and understanding their traversal is crucial for various applications. Traversing a binary tree means visiting all the nodes in a specific order. There are several traversal methods, each with its unique applications and benefits. This
15+ min read