Check whether nodes of Binary Tree form Arithmetic , Geometric or Harmonic Progression | C ++ implementation to check that nodes of binary tree form AP / GP / HP ; 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 ; Function to create new node ; Driver Code ; Constructed Binary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; int size ( Node * node ) { if ( node == NULL ) return 0 ; else return ( size ( node -> left ) + 1 + size ( node -> right ) ) ; } bool checkIsAP ( double arr [ ] , int n ) { if ( n == 1 ) return true ; sort ( arr , arr + n ) ; double d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; } bool checkIsGP ( double arr [ ] , int n ) { if ( n == 1 ) return true ; sort ( arr , arr + n ) ; double r = arr [ 1 ] / arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] / arr [ i - 1 ] != r ) return false ; } return true ; } bool checkIsHP ( double arr [ ] , int n ) { if ( n == 1 ) { return true ; } double rec [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { rec [ i ] = ( ( 1 / arr [ i ] ) ) ; } sort ( rec , rec + n ) ; double d = ( rec [ 1 ] ) - ( rec [ 0 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( rec [ i ] - rec [ i - 1 ] != d ) { return false ; } } return true ; } void checktype ( Node * root ) { int n = size ( root ) ; double arr [ n ] ; int i = 0 ; if ( root == NULL ) return ; queue < Node * > q ; q . push ( root ) ; while ( q . empty ( ) == false ) { Node * node = q . front ( ) ; arr [ i ] = node -> data ; i ++ ; q . pop ( ) ; if ( node -> left != NULL ) q . push ( node -> left ) ; if ( node -> right != NULL ) q . push ( node -> right ) ; } int flag = 0 ; if ( checkIsAP ( arr , n ) ) { cout << " Arithmetic ▁ Progression " << endl ; flag = 1 ; } else if ( checkIsGP ( arr , n ) ) { cout << " Geometric ▁ Progression " << endl ; flag = 1 ; } else if ( checkIsHP ( arr , n ) ) { cout << " Geometric ▁ Progression " << endl ; flag = 1 ; } else if ( flag == 0 ) { cout << " No " ; } } struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } int main ( ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> right = newNode ( 8 ) ; root -> right -> right -> left = newNode ( 6 ) ; root -> right -> right -> right = newNode ( 7 ) ; checktype ( root ) ; return 0 ; }