codeparrot/xlcost-text-to-code huggingface dataset:
Check if Bitwise AND of concatenation of diagonals exceeds that of middle row / column elements of a Binary Matrix | Functio to convert obtained binary representation to decimal value ; Stores the resultant number ; Traverse string arr ; Return the number formed ; Function to count the number of set bits in the number num ; Stores the count of set bits ; Iterate until num > 0 ; Function to check if the given matrix satisfies the given condition or not ; To get P , S , MR , and MC ; Stores decimal equivalents of binary representations ; Gett the number of set bits ; Print the answer ; Driver Code
def convert ( arr ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in arr : NEW_LINE INDENT ans = ( ans << 1 ) | i NEW_LINE DEDENT return ans NEW_LINE DEDENT def count ( num ) : NEW_LINE INDENT ans = 0 NEW_LINE while num : NEW_LINE INDENT ans += num & 1 NEW_LINE num >>= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def checkGoodMatrix ( mat ) : NEW_LINE INDENT P = [ ] NEW_LINE S = [ ] NEW_LINE MR = [ ] NEW_LINE MC = [ ] NEW_LINE for i in range ( len ( mat ) ) : NEW_LINE INDENT for j in range ( len ( mat [ 0 ] ) ) : NEW_LINE INDENT if i == j : NEW_LINE INDENT P . append ( mat [ i ] [ j ] ) NEW_LINE DEDENT if i + j == len ( mat ) - 1 : NEW_LINE INDENT S . append ( mat [ i ] [ j ] ) NEW_LINE DEDENT if i == ( len ( mat ) - 1 ) // 2 : NEW_LINE INDENT MR . append ( mat [ i ] [ j ] ) NEW_LINE DEDENT if j == ( len ( mat ) - 1 ) // 2 : NEW_LINE INDENT MC . append ( mat [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT S . reverse ( ) NEW_LINE P = convert ( P ) NEW_LINE S = convert ( S ) NEW_LINE MR = convert ( MR ) NEW_LINE MC = convert ( MC ) NEW_LINE setBitsPS = count ( P & S ) NEW_LINE setBitsMM = count ( MR & MC ) NEW_LINE if setBitsPS > setBitsMM : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT mat = [ [ 1 , 0 , 1 ] , [ 0 , 0 , 1 ] , [ 0 , 1 , 1 ] ] NEW_LINE checkGoodMatrix ( mat ) NEW_LINE
Construct a Tree whose sum of nodes of all the root to leaf path is not divisible by the count of nodes in that path | Python3 program for the above approach ; Function to assign values to nodes of the tree s . t . sum of values of nodes of path between any 2 nodes is not divisible by length of path ; Stores the adjacency list ; Create a adjacency list ; Stores whether any node is visited or not ; Stores the node values ; Variable used to assign values to the nodes alternatively to the parent child ; Declare a queue ; Push the 1 st node ; Assign K value to this node ; Dequeue the node ; Mark it as visited ; Upgrade the value of K ; Assign K to the child nodes ; If the child is unvisited ; Enqueue the child ; Assign K to the child ; Print the value assigned to the nodes ; Driver Code ; Function Call
from collections import deque NEW_LINE def assignValues ( Edges , n ) : NEW_LINE INDENT tree = [ [ ] for i in range ( n + 1 ) ] NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT u = Edges [ i ] [ 0 ] NEW_LINE v = Edges [ i ] [ 1 ] NEW_LINE tree [ u ] . append ( v ) NEW_LINE tree [ v ] . append ( u ) NEW_LINE DEDENT visited = [ False ] * ( n + 1 ) NEW_LINE answer = [ 0 ] * ( n + 1 ) NEW_LINE K = 1 NEW_LINE q = deque ( ) NEW_LINE q . append ( 1 ) NEW_LINE answer [ 1 ] = K NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT node = q . popleft ( ) NEW_LINE visited [ node ] = True NEW_LINE K = 2 if ( answer [ node ] == 1 ) else 1 NEW_LINE for child in tree [ node ] : NEW_LINE INDENT if ( not visited [ child ] ) : NEW_LINE INDENT q . append ( child ) NEW_LINE answer [ child ] = K NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( answer [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 NEW_LINE Edges = [ [ 1 , 2 ] , [ 4 , 6 ] , [ 3 , 5 ] , [ 1 , 4 ] , [ 7 , 5 ] , [ 5 , 1 ] ] NEW_LINE assignValues ( Edges , N ) NEW_LINE DEDENT
Number of full binary trees such that each node is product of its children | Return the number of all possible full binary tree with given product property . ; Finding the minimum and maximum values in given array . ; Marking the presence of each array element and initialising the number of possible full binary tree for each integer equal to 1 because single node will also contribute as a full binary tree . ; From minimum value to maximum value of array finding the number of all possible Full Binary Trees . ; Find if value present in the array ; For each multiple of i , less than equal to maximum value of array ; If multiple is not present in the array then continue . ; Finding the number of possible Full binary trees for multiple j by multiplying number of possible Full binary tree from the number i and number of possible Full binary tree from i / j . ; Condition for possiblity when left chid became right child and vice versa . ; Driver Code
def numoffbt ( arr , n ) : NEW_LINE INDENT maxvalue = - 2147483647 NEW_LINE minvalue = 2147483647 NEW_LINE for i in range ( n ) : NEW_LINE INDENT maxvalue = max ( maxvalue , arr [ i ] ) NEW_LINE minvalue = min ( minvalue , arr [ i ] ) NEW_LINE DEDENT mark = [ 0 for i in range ( maxvalue + 2 ) ] NEW_LINE value = [ 0 for i in range ( maxvalue + 2 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT mark [ arr [ i ] ] = 1 NEW_LINE value [ arr [ i ] ] = 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( minvalue , maxvalue + 1 ) : NEW_LINE INDENT if ( mark [ i ] != 0 ) : NEW_LINE INDENT j = i + i NEW_LINE while ( j <= maxvalue and j // i <= i ) : NEW_LINE INDENT if ( mark [ j ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT value [ j ] = value [ j ] + ( value [ i ] * value [ j // i ] ) NEW_LINE if ( i != j // i ) : NEW_LINE INDENT value [ j ] = value [ j ] + ( value [ i ] * value [ j // i ] ) NEW_LINE DEDENT j += i NEW_LINE DEDENT DEDENT ans += value [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( numoffbt ( arr , n ) ) NEW_LINE
Minimum prime numbers required to be subtracted to make all array elements equal | Python3 program for the above approach ; Stores the sieve of prime numbers ; Function that performs the Sieve of Eratosthenes ; Iterate over the range [ 2 , 1000 ] ; If the current element is a prime number ; Mark all its multiples as false ; Function to find the minimum number of subtraction of primes numbers required to make all array elements the same ; Perform sieve of eratosthenes ; Find the minimum value ; Stores the value to each array element should be reduced ; If an element exists with value ( M + 1 ) ; Stores the minimum count of subtraction of prime numbers ; Traverse the array ; If D is equal to 0 ; If D is a prime number ; Increase count by 1 ; If D is an even number ; Increase count by 2 ; If D - 2 is prime ; Increase count by 2 ; Otherwise , increase count by 3 ; Driver Code
import sys NEW_LINE limit = 100000 NEW_LINE prime = [ True ] * ( limit + 1 ) NEW_LINE def sieve ( ) : NEW_LINE INDENT p = 2 NEW_LINE while ( p * p <= limit ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , limit , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def findOperations ( arr , n ) : NEW_LINE INDENT sieve ( ) NEW_LINE minm = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT minm = min ( minm , arr [ i ] ) NEW_LINE DEDENT val = minm NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == minm + 1 ) : NEW_LINE INDENT val = minm - 2 NEW_LINE break NEW_LINE DEDENT DEDENT cnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT D = arr [ i ] - val NEW_LINE if ( D == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( prime [ D ] == True ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT elif ( D % 2 == 0 ) : NEW_LINE INDENT cnt += 2 NEW_LINE DEDENT else : NEW_LINE INDENT if ( prime [ D - 2 ] == True ) : NEW_LINE INDENT cnt += 2 NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 3 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT arr = [ 7 , 10 , 4 , 5 ] NEW_LINE N = 4 NEW_LINE print ( findOperations ( arr , N ) ) NEW_LINE
Print all unique digits present in concatenation of all array elements in the order of their occurrence | Function to print unique elements ; Reverse the list ; Traverse the list ; Function which check for all unique digits ; Stores the final number ; Stores the count of unique digits ; Converting string to integer to remove leading zeros ; Stores count of digits ; Iterate over the digits of N ; Retrieve the last digit of N ; Increase the count of the last digit ; Remove the last digit of N ; Converting string to integer again ; Iterate over the digits of N ; Retrieve the last digit of N ; If the value of this digit is not visited ; If its frequency is 1 ( unique ) ; Mark the digit visited ; Remove the last digit of N ; Passing this list to print the reversed list ; Function to concatenate array elements ; Stores the concatenated number ; Traverse the array ; Convert to equivalent string ; Concatenate the string ; Passing string to checkUnique function ; Driver Code ; Function call to print unique digits present in the concatenation of array elements
def printUnique ( lis ) : NEW_LINE INDENT lis . reverse ( ) NEW_LINE for i in lis : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT def checkUnique ( string ) : NEW_LINE INDENT lis = [ ] NEW_LINE res = 0 NEW_LINE N = int ( string ) NEW_LINE cnt = [ 0 ] * 10 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT rem = N % 10 NEW_LINE cnt [ rem ] += 1 NEW_LINE N = N // 10 NEW_LINE DEDENT N = int ( string ) NEW_LINE while ( N > 0 ) : NEW_LINE INDENT rem = N % 10 NEW_LINE if ( cnt [ rem ] != ' visited ' ) : NEW_LINE INDENT if ( cnt [ rem ] == 1 ) : NEW_LINE INDENT lis . append ( rem ) NEW_LINE DEDENT DEDENT cnt [ rem ] = ' visited ' NEW_LINE N = N // 10 NEW_LINE DEDENT printUnique ( lis ) NEW_LINE DEDENT def combineArray ( lis ) : NEW_LINE INDENT string = " " NEW_LINE for el in lis : NEW_LINE INDENT el = str ( el ) NEW_LINE string = string + el NEW_LINE DEDENT checkUnique ( string ) NEW_LINE DEDENT arr = [ 122 , 474 , 612 , 932 ] NEW_LINE combineArray ( arr ) NEW_LINE
Maximize sum of Bitwise AND of same | Function to calculate sum of Bitwise AND of same indexed elements of the arrays p [ ] and arr [ ] ; Stores the resultant sum ; Traverse the array ; Update sum of Bitwise AND ; Return the value obtained ; Function to generate all permutations and calculate the maximum sum of Bitwise AND of same indexed elements present in any permutation and an array arr [ ] ; If the size of the array is N ; Calculate cost of permutation ; Generate all permutations ; Update chosen [ i ] ; Update the permutation p [ ] ; Generate remaining permutations ; Return the resultant sum ; Function to find the maximum sum of Bitwise AND of same indexed elements in a permutation of first N natural numbers and arr [ ] ; Stores the resultant maximum sum ; Stores the generated permutation P ; Function call to store result ; Print the result ; Driver Code ; Function Call
def calcScore ( p , arr ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT ans += ( p [ i ] & arr [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def getMaxUtil ( p , arr , ans , chosen , N ) : NEW_LINE INDENT if len ( p ) == N : NEW_LINE INDENT ans = max ( ans , calcScore ( p , arr ) ) NEW_LINE return ans NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if chosen [ i ] : NEW_LINE INDENT continue NEW_LINE DEDENT chosen [ i ] = True NEW_LINE p . append ( i ) NEW_LINE ans = getMaxUtil ( p , arr , ans , chosen , N ) NEW_LINE chosen [ i ] = False NEW_LINE p . pop ( ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def getMax ( arr , N ) : NEW_LINE INDENT ans = 0 NEW_LINE chosen = [ False for i in range ( N ) ] NEW_LINE p = [ ] NEW_LINE res = getMaxUtil ( p , arr , ans , chosen , N ) NEW_LINE print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 2 , 3 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE getMax ( arr , N ) NEW_LINE DEDENT
Sum of array elements whose count of set bits are unique | Function to count the number of set bits in an integer N ; Stores the count of set bits ; Iterate until N is non - zero ; Stores the resultant count ; Function to calculate sum of all array elements whose count of set bits are unique ; Stores frequency of all possible count of set bits ; Stores the sum of array elements ; Traverse the array ; Count the number of set bits ; Traverse the array and Update the value of ans ; If frequency is 1 ; Driver Code
def setBitCount ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE while n : NEW_LINE INDENT ans += n & 1 NEW_LINE n >>= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def getSum ( arr ) : NEW_LINE INDENT mp = { } NEW_LINE ans = 0 NEW_LINE for i in arr : NEW_LINE INDENT key = setBitCount ( i ) NEW_LINE mp [ key ] = [ 0 , i ] NEW_LINE DEDENT for i in arr : NEW_LINE INDENT key = setBitCount ( i ) NEW_LINE mp [ key ] [ 0 ] += 1 NEW_LINE DEDENT for i in mp : NEW_LINE INDENT if mp [ i ] [ 0 ] == 1 : NEW_LINE INDENT ans += mp [ i ] [ 1 ] NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 8 , 3 , 7 , 5 , 3 ] NEW_LINE getSum ( arr ) NEW_LINE
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | Function to check if array can be split into three equal sum subarrays by removing a pair ; Stores prefix sum array ; Traverse the array ; Stores sums of all three subarrays ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Check if sum of subarrays are equal ; Prthe possible pair ; If no such pair exists , pr - 1 ; Driver Code ; Given array ; Size of the array
def findSplit ( arr , N ) : NEW_LINE INDENT sum = [ i for i in arr ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT sum [ i ] += sum [ i - 1 ] NEW_LINE DEDENT for l in range ( 1 , N - 3 ) : NEW_LINE INDENT for r in range ( l + 2 , N - 1 ) : NEW_LINE INDENT lsum , rsum , msum = 0 , 0 , 0 NEW_LINE lsum = sum [ l - 1 ] NEW_LINE msum = sum [ r - 1 ] - sum [ l ] NEW_LINE rsum = sum [ N - 1 ] - sum [ r ] NEW_LINE if ( lsum == rsum and rsum == msum ) : NEW_LINE INDENT print ( l , r ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 5 , 12 , 7 , 19 , 4 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE findSplit ( arr , N ) NEW_LINE DEDENT
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | Function to check if array can be split into three equal sum subarrays by removing a pair ; Two pointers l and r ; Stores prefix sum array ; Traverse the array ; Two pointer approach ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Print split indices if sum is equal ; Move left pointer if lsum < rsum ; Move right pointer if rsum > lsum ; Move both pointers if lsum = rsum but they are not equal to msum ; If no possible pair exists , print - 1 ; Driver Code ; Given array ; Size of the array
def findSplit ( arr , N ) : NEW_LINE INDENT l = 1 ; r = N - 2 ; NEW_LINE sum = [ 0 ] * N ; NEW_LINE sum [ 0 ] = arr [ 0 ] ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT sum [ i ] = sum [ i - 1 ] + arr [ i ] ; NEW_LINE DEDENT while ( l < r ) : NEW_LINE INDENT lsum = sum [ l - 1 ] ; NEW_LINE msum = sum [ r - 1 ] - sum [ l ] ; NEW_LINE rsum = sum [ N - 1 ] - sum [ r ] ; NEW_LINE if ( lsum == msum and msum == rsum ) : NEW_LINE INDENT print ( l , r ) ; NEW_LINE return ; NEW_LINE DEDENT if ( lsum < rsum ) : NEW_LINE INDENT l += 1 ; NEW_LINE DEDENT elif ( lsum > rsum ) : NEW_LINE INDENT r -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT l += 1 ; NEW_LINE r -= 1 ; NEW_LINE DEDENT DEDENT print ( - 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 5 , 12 , 7 , 19 , 4 , 3 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE findSplit ( arr , N ) ; NEW_LINE DEDENT
Number of subtrees having odd count of even numbers | Helper class that allocates a new Node with the given data and None left and right pointers . ; Returns count of subtrees having odd count of even numbers ; base condition ; count even nodes in left subtree ; Add even nodes in right subtree ; Check if root data is an even number ; if total count of even numbers for the subtree is odd ; Total count of even nodes of the subtree ; A wrapper over countRec ( ) ; Driver Code ; binary tree formation 2 ; / \ ; 1 3 ; / \ / \ ; 4 10 8 5 ; / ; 6 ;
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def countRec ( root , pcount ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT c = countRec ( root . left , pcount ) NEW_LINE c += countRec ( root . right , pcount ) NEW_LINE if ( root . data % 2 == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if c % 2 != 0 : NEW_LINE INDENT pcount [ 0 ] += 1 NEW_LINE DEDENT return c NEW_LINE DEDENT def countSubtrees ( root ) : NEW_LINE INDENT count = [ 0 ] NEW_LINE pcount = count NEW_LINE countRec ( root , pcount ) NEW_LINE return count [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 2 ) NEW_LINE root . left = newNode ( 1 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 10 ) NEW_LINE root . right . left = newNode ( 8 ) NEW_LINE root . right . right = newNode ( 5 ) NEW_LINE root . left . right . left = newNode ( 6 ) NEW_LINE print ( " Count ▁ = ▁ " , countSubtrees ( root ) ) NEW_LINE DEDENT
Inorder Successor of a node in Binary Tree | A Binary Tree Node Utility function to create a new tree node ; function to find left most node in a tree ; function to find right most node in a tree ; recursive function to find the Inorder Scuccessor when the right child of node x is None ; function to find inorder successor of a node ; Case1 : If right child is not None ; Case2 : If right child is None ; case3 : If x is the right most node ; Driver Code ; Case 1 ; case 2 ; case 3
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def leftMostNode ( node ) : NEW_LINE INDENT while ( node != None and node . left != None ) : NEW_LINE INDENT node = node . left NEW_LINE DEDENT return node NEW_LINE DEDENT def rightMostNode ( node ) : NEW_LINE INDENT while ( node != None and node . right != None ) : NEW_LINE INDENT node = node . right NEW_LINE DEDENT return node NEW_LINE DEDENT def findInorderRecursive ( root , x ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return None NEW_LINE DEDENT if ( root == x or ( findInorderRecursive ( root . left , x ) ) or ( findInorderRecursive ( root . right , x ) ) ) : NEW_LINE INDENT if findInorderRecursive ( root . right , x ) : NEW_LINE INDENT temp = findInorderRecursive ( root . right , x ) NEW_LINE DEDENT else : NEW_LINE INDENT temp = findInorderRecursive ( root . left , x ) NEW_LINE DEDENT if ( temp ) : NEW_LINE INDENT if ( root . left == temp ) : NEW_LINE INDENT print ( " Inorder ▁ Successor ▁ of " , x . data , end = " " ) NEW_LINE print ( " ▁ is " , root . data ) NEW_LINE return None NEW_LINE DEDENT DEDENT return root NEW_LINE DEDENT return None NEW_LINE DEDENT def inorderSuccesor ( root , x ) : NEW_LINE INDENT if ( x . right != None ) : NEW_LINE INDENT inorderSucc = leftMostNode ( x . right ) NEW_LINE print ( " Inorder ▁ Successor ▁ of " , x . data , " is " , end = " ▁ " ) NEW_LINE print ( inorderSucc . data ) NEW_LINE DEDENT if ( x . right == None ) : NEW_LINE INDENT f = 0 NEW_LINE rightMost = rightMostNode ( root ) NEW_LINE if ( rightMost == x ) : NEW_LINE INDENT print ( " No ▁ inorder ▁ successor ! " , " Right ▁ most ▁ node . " ) NEW_LINE DEDENT else : NEW_LINE INDENT findInorderRecursive ( root , x ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 6 ) NEW_LINE inorderSuccesor ( root , root . right ) NEW_LINE inorderSuccesor ( root , root . left . left ) NEW_LINE inorderSuccesor ( root , root . right . right ) NEW_LINE DEDENT
Binary Tree | Set 1 ( Introduction ) | Class containing left and right child of current node and key value ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . left = None NEW_LINE self . right = None NEW_LINE self . val = key NEW_LINE DEDENT DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) ; NEW_LINE root . right = Node ( 3 ) ; NEW_LINE root . left . left = Node ( 4 ) ; NEW_LINE
Find distance from root to given node in a binary tree | A class to create a new Binary Tree Node ; Returns - 1 if x doesn 't exist in tree. Else returns distance of x from root ; Base case ; Initialize distance ; Check if x is present at root or in left subtree or right subtree . ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , item ) : NEW_LINE INDENT self . data = item NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def findDistance ( root , x ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT dist = - 1 NEW_LINE if ( root . data == x ) : NEW_LINE INDENT return dist + 1 NEW_LINE DEDENT else : NEW_LINE INDENT dist = findDistance ( root . left , x ) NEW_LINE if dist >= 0 : NEW_LINE INDENT return dist + 1 NEW_LINE DEDENT else : NEW_LINE INDENT dist = findDistance ( root . right , x ) NEW_LINE if dist >= 0 : NEW_LINE INDENT return dist + 1 NEW_LINE DEDENT DEDENT DEDENT return dist NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 5 ) NEW_LINE root . left = newNode ( 10 ) NEW_LINE root . right = newNode ( 15 ) NEW_LINE root . left . left = newNode ( 20 ) NEW_LINE root . left . right = newNode ( 25 ) NEW_LINE root . left . right . right = newNode ( 45 ) NEW_LINE root . right . left = newNode ( 30 ) NEW_LINE root . right . right = newNode ( 35 ) NEW_LINE print ( findDistance ( root , 45 ) ) NEW_LINE DEDENT