Print all Coprime path of a Binary Tree | Java 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
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static int N = 1000000 ; static Vector < Integer > prime = new Vector < Integer > ( ) ; static void SieveOfEratosthenes ( ) { boolean [ ] check = new boolean [ N + 1 ] ; Arrays . fill ( check , true ) ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( check [ p ] == true ) { prime . add ( p ) ; for ( int i = p * p ; i <= N ; i += p ) check [ i ] = false ; } } } static boolean isPathCo_Prime ( Vector < Integer > path ) { int max = 0 ; for ( int x : path ) { if ( max < x ) max = x ; } for ( int i = 0 ; i * prime . get ( i ) <= max / 2 ; i ++ ) { int ct = 0 ; for ( int x : path ) { if ( x % prime . get ( i ) == 0 ) ct ++ ; } if ( ct > 1 ) { return false ; } } return true ; } static void printCo_PrimePaths ( Vector < Integer > path ) { for ( int x : path ) { System . out . print ( x + " ▁ " ) ; } System . out . println ( ) ; } static void findCo_PrimePaths ( Node root , Vector < Integer > path ) { if ( root == null ) return ; path . add ( 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 . remove ( path . size ( ) - 1 ) ; } static void printCo_PrimePaths ( Node node ) { SieveOfEratosthenes ( ) ; Vector < Integer > path = new Vector < Integer > ( ) ; findCo_PrimePaths ( node , path ) ; } public static void main ( String [ ] args ) { 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 ) ; } }