Print the number of set bits in each node of a Binary Tree
Last Updated :
29 Nov, 2022
Given a Binary Tree. The task is to print the number of set bits in each of the nodes in the Binary Tree.
The idea is to traverse the given binary tree using any tree traversal method, and for each node calculate the number of set bits and print it.
Note: One can also use the __builtin_popcount() function available in C++ to directly count the number of set bits in an integer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
Node* newNode( int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
void printSetBit(Node* root)
{
if (root == NULL)
return ;
cout << "Set bits in Node " << root->data << " = " <<
__builtin_popcount(root->data) << "\n" ;
printSetBit(root->left);
printSetBit(root->right);
}
int main()
{
Node* root = newNode(16);
root->left = newNode(13);
root->left->left = newNode(14);
root->left->right = newNode(12);
root->right = newNode(11);
root->right->left = newNode(10);
root->right->right = newNode(16);
printSetBit(root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
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 void printSetBit(Node root)
{
if (root == null )
return ;
System.out.print( "Set bits in Node " + root.data + " = " +
Integer.bitCount(root.data) + "\n" );
printSetBit(root.left);
printSetBit(root.right);
}
public static void main(String[] args)
{
Node root = newNode( 16 );
root.left = newNode( 13 );
root.left.left = newNode( 14 );
root.left.right = newNode( 12 );
root.right = newNode( 11 );
root.right.left = newNode( 10 );
root.right.right = newNode( 16 );
printSetBit(root);
}
}
|
Python3
class Node:
def __init__( self ):
self .data = None
self .left = None
self .right = None
def newNode(data):
node = Node()
node.data = data
node.left = None
node.right = None
return node
def printSetBit(root: Node):
if root is None :
return
print ( "Set bits in Node %d = %d" %
(root.data, bin (root.data).count( '1' )))
printSetBit(root.left)
printSetBit(root.right)
if __name__ = = "__main__" :
root = newNode( 16 )
root.left = newNode( 13 )
root.left.left = newNode( 14 )
root.left.right = newNode( 12 )
root.right = newNode( 11 )
root.right.left = newNode( 10 )
root.right.right = newNode( 16 )
printSetBit(root)
|
C#
using System;
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 void printSetBit(Node root)
{
if (root == null )
return ;
Console.Write( "Set bits in Node " + root.data +
" = " + bitCount(root.data) + "\n" );
printSetBit(root.left);
printSetBit(root.right);
}
static int bitCount( int x)
{
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
public static void Main(String[] args)
{
Node root = newNode(16);
root.left = newNode(13);
root.left.left = newNode(14);
root.left.right = newNode(12);
root.right = newNode(11);
root.right.left = newNode(10);
root.right.right = newNode(16);
printSetBit(root);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let node = new Node(data);
return (node);
}
function bitCount(x)
{
let setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
function printSetBit(root)
{
if (root == null )
return ;
document.write( "Set bits in Node " + root.data +
" = " + bitCount(root.data) + "</br>" );
printSetBit(root.left);
printSetBit(root.right);
}
let root = newNode(16);
root.left = newNode(13);
root.left.left = newNode(14);
root.left.right = newNode(12);
root.right = newNode(11);
root.right.left = newNode(10);
root.right.right = newNode(16);
printSetBit(root);
</script>
|
Output
Set bits in Node 16 = 1
Set bits in Node 13 = 3
Set bits in Node 14 = 3
Set bits in Node 12 = 2
Set bits in Node 11 = 3
Set bits in Node 10 = 2
Set bits in Node 16 = 1
Time Complexity: O(N × log2(max_node)), where N is the number of nodes, and max_node is the node with the maximum value.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...