Select a Random Node from a tree with equal probability | JavaScript program to Select a Random Node from a tree ; This is used to fill children counts . ; Inserts Children count for each node ; returns number of children for root ; Helper Function to return a random node ; Returns Random node ; Creating Above Tree
class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; this . children = 0 ; } } function getElements ( root ) { if ( root == null ) return 0 ; return getElements ( root . left ) + getElements ( root . right ) + 1 ; } function insertChildrenCount ( root ) { if ( root == null ) return null ; root . children = getElements ( root ) - 1 ; root . left = insertChildrenCount ( root . left ) ; root . right = insertChildrenCount ( root . right ) ; return root ; } function children ( root ) { if ( root == null ) return 0 ; return root . children + 1 ; } function randomNodeUtil ( root , count ) { if ( root == null ) return 0 ; if ( count == children ( root . left ) ) return root . data ; if ( count < children ( root . left ) ) return randomNodeUtil ( root . left , count ) ; return randomNodeUtil ( root . right , count - children ( root . left ) - 1 ) ; } function randomNode ( root ) { let count = Math . floor ( Math . random ( ) * ( root . children + 1 ) ) ; return randomNodeUtil ( root , count ) ; } let root = new Node ( 10 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . right = new Node ( 40 ) ; root . left . right = new Node ( 50 ) ; root . right . left = new Node ( 60 ) ; root . right . right = new Node ( 70 ) ; insertChildrenCount ( root ) ; document . write ( " " + randomNode ( root ) + " " ) ;