Print the number of set bits in each node of a Binary Tree | A Binary Tree Node ; Utility function that allocates a new Node ; Function to print the number of set bits in each node of the binary tree ; Print the number of set bits of current node using __builtin_popcount ( ) ; Traverse Left Subtree ; Traverse Right Subtree ; Driver code
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 ( " " + root . data + " " + bitCount ( root . data ) + " " ) ; 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 ) ;