codeparrot/xlcost-text-to-code huggingface dataset:
Find Unique pair in an array with pairs of numbers | C program to find a unique pair in an array of pairs . ; XOR each element and get XOR of two unique elements ( ans ) ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . int x = 0 , y = 0 ; Initialize missing numbers ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; Driver code
#include <stdio.h> NEW_LINE void findUniquePair ( int arr [ ] , int n ) { int XOR = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) XOR = XOR ^ arr [ i ] ; int set_bit_no = XOR & ~ ( XOR - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & set_bit_no ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } printf ( " The ▁ unique ▁ pair ▁ is ▁ ( % d , ▁ % d ) " , x , y ) ; } int main ( ) { int a [ ] = { 6 , 1 , 3 , 5 , 1 , 3 , 7 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; findUniquePair ( a , n ) ; return 0 ; }
M | C ++ implementation to find the mth smallest number having k number of set bits ; function to find the next higher number with same number of set bits as in ' x ' ; the approach is same as discussed in ; function to find the mth smallest number having k number of set bits ; smallest number having ' k ' number of set bits ; finding the mth smallest number having k set bits ; required number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef unsigned int uint_t ; uint_t nxtHighWithNumOfSetBits ( uint_t x ) { uint_t rightOne ; uint_t nextHigherOneBit ; uint_t rightOnesPattern ; uint_t next = 0 ; if ( x ) { rightOne = x & - ( signed ) x ; nextHigherOneBit = x + rightOne ; rightOnesPattern = x ^ nextHigherOneBit ; rightOnesPattern = ( rightOnesPattern ) / rightOne ; rightOnesPattern >>= 2 ; next = nextHigherOneBit | rightOnesPattern ; } return next ; } int mthSmallestWithKSetBits ( uint_t m , uint_t k ) { uint_t num = ( 1 << k ) - 1 ; for ( int i = 1 ; i < m ; i ++ ) num = nxtHighWithNumOfSetBits ( num ) ; return num ; } int main ( ) { uint_t m = 6 , k = 4 ; cout << mthSmallestWithKSetBits ( m , k ) ; return 0 ; }
Count unset bits of a number | An optimized C ++ program to count unset bits in an integer . ; This makes sure two bits ( From MSB and including MSB ) are set ; This makes sure 4 bits ( From MSB and including MSB ) are set ; Count set bits in toggled number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countUnsetBits ( int n ) { int x = n ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return __builtin_popcount ( x ^ n ) ; } int main ( ) { int n = 17 ; cout << countUnsetBits ( n ) ; return 0 ; }
Toggle all bits after most significant bit | CPP program to toggle set bits starting from MSB ; Function to toggle bits starting from MSB ; temporary variable to use XOR with one of a n ; Run loop until the only set bit in temp crosses MST of n . ; Toggle bit of n corresponding to current set bit in temp . ; Move set bit to next higher position . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void toggle ( int & n ) { int temp = 1 ; while ( temp <= n ) { n = n ^ temp ; temp = temp << 1 ; } } int main ( ) { int n = 10 ; toggle ( n ) ; cout << n ; return 0 ; }
Find the n | C ++ program to find n - th number whose binary representation is palindrome . ; Finds if the kth bit is set in the binary representation ; Returns the position of leftmost set bit in the binary representation ; Finds whether the integer in binary representation is palindrome or not ; One by one compare bits ; Compare left and right bits and converge ; Start from 1 , traverse through all the integers ; If we reach n , break the loop ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isKthBitSet ( int x , int k ) { return ( x & ( 1 << ( k - 1 ) ) ) ? 1 : 0 ; } int leftmostSetBit ( int x ) { int count = 0 ; while ( x ) { count ++ ; x = x >> 1 ; } return count ; } int isBinPalindrome ( int x ) { int l = leftmostSetBit ( x ) ; int r = 1 ; while ( l > r ) { if ( isKthBitSet ( x , l ) != isKthBitSet ( x , r ) ) return 0 ; l -- ; r ++ ; } return 1 ; } int findNthPalindrome ( int n ) { int pal_count = 0 ; int i = 0 ; for ( i = 1 ; i <= INT_MAX ; i ++ ) { if ( isBinPalindrome ( i ) ) { pal_count ++ ; } if ( pal_count == n ) break ; } return i ; } int main ( ) { int n = 9 ; cout << findNthPalindrome ( n ) ; }
Print ' K ' th least significant bit of a number | CPP code to print ' K ' th LSB ; Function returns 1 if set , 0 if not ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool LSB ( int num , int K ) { return ( num & ( 1 << ( K - 1 ) ) ) ; } int main ( ) { int num = 10 , K = 4 ; cout << LSB ( num , K ) ; return 0 ; }
Check if two numbers are equal without using comparison operators | CPP code to check if 2 numbers are same ; Finds if a and b are same ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void areSame ( int a , int b ) { if ( ! ( a - b ) ) cout << " Same " ; else cout << " Not ▁ Same " ; } int main ( ) { areSame ( 10 , 20 ) ; return 0 ; }
Position of rightmost different bit | C ++ implementation to find the position of rightmost different bit ; Function to find the position of rightmost set bit in ' n ' returns 0 if there is no set bit . ; to handle edge case when n = 0. ; Function to find the position of rightmost different bit in the binary representations of ' m ' and ' n ' returns 0 if there is no rightmost different bit . ; position of rightmost different bit ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getRightMostSetBit ( int n ) { if ( n == 0 ) return 0 ; return log2 ( n & - n ) + 1 ; } int posOfRightMostDiffBit ( int m , int n ) { return getRightMostSetBit ( m ^ n ) ; } int main ( ) { int m = 52 , n = 24 ; cout << " Position ▁ of ▁ rightmost ▁ different ▁ bit : " << posOfRightMostDiffBit ( m , n ) << endl ; return 0 ; }
Closest ( or Next ) smaller and greater numbers with same number of set bits | C ++ Implementation of getNext with Same number of bits 1 's is below ; Main Function to find next smallest number bigger than n ; Compute c0 and c1 ; If there is no bigger number with the same no . of 1 's ; Driver Code ; input 1 ; input 2
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getNext ( int n ) { int c = n ; int c0 = 0 ; int c1 = 0 ; while ( ( ( c & 1 ) == 0 ) && ( c != 0 ) ) { c0 ++ ; c >>= 1 ; } while ( ( c & 1 ) == 1 ) { c1 ++ ; c >>= 1 ; } if ( c0 + c1 == 31 c0 + c1 == 0 ) return -1 ; return n + ( 1 << c0 ) + ( 1 << ( c1 - 1 ) ) - 1 ; } int main ( ) { int n = 5 ; cout << getNext ( n ) ; n = 8 ; cout << endl ; cout << getNext ( n ) ; return 0 ; }
Count minimum bits to flip such that XOR of A and B equal to C | C ++ code to count the Minimum bits in A and B ; If both A [ i ] and B [ i ] are equal ; If Both A and B are unequal ; Driver Code ; N represent total count of Bits
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalFlips ( char * A , char * B , char * C , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( A [ i ] == B [ i ] && C [ i ] == '1' ) ++ count ; else if ( A [ i ] != B [ i ] && C [ i ] == '0' ) ++ count ; } return count ; } int main ( ) { int N = 5 ; char a [ ] = "10100" ; char b [ ] = "00010" ; char c [ ] = "10011" ; cout << totalFlips ( a , b , c , N ) ; return 0 ; }
Swap three variables without using temporary variable | C ++ program to swap three variables without using temporary variable ; Assign c ' s ▁ value ▁ to ▁ a , ▁ a ' s value to b and b 's value to c. ; Store XOR of all in a ; After this , b has value of a ; After this , c has value of b ; After this , a has value of c ; Driver code ; Calling Function
#include <iostream> NEW_LINE using namespace std ; void swapThree ( int & a , int & b , int & c ) { a = a ^ b ^ c ; b = a ^ b ^ c ; c = a ^ b ^ c ; a = a ^ b ^ c ; } int main ( ) { int a = 10 , b = 20 , c = 30 ; cout << " Before ▁ swapping ▁ a ▁ = ▁ " << a << " , ▁ b ▁ = ▁ " << b << " , ▁ c ▁ = ▁ " << c << endl ; swapThree ( a , b , c ) ; cout << " After ▁ swapping ▁ a ▁ = ▁ " << a << " , ▁ b ▁ = ▁ " << b << " , ▁ c ▁ = ▁ " << c << endl ; return 0 ; }
Find Two Missing Numbers | Set 2 ( XOR based solution ) | C ++ Program to find 2 Missing Numbers using O ( 1 ) extra space and no overflow . ; Function to find two missing numbers in range [ 1 , n ] . This function assumes that size of array is n - 2 and all array elements are distinct ; Get the XOR of all elements in arr [ ] and { 1 , 2 . . n } ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . int x = 0 , y = 0 ; Initialize missing numbers ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; XOR of first set in arr [ ] and { 1 , 2 , ... n } ; XOR of second set in arr [ ] and { 1 , 2 , ... n } ; Driver program to test above function ; Range of numbers is 2 plus size of array
#include <bits/stdc++.h> NEW_LINE void findTwoMissingNumbers ( int arr [ ] , int n ) { int XOR = arr [ 0 ] ; for ( int i = 1 ; i < n - 2 ; i ++ ) XOR ^= arr [ i ] ; for ( int i = 1 ; i <= n ; i ++ ) XOR ^= i ; int set_bit_no = XOR & ~ ( XOR - 1 ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( arr [ i ] & set_bit_no ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( i & set_bit_no ) x = x ^ i ; else y = y ^ i ; } printf ( " Two ▁ Missing ▁ Numbers ▁ are % d % d " , x , y ) ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 6 } ; int n = 2 + sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findTwoMissingNumbers ( arr , n ) ; return 0 ; }
Find profession in a special family | C ++ program to find profession of a person at given level and position . ; Function to get no of set bits in binary representation of passed binary no . ; Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Count set bits in ' pos - 1' ; If set bit count is odd , then doctor , else engineer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSetBits ( int n ) { int count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; } char findProffesion ( int level , int pos ) { int c = countSetBits ( pos - 1 ) ; return ( c % 2 ) ? ' d ' : ' e ' ; } int main ( void ) { int level = 3 , pos = 4 ; ( findProffesion ( level , pos ) == ' e ' ) ? cout << " Engineer " : cout << " Doctor " ; return 0 ; }
Find XOR of two number without using XOR operator | C ++ program to find XOR without using ^ ; Returns XOR of x and y ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int myXOR ( int x , int y ) { return ( x & ( ~ y ) ) | ( ( ~ x ) & y ) ; } int main ( ) { int x = 3 , y = 5 ; cout << " XOR ▁ is ▁ " << myXOR ( x , y ) ; return 0 ; }
Find maximum xor of k elements in an array | C ++ implementation of the approach ; Function to return the maximum xor for a subset of size k from the given array ; Initialize result ; Traverse all subsets of the array ; __builtin_popcount ( ) returns the number of sets bits in an integer ; Initialize current xor as 0 ; If jth bit is set in i then include jth element in the current xor ; Update maximum xor so far ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Max_Xor ( int arr [ ] , int n , int k ) { int maxXor = INT_MIN ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { if ( __builtin_popcount ( i ) == k ) { int cur_xor = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) cur_xor = cur_xor ^ arr [ j ] ; } maxXor = max ( maxXor , cur_xor ) ; } } return maxXor ; } int main ( ) { int arr [ ] = { 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 3 ; cout << Max_Xor ( arr , n , k ) ; return 0 ; }
Bitwise Operators in C / C ++ | CPP Program to demonstrate use of bitwise operators ; a = 5 ( 00000101 ) , b = 9 ( 00001001 ) ; The result is 00000001 ; The result is 00001101 ; The result is 00001100 ; The result is 11111010 ; The result is 00010010 ; The result is 00000100
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int a = 5 , b = 9 ; cout << " a ▁ = ▁ " << a << " , " << " ▁ b ▁ = ▁ " << b << endl ; cout << " a ▁ & ▁ b ▁ = ▁ " << ( a & b ) << endl ; cout << " a ▁ | ▁ b ▁ = ▁ " << ( a b ) << endl ; cout << " a ▁ ^ ▁ b ▁ = ▁ " << ( a ^ b ) << endl ; cout << " ~ ( " << a << " ) ▁ = ▁ " << ( ~ a ) << endl ; cout << " b ▁ < < ▁ 1" << " ▁ = ▁ " << ( b << 1 ) << endl ; cout << " b ▁ > > ▁ 1 ▁ " << " = ▁ " << ( b >> 1 ) << endl ; return 0 ; }
Convert a given temperature to another system based on given boiling and freezing points | C ++ program for above approach ; Function to return temperature in the second thermometer ; Calculate the temperature ; Driver Code
#include <iostream> NEW_LINE using namespace std ; double temp_convert ( int F1 , int B1 , int F2 , int B2 , int T ) { float t2 ; t2 = F2 + ( float ) ( B2 - F2 ) / ( B1 - F1 ) * ( T - F1 ) ; return t2 ; } int main ( ) { int F1 = 0 , B1 = 100 ; int F2 = 32 , B2 = 212 ; int T = 37 ; float t2 ; cout << temp_convert ( F1 , B1 , F2 , B2 , T ) ; return 0 ; }
Maximum possible elements which are divisible by 2 | CPP program to find maximum possible elements which divisible by 2 ; Function to find maximum possible elements which divisible by 2 ; To store count of even numbers ; All even numbers and half of odd numbers ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Divisible ( int arr [ ] , int n ) { int count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 0 ) count_even ++ ; return count_even + ( n - count_even ) / 2 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << Divisible ( arr , n ) ; return 0 ; }
Select a Random Node from a tree with equal probability | CPP 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 ; Driver code ; Creating Above Tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; int children ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; temp -> children = 0 ; return temp ; } int getElements ( Node * root ) { if ( ! root ) return 0 ; return getElements ( root -> left ) + getElements ( root -> right ) + 1 ; } void insertChildrenCount ( Node * & root ) { if ( ! root ) return ; root -> children = getElements ( root ) - 1 ; insertChildrenCount ( root -> left ) ; insertChildrenCount ( root -> right ) ; } int children ( Node * root ) { if ( ! root ) return 0 ; return root -> children + 1 ; } int randomNodeUtil ( Node * root , int count ) { if ( ! root ) 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 ) ; } int randomNode ( Node * root ) { srand ( time ( 0 ) ) ; int count = rand ( ) % ( root -> children + 1 ) ; return randomNodeUtil ( root , count ) ; } int main ( ) { Node * root = newNode ( 10 ) ; root -> left = newNode ( 20 ) ; root -> right = newNode ( 30 ) ; root -> left -> right = newNode ( 40 ) ; root -> left -> right = newNode ( 50 ) ; root -> right -> left = newNode ( 60 ) ; root -> right -> right = newNode ( 70 ) ; insertChildrenCount ( root ) ; cout << " A ▁ Random ▁ Node ▁ From ▁ Tree ▁ : ▁ " << randomNode ( root ) << endl ; return 0 ; }
Implement rand3 ( ) using rand2 ( ) | C ++ Program to print 0 , 1 or 2 with equal probability ; Random Function to that returns 0 or 1 with equal probability ; rand ( ) function will generate odd or even number with equal probability . If rand ( ) generates odd number , the function will return 1 else it will return 0. ; Random Function to that returns 0 , 1 or 2 with equal probability 1 with 75 % ; returns 0 , 1 , 2 or 3 with 25 % probability ; Driver code to test above functions
#include <iostream> NEW_LINE using namespace std ; int rand2 ( ) { return rand ( ) & 1 ; } int rand3 ( ) { int r = 2 * rand2 ( ) + rand2 ( ) ; if ( r < 3 ) return r ; return rand3 ( ) ; } int main ( ) { srand ( time ( NULL ) ) ; for ( int i = 0 ; i < 100 ; i ++ ) cout << rand3 ( ) ; return 0 ; }
Delete leaf nodes with value as x | CPP code to delete all leaves with given value . ; A binary tree node ; A utility function to allocate a new node ; deleteleaves ( ) ; inorder ( ) ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * newNode = new Node ; newNode -> data = data ; newNode -> left = newNode -> right = NULL ; return ( newNode ) ; } Node * deleteLeaves ( Node * root , int x ) { if ( root == NULL ) return nullptr ; root -> left = deleteLeaves ( root -> left , x ) ; root -> right = deleteLeaves ( root -> right , x ) ; if ( root -> data == x && root -> left == NULL && root -> right == NULL ) { return nullptr ; } return root ; } void inorder ( Node * root ) { if ( root == NULL ) return ; inorder ( root -> left ) ; cout << root -> data << " ▁ " ; inorder ( root -> right ) ; } int main ( void ) { struct Node * root = newNode ( 10 ) ; root -> left = newNode ( 3 ) ; root -> right = newNode ( 10 ) ; root -> left -> left = newNode ( 3 ) ; root -> left -> right = newNode ( 1 ) ; root -> right -> right = newNode ( 3 ) ; root -> right -> right -> left = newNode ( 3 ) ; root -> right -> right -> right = newNode ( 3 ) ; deleteLeaves ( root , 3 ) ; cout << " Inorder ▁ traversal ▁ after ▁ deletion ▁ : ▁ " ; inorder ( root ) ; return 0 ; }
Non | Non - Recursive Program to delete an entire binary tree . ; A Binary Tree Node ; Non - recursive function to delete an entire binary tree . ; Base Case ; Create an empty queue for level order traversal ; Do level order traversal starting from root ; Deletes a tree and sets the root as NULL ; Utility function to create a new tree Node ; Driver program to test above functions ; create a binary tree ; delete entire binary tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; void _deleteTree ( Node * root ) { if ( root == NULL ) return ; queue < Node * > q ; q . push ( root ) ; while ( ! q . empty ( ) ) { Node * node = q . front ( ) ; q . pop ( ) ; if ( node -> left != NULL ) q . push ( node -> left ) ; if ( node -> right != NULL ) q . push ( node -> right ) ; free ( node ) ; } } void deleteTree ( Node * * node_ref ) { _deleteTree ( * node_ref ) ; * node_ref = NULL ; } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { Node * root = newNode ( 15 ) ; root -> left = newNode ( 10 ) ; root -> right = newNode ( 20 ) ; root -> left -> left = newNode ( 8 ) ; root -> left -> right = newNode ( 12 ) ; root -> right -> left = newNode ( 16 ) ; root -> right -> right = newNode ( 25 ) ; deleteTree ( & root ) ; return 0 ; }
Iterative program to Calculate Size of a tree | C ++ program to print size of tree in iterative ; Node Structure ; A utility function to create a new Binary Tree Node ; return size of tree ; if tree is empty it will return 0 ; Using level order Traversal . ; when the queue is empty : the poll ( ) method returns null . ; Increment count ; Enqueue left child ; Increment count ; Enqueue right child ; Driver Code ; creating a binary tree and entering the nodes
#include <iostream> NEW_LINE #include <queue> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = NULL ; temp -> right = NULL ; return temp ; } int sizeoftree ( Node * root ) { if ( root == NULL ) return 0 ; queue < Node * > q ; int count = 1 ; q . push ( root ) ; while ( ! q . empty ( ) ) { Node * temp = q . front ( ) ; if ( temp -> left ) { count ++ ; q . push ( temp -> left ) ; } if ( temp -> right ) { count ++ ; q . push ( temp -> right ) ; } q . pop ( ) ; } return count ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; cout << " Size ▁ of ▁ the ▁ tree ▁ is ▁ " << sizeoftree ( root ) << endl ; return 0 ; }
Write a Program to Find the Maximum Depth or Height of a Tree | C ++ program to find height of tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Compute the " maxDepth " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the depth of each subtree ; use the larger one ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; } ; int maxDepth ( node * node ) { if ( node == NULL ) return 0 ; else { int lDepth = maxDepth ( node -> left ) ; int rDepth = maxDepth ( node -> right ) ; if ( lDepth > rDepth ) return ( lDepth + 1 ) ; else return ( rDepth + 1 ) ; } } node * newNode ( int data ) { node * Node = new node ( ) ; Node -> data = data ; Node -> left = NULL ; Node -> right = NULL ; return ( Node ) ; } int main ( ) { node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; cout << " Height ▁ of ▁ tree ▁ is ▁ " << maxDepth ( root ) ; return 0 ; }
Find postorder traversal of BST from preorder traversal | C ++ program for finding postorder traversal of BST from preorder traversal ; Function to find postorder traversal from preorder traversal . ; If entire preorder array is traversed then return as no more element is left to be added to post order array . ; If array element does not lie in range specified , then it is not part of current subtree . ; Store current value , to be printed later , after printing left and right subtrees . Increment preIndex to find left and right subtrees , and pass this updated value to recursive calls . ; All elements with value between minval and val lie in left subtree . ; All elements with value between val and maxval lie in right subtree . ; Function to find postorder traversal . ; To store index of element to be traversed next in preorder array . This is passed by reference to utility function . ; Driver code ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPostOrderUtil ( int pre [ ] , int n , int minval , int maxval , int & preIndex ) { if ( preIndex == n ) return ; if ( pre [ preIndex ] < minval pre [ preIndex ] > maxval ) { return ; } int val = pre [ preIndex ] ; preIndex ++ ; findPostOrderUtil ( pre , n , minval , val , preIndex ) ; findPostOrderUtil ( pre , n , val , maxval , preIndex ) ; cout << val << " ▁ " ; } void findPostOrder ( int pre [ ] , int n ) { int preIndex = 0 ; findPostOrderUtil ( pre , n , INT_MIN , INT_MAX , preIndex ) ; } int main ( ) { int pre [ ] = { 40 , 30 , 35 , 80 , 100 } ; int n = sizeof ( pre ) / sizeof ( pre [ 0 ] ) ; findPostOrder ( pre , n ) ; return 0 ; }