Check whether nodes of Binary Tree form Arithmetic , Geometric or Harmonic Progression | Structure of the node of the binary tree ; Function to find the size of the Binary Tree ; Base Case ; Function to check if the permutation of the sequence form Arithmetic Progression ; If the sequence contains only one element ; Sorting the array ; Loop to check if the sequence have same common difference between its consecutive elements ; Function to check if the permutation of the sequence form Geometric progression ; Condition when the length of the sequence is 1 ; Sorting the array ; Loop to check if the the sequence have same common ratio in consecutive elements ; Function to check if the permutation of the sequence form Harmonic Progression ; Condition when length of sequence in 1 ; Loop to find the reciprocal of the sequence ; Sorting the array ; Loop to check if the common difference of the sequence is same ; Function to check if the nodes of the Binary tree forms AP / GP / HP ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize height ; Loop to traverse the tree using Level order Traversal ; Enqueue left child ; Enqueue right child ; Condition to check if the sequence form Arithmetic Progression ; Condition to check if the sequence form Geometric Progression ; Condition to check if the sequence form Geometric Progression ; Constructed Binary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7
class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function size ( node ) { if ( node == null ) return 0 ; else return ( size ( node . left ) + 1 + size ( node . right ) ) ; } function checkIsAP ( arr , n ) { if ( n == 1 ) return true ; arr . sort ( ) ; let d = arr [ 1 ] - arr [ 0 ] ; for ( let i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; } function checkIsGP ( arr , n ) { if ( n == 1 ) return true ; arr . sort ( ) ; let r = arr [ 1 ] / arr [ 0 ] ; for ( let i = 2 ; i < n ; i ++ ) { if ( arr [ i ] / arr [ i - 1 ] != r ) return false ; } return true ; } function checkIsHP ( arr , n ) { if ( n == 1 ) { return true ; } let rec = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { rec [ i ] = ( ( 1 / arr [ i ] ) ) ; } rec . sort ( ) ; let d = ( rec [ 1 ] ) - ( rec [ 0 ] ) ; for ( let i = 2 ; i < n ; i ++ ) { if ( rec [ i ] - rec [ i - 1 ] != d ) { return false ; } } return true ; } function checktype ( root ) { let n = size ( root ) ; let arr = new Array ( n ) ; let i = 0 ; if ( root == null ) return ; let q = [ ] ; q . push ( root ) ; while ( q . length > 0 ) { let node = q [ 0 ] ; q . shift ( ) ; arr [ i ] = node . data ; i ++ ; if ( node . left != null ) q . push ( node . left ) ; if ( node . right != null ) q . push ( node . right ) ; } let flag = 0 ; if ( checkIsAP ( arr , n ) ) { document . write ( " " ) ; flag = 1 ; } else if ( checkIsGP ( arr , n ) ) { document . write ( " " ) ; flag = 1 ; } else if ( checkIsHP ( arr , n ) ) { document . write ( " " ) ; flag = 1 ; } else if ( flag == 0 ) { document . write ( " " ) ; } } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . right = new Node ( 8 ) ; root . right . right . left = new Node ( 6 ) ; root . right . right . right = new Node ( 7 ) ; checktype ( root ) ;