Print all Coprime path of a Binary Tree | C ++ program for printing Co - prime paths of binary Tree ; A Tree node ; Utility function to create a new node ; Vector to store all the prime numbers ; Function to store all the prime numbers in an array ; Create a boolean array " prime [ 0 . . N ] " and initialize all the entries in it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiples of p and are less than p ^ 2 are already marked . ; Function to check whether Path is Co - prime or not ; Iterating through the array to find the maximum element in the array ; Incrementing the variable if any of the value has a factor ; If not co - prime ; Function to print a Co - Prime path ; Function to find co - prime paths of binary tree ; Base case ; Store the value in path vector ; Recursively call for left sub tree ; Recursively call for right sub tree ; Condition to check , if leaf node ; Condition to check , if path co - prime or not ; Print co - prime path ; Remove the last element from the path vector ; Function to find Co - Prime paths In a given binary tree ; To save all prime numbers ; Function call ; Driver Code ; Create Binary Tree as shown ; Print Co - Prime Paths
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } int N = 1000000 ; vector < int > prime ; void SieveOfEratosthenes ( ) { bool check [ N + 1 ] ; memset ( check , true , sizeof ( check ) ) ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( check [ p ] == true ) { prime . push_back ( p ) ; for ( int i = p * p ; i <= N ; i += p ) check [ i ] = false ; } } } bool isPathCo_Prime ( vector < int > & path ) { int max = 0 ; for ( auto x : path ) { if ( max < x ) max = x ; } for ( int i = 0 ; i * prime [ i ] <= max / 2 ; i ++ ) { int ct = 0 ; for ( auto x : path ) { if ( x % prime [ i ] == 0 ) ct ++ ; } if ( ct > 1 ) { return false ; } } return true ; } void printCo_PrimePaths ( vector < int > & path ) { for ( auto x : path ) { cout << x << " ▁ " ; } cout << endl ; } void findCo_PrimePaths ( struct Node * root , vector < int > & path ) { if ( root == NULL ) return ; path . push_back ( root -> key ) ; findCo_PrimePaths ( root -> left , path ) ; findCo_PrimePaths ( root -> right , path ) ; if ( root -> left == NULL && root -> right == NULL ) { if ( isPathCo_Prime ( path ) ) { printCo_PrimePaths ( path ) ; } } path . pop_back ( ) ; } void printCo_PrimePaths ( struct Node * node ) { SieveOfEratosthenes ( ) ; vector < int > path ; findCo_PrimePaths ( node , path ) ; } int main ( ) { Node * root = newNode ( 10 ) ; root -> left = newNode ( 48 ) ; root -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 11 ) ; root -> right -> right = newNode ( 37 ) ; root -> right -> left -> left = newNode ( 7 ) ; root -> right -> left -> right = newNode ( 29 ) ; root -> right -> right -> left = newNode ( 42 ) ; root -> right -> right -> right = newNode ( 19 ) ; root -> right -> right -> right -> left = newNode ( 7 ) ; printCo_PrimePaths ( root ) ; return 0 ; }