codeparrot/xlcost-text-to-code huggingface dataset:
Set all odd bits of a number | set all odd bit ; res for store 010101. . number ; generate number form of 010101. . ... till temp size ; if bit is odd , then generate number and or with res ; Driver code
def oddbitsetnumber ( n ) : NEW_LINE INDENT count = 0 NEW_LINE res = 0 NEW_LINE temp = n NEW_LINE while temp > 0 : NEW_LINE INDENT if count % 2 == 0 : NEW_LINE INDENT res |= ( 1 << count ) NEW_LINE DEDENT count += 1 NEW_LINE temp >>= 1 NEW_LINE DEDENT return ( n res ) NEW_LINE DEDENT n = 10 NEW_LINE print ( oddbitsetnumber ( n ) ) NEW_LINE
Set all odd bits of a number | Efficient python3 program to set all odd bits number ; return MSB set number ; set all bits including MSB . ; return MSB ; Returns a number of same size ( MSB at same position ) as n and all odd bits set . ; generate odd bits like 010101. . ; if bits is even then shift by 1 ; return odd set bits number ; set all odd bits here ; take OR with odd set bits number ; Driver Program
import math NEW_LINE def getmsb ( n ) : NEW_LINE INDENT n |= n >> 1 NEW_LINE n |= n >> 2 NEW_LINE n |= n >> 4 NEW_LINE n |= n >> 8 NEW_LINE n |= n >> 16 NEW_LINE return ( n + 1 ) >> 1 NEW_LINE DEDENT def getevenbits ( n ) : NEW_LINE INDENT n = getmsb ( n ) NEW_LINE n |= n >> 2 NEW_LINE n |= n >> 4 NEW_LINE n |= n >> 8 NEW_LINE n |= n >> 16 NEW_LINE if ( ( n & 1 ) == 0 ) : NEW_LINE INDENT n = n >> 1 NEW_LINE DEDENT return n NEW_LINE DEDENT def setalloddbits ( n ) : NEW_LINE INDENT return n | getevenbits ( n ) NEW_LINE DEDENT n = 10 NEW_LINE print ( setalloddbits ( n ) ) NEW_LINE
Print numbers in the range 1 to n having bits in alternate pattern | function to print numbers in the range 1 to nhaving bits in alternate pattern ; first number having bits in alternate pattern ; display ; loop until n < curr_num ; generate next number having alternate bit pattern ; if true then break ; display ; generate next number having alternate bit pattern ; if true then break ; display ; Driven code
def printNumHavingAltBitPatrn ( n ) : NEW_LINE INDENT curr_num = 1 NEW_LINE print ( curr_num ) NEW_LINE while ( 1 ) : NEW_LINE INDENT curr_num = curr_num << 1 ; NEW_LINE if ( n < curr_num ) : NEW_LINE INDENT break ; NEW_LINE DEDENT print ( curr_num ) NEW_LINE curr_num = ( ( curr_num ) << 1 ) ^ 1 ; NEW_LINE if ( n < curr_num ) : NEW_LINE INDENT break NEW_LINE DEDENT print ( curr_num ) NEW_LINE DEDENT DEDENT n = 50 NEW_LINE printNumHavingAltBitPatrn ( n ) NEW_LINE
Smallest perfect power of 2 greater than n ( without using arithmetic operators ) | Function to find smallest perfect power of 2 greater than n ; To store perfect power of 2 ; bitwise left shift by 1 ; bitwise right shift by 1 ; Required perfect power of 2 ; Driver program to test above
def perfectPowerOf2 ( n ) : NEW_LINE INDENT per_pow = 1 NEW_LINE while n > 0 : NEW_LINE INDENT per_pow = per_pow << 1 NEW_LINE n = n >> 1 NEW_LINE DEDENT return per_pow NEW_LINE DEDENT n = 128 NEW_LINE print ( " Perfect ▁ power ▁ of ▁ 2 ▁ greater ▁ than " , n , " : " , perfectPowerOf2 ( n ) ) NEW_LINE
Find Unique pair in an array with pairs of numbers | Python 3 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 . ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; Driver code
def findUniquePair ( arr , n ) : NEW_LINE INDENT XOR = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT XOR = XOR ^ arr [ i ] NEW_LINE DEDENT set_bit_no = XOR & ~ ( XOR - 1 ) NEW_LINE x = 0 NEW_LINE y = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] & set_bit_no ) : NEW_LINE INDENT x = x ^ arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT y = y ^ arr [ i ] NEW_LINE DEDENT DEDENT print ( " The ▁ unique ▁ pair ▁ is ▁ ( " , x , " , ▁ " , y , " ) " , sep = " " ) NEW_LINE DEDENT a = [ 6 , 1 , 3 , 5 , 1 , 3 , 7 , 6 ] NEW_LINE n = len ( a ) NEW_LINE findUniquePair ( a , n ) NEW_LINE
M | 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 Code
' NEW_LINE def nxtHighWithNumOfSetBits ( x ) : NEW_LINE INDENT rightOne = 0 NEW_LINE nextHigherOneBit = 0 NEW_LINE rightOnesPattern = 0 NEW_LINE next = 0 NEW_LINE if ( x ) : NEW_LINE INDENT rightOne = x & ( - x ) NEW_LINE nextHigherOneBit = x + rightOne NEW_LINE rightOnesPattern = x ^ nextHigherOneBit NEW_LINE rightOnesPattern = ( rightOnesPattern ) // rightOne NEW_LINE rightOnesPattern >>= 2 NEW_LINE next = nextHigherOneBit | rightOnesPattern NEW_LINE DEDENT return next NEW_LINE DEDENT def mthSmallestWithKSetBits ( m , k ) : NEW_LINE INDENT num = ( 1 << k ) - 1 NEW_LINE for i in range ( 1 , m ) : NEW_LINE INDENT num = nxtHighWithNumOfSetBits ( num ) NEW_LINE DEDENT return num NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = 6 NEW_LINE k = 4 NEW_LINE print ( mthSmallestWithKSetBits ( m , k ) ) NEW_LINE DEDENT
Count unset bits of a number | An optimized Python 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
import math NEW_LINE def countUnsetBits ( n ) : NEW_LINE INDENT x = n NEW_LINE n |= n >> 1 NEW_LINE n |= n >> 2 NEW_LINE n |= n >> 4 NEW_LINE n |= n >> 8 NEW_LINE n |= n >> 16 NEW_LINE t = math . log ( x ^ n , 2 ) NEW_LINE return math . floor ( t ) NEW_LINE DEDENT n = 17 NEW_LINE print ( countUnsetBits ( n ) ) NEW_LINE
Count total bits in a number | Function to get no of bits in binary representation of positive integer ; Driver program
def countBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT count += 1 NEW_LINE n >>= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT i = 65 NEW_LINE print ( countBits ( i ) ) NEW_LINE
Toggle all bits after most significant bit | 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
def toggle ( n ) : NEW_LINE INDENT temp = 1 NEW_LINE while ( temp <= n ) : NEW_LINE INDENT n = n ^ temp NEW_LINE temp = temp << 1 NEW_LINE DEDENT return n NEW_LINE DEDENT n = 10 NEW_LINE n = toggle ( n ) NEW_LINE print ( n ) NEW_LINE
Find the n | Python 3 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
INT_MAX = 2147483647 NEW_LINE def isKthBitSet ( x , k ) : NEW_LINE INDENT return 1 if ( x & ( 1 << ( k - 1 ) ) ) else 0 NEW_LINE DEDENT def leftmostSetBit ( x ) : NEW_LINE INDENT count = 0 NEW_LINE while ( x ) : NEW_LINE INDENT count += 1 NEW_LINE x = x >> 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def isBinPalindrome ( x ) : NEW_LINE INDENT l = leftmostSetBit ( x ) NEW_LINE r = 1 NEW_LINE while ( l > r ) : NEW_LINE INDENT if ( isKthBitSet ( x , l ) != isKthBitSet ( x , r ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT l -= 1 NEW_LINE r += 1 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def findNthPalindrome ( n ) : NEW_LINE INDENT pal_count = 0 NEW_LINE i = 0 NEW_LINE for i in range ( 1 , INT_MAX + 1 ) : NEW_LINE INDENT if ( isBinPalindrome ( i ) ) : NEW_LINE INDENT pal_count += 1 NEW_LINE DEDENT if ( pal_count == n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return i NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 9 NEW_LINE print ( findNthPalindrome ( n ) ) NEW_LINE DEDENT
Print ' K ' th least significant bit of a number | Function returns 1 if set , 0 if not ; Driver code ; Function call
def LSB ( num , K ) : NEW_LINE INDENT return bool ( num & ( 1 << ( K - 1 ) ) ) NEW_LINE DEDENT num , k = 10 , 4 NEW_LINE res = LSB ( num , k ) NEW_LINE if res : NEW_LINE INDENT print 1 NEW_LINE DEDENT else : NEW_LINE INDENT print 0 NEW_LINE DEDENT
Check if two numbers are equal without using comparison operators | Finds if a and b are same ; Driver code
def areSame ( a , b ) : NEW_LINE INDENT if ( not ( a - b ) ) : NEW_LINE INDENT print " Same " NEW_LINE DEDENT else : NEW_LINE INDENT print " Not ▁ Same " NEW_LINE DEDENT DEDENT areSame ( 10 , 20 ) NEW_LINE
Toggle bits in the given range | function to toggle bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; toggle bits in the range l to r in ' n ' Besides this , we can calculate num as : num = ( 1 << r ) - l . ; Driver code
def toggleBitsFromLToR ( n , l , r ) : NEW_LINE INDENT num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) NEW_LINE return ( n ^ num ) NEW_LINE DEDENT n = 50 NEW_LINE l = 2 NEW_LINE r = 5 NEW_LINE print ( toggleBitsFromLToR ( n , l , r ) ) NEW_LINE
Position of rightmost different bit | Python implementation to find the position of rightmost different bit ; Function to find the position of rightmost set bit in 'n ; to handle edge case when n = 0. ; Function to find the position of rightmost different bit in the binary representations of ' m ' and 'n ; position of rightmost different bit ; Driver code
import math NEW_LINE ' NEW_LINE def getRightMostSetBit ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return math . log2 ( n & - n ) + 1 NEW_LINE DEDENT ' NEW_LINE def posOfRightMostDiffBit ( m , n ) : NEW_LINE INDENT return getRightMostSetBit ( m ^ n ) NEW_LINE DEDENT m = 52 NEW_LINE n = 4 NEW_LINE print ( " position ▁ = ▁ " , int ( posOfRightMostDiffBit ( m , n ) ) ) NEW_LINE
Closest ( or Next ) smaller and greater numbers with same number of set bits | 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
def getNext ( n ) : NEW_LINE INDENT c = n NEW_LINE c0 = 0 NEW_LINE c1 = 0 NEW_LINE while ( ( ( c & 1 ) == 0 ) and ( c != 0 ) ) : NEW_LINE INDENT c0 = c0 + 1 NEW_LINE c >>= 1 NEW_LINE DEDENT while ( ( c & 1 ) == 1 ) : NEW_LINE INDENT c1 = c1 + 1 NEW_LINE c >>= 1 NEW_LINE DEDENT if ( c0 + c1 == 31 or c0 + c1 == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return n + ( 1 << c0 ) + ( 1 << ( c1 - 1 ) ) - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( getNext ( n ) ) NEW_LINE n = 8 NEW_LINE print ( getNext ( n ) ) NEW_LINE DEDENT
Count minimum bits to flip such that XOR of A and B equal to C | Python code to find minimum bits to be flip ; If both A [ i ] and B [ i ] are equal ; if A [ i ] and B [ i ] are unequal ; N represent total count of Bits
def totalFlips ( A , B , C , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if A [ i ] == B [ i ] and C [ i ] == '1' : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT elif A [ i ] != B [ i ] and C [ i ] == '0' : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT N = 5 NEW_LINE a = "10100" NEW_LINE b = "00010" NEW_LINE c = "10011" NEW_LINE print ( totalFlips ( a , b , c , N ) ) NEW_LINE
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 [ 0 ] ; After this , c has value of b ; After this , a [ 0 ] has value of c ; Driver code ; Calling Function
def swapThree ( a , b , c ) : NEW_LINE INDENT a [ 0 ] = a [ 0 ] ^ b [ 0 ] ^ c [ 0 ] NEW_LINE b [ 0 ] = a [ 0 ] ^ b [ 0 ] ^ c [ 0 ] NEW_LINE c [ 0 ] = a [ 0 ] ^ b [ 0 ] ^ c [ 0 ] NEW_LINE a [ 0 ] = a [ 0 ] ^ b [ 0 ] ^ c [ 0 ] NEW_LINE DEDENT a , b , c = [ 10 ] , [ 20 ] , [ 30 ] NEW_LINE print ( " Before ▁ swapping ▁ a ▁ = ▁ " , a [ 0 ] , " , ▁ b ▁ = ▁ " , b [ 0 ] , " , ▁ c ▁ = ▁ " , c [ 0 ] ) NEW_LINE swapThree ( a , b , c ) NEW_LINE print ( " After ▁ swapping ▁ a ▁ = ▁ " , a [ 0 ] , " , ▁ b ▁ = ▁ " , b [ 0 ] , " , ▁ c ▁ = ▁ " , c [ 0 ] ) NEW_LINE
Find Two Missing Numbers | Set 2 ( XOR based solution ) | 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 . ; 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
def findTwoMissingNumbers ( arr , n ) : NEW_LINE INDENT XOR = arr [ 0 ] NEW_LINE for i in range ( 1 , n - 2 ) : NEW_LINE INDENT XOR ^= arr [ i ] NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT XOR ^= i NEW_LINE DEDENT set_bit_no = XOR & ~ ( XOR - 1 ) NEW_LINE x = 0 NEW_LINE y = 0 NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT if arr [ i ] & set_bit_no : NEW_LINE INDENT x = x ^ arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT y = y ^ arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if i & set_bit_no : NEW_LINE INDENT x = x ^ i NEW_LINE DEDENT else : NEW_LINE INDENT y = y ^ i NEW_LINE DEDENT DEDENT print ( " Two Missing Numbers are % d % d " % ( x , y ) ) NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 6 ] NEW_LINE n = 2 + len ( arr ) NEW_LINE findTwoMissingNumbers ( arr , n ) NEW_LINE
Find XOR of two number without using XOR operator | Returns XOR of x and y ; Driver Code
def myXOR ( x , y ) : NEW_LINE INDENT return ( x & ( ~ y ) ) | ( ( ~ x ) & y ) NEW_LINE DEDENT x = 3 NEW_LINE y = 5 NEW_LINE print ( " XOR ▁ is " , myXOR ( x , y ) ) NEW_LINE
Convert a given temperature to another system based on given boiling and freezing points | Function to return temperature in the second thermometer ; Calculate the temperature ; Driver Code
def temp_convert ( F1 , B1 , F2 , B2 , T ) : NEW_LINE INDENT t2 = F2 + ( ( float ) ( B2 - F2 ) / ( B1 - F1 ) * ( T - F1 ) ) NEW_LINE return t2 NEW_LINE DEDENT F1 = 0 NEW_LINE B1 = 100 NEW_LINE F2 = 32 NEW_LINE B2 = 212 NEW_LINE T = 37 NEW_LINE print ( temp_convert ( F1 , B1 , F2 , B2 , T ) ) NEW_LINE
Maximum possible elements which are 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
def Divisible ( arr , n ) : NEW_LINE INDENT count_even = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT count_even += 1 NEW_LINE DEDENT DEDENT return count_even + ( n - count_even ) // 2 NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( Divisible ( arr , n ) ) NEW_LINE
Select a Random Node from a tree with equal probability | Python3 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
from random import randint NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . children = 0 NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def getElements ( root ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( getElements ( root . left ) + getElements ( root . right ) + 1 ) NEW_LINE DEDENT def insertChildrenCount ( root ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return NEW_LINE DEDENT root . children = getElements ( root ) - 1 NEW_LINE insertChildrenCount ( root . left ) NEW_LINE insertChildrenCount ( root . right ) NEW_LINE DEDENT def children ( root ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return 0 NEW_LINE DEDENT return root . children + 1 NEW_LINE DEDENT def randomNodeUtil ( root , count ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return 0 NEW_LINE DEDENT if count == children ( root . left ) : NEW_LINE INDENT return root . data NEW_LINE DEDENT if count < children ( root . left ) : NEW_LINE INDENT return randomNodeUtil ( root . left , count ) NEW_LINE DEDENT return randomNodeUtil ( root . right , count - children ( root . left ) - 1 ) NEW_LINE DEDENT def randomNode ( root ) : NEW_LINE INDENT count = randint ( 0 , root . children ) NEW_LINE return randomNodeUtil ( root , count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 10 ) NEW_LINE root . left = Node ( 20 ) NEW_LINE root . right = Node ( 30 ) NEW_LINE root . left . right = Node ( 40 ) NEW_LINE root . left . right = Node ( 50 ) NEW_LINE root . right . left = Node ( 60 ) NEW_LINE root . right . right = Node ( 70 ) NEW_LINE insertChildrenCount ( root ) NEW_LINE print ( " A ▁ Random ▁ Node ▁ From ▁ Tree ▁ : " , randomNode ( root ) ) NEW_LINE DEDENT
Implement rand3 ( ) using rand2 ( ) | Python3 Program to print 0 , 1 or 2 with equal Probability ; Random Function to that returns 0 or 1 with equal probability ; randint ( 0 , 100 ) function will generate odd or even number [ 1 , 100 ] 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
import random NEW_LINE def rand2 ( ) : NEW_LINE INDENT tmp = random . randint ( 1 , 100 ) NEW_LINE return tmp % 2 NEW_LINE DEDENT def rand3 ( ) : NEW_LINE INDENT r = 2 * rand2 ( ) + rand2 ( ) NEW_LINE if r < 3 : NEW_LINE INDENT return r NEW_LINE DEDENT return rand3 ( ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT for i in range ( 100 ) : NEW_LINE INDENT print ( rand3 ( ) , end = " " ) NEW_LINE DEDENT DEDENT
Delete leaf nodes with value as x | A utility class to allocate a new node ; deleteleaves ( ) ; inorder ( ) ; Driver Code
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 deleteLeaves ( root , x ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT root . left = deleteLeaves ( root . left , x ) NEW_LINE root . right = deleteLeaves ( root . right , x ) NEW_LINE if ( root . data == x and root . left == None and root . right == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT return root NEW_LINE DEDENT def inorder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT inorder ( root . left ) NEW_LINE print ( root . data , end = " ▁ " ) NEW_LINE inorder ( root . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 3 ) NEW_LINE root . right = newNode ( 10 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 1 ) NEW_LINE root . right . right = newNode ( 3 ) NEW_LINE root . right . right . left = newNode ( 3 ) NEW_LINE root . right . right . right = newNode ( 3 ) NEW_LINE deleteLeaves ( root , 3 ) NEW_LINE print ( " Inorder ▁ traversal ▁ after ▁ deletion ▁ : ▁ " ) NEW_LINE inorder ( root ) NEW_LINE DEDENT
Non | A binary tree node ; Non - recursive function to delete an entrie binary tree ; Base Case ; Create a empty queue for level order traversal ; Do level order traversal starting from root ; Deletes a tree and sets the root as None ; Create a binary tree ; delete entire binary tree
class Node : 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 _deleteTree ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT node = q . pop ( 0 ) NEW_LINE if node . left is not None : NEW_LINE INDENT q . append ( node . left ) NEW_LINE DEDENT if node . right is not None : NEW_LINE INDENT q . append ( node . right ) NEW_LINE DEDENT node = None NEW_LINE DEDENT return node NEW_LINE DEDENT def deleteTree ( node_ref ) : NEW_LINE INDENT node_ref = _deleteTree ( node_ref ) NEW_LINE return node_ref NEW_LINE DEDENT root = Node ( 15 ) NEW_LINE root . left = Node ( 10 ) NEW_LINE root . right = Node ( 20 ) NEW_LINE root . left . left = Node ( 8 ) NEW_LINE root . left . right = Node ( 12 ) NEW_LINE root . right . left = Node ( 16 ) NEW_LINE root . right . right = Node ( 25 ) NEW_LINE root = deleteTree ( root ) NEW_LINE
Iterative program to Calculate Size of a tree | Node Structure ; Return size of tree ; if tree is empty it will return 0 ; Using level order Traversal . ; when the queue is empty : the pop ( ) method returns null . ; Increment count ; Enqueue left child ; Increment count ; Enqueue right child ; creating a binary tree and entering the nodes
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 sizeoftree ( root ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return 0 NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE count = 1 NEW_LINE while ( len ( q ) != 0 ) : NEW_LINE INDENT root = q . pop ( 0 ) NEW_LINE if ( root . left ) : NEW_LINE INDENT count += 1 NEW_LINE q . append ( root . left ) NEW_LINE DEDENT if ( root . right ) : NEW_LINE INDENT count += 1 NEW_LINE q . append ( root . right ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT 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 print ( sizeoftree ( root ) ) NEW_LINE
Write a Program to Find the Maximum Depth or Height of a Tree | A binary tree node ; 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 ; Driver program to test above function
class Node : 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 maxDepth ( node ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT lDepth = maxDepth ( node . left ) NEW_LINE rDepth = maxDepth ( node . right ) NEW_LINE if ( lDepth > rDepth ) : NEW_LINE INDENT return lDepth + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return rDepth + 1 NEW_LINE DEDENT 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 root . left . right = Node ( 5 ) NEW_LINE print ( " Height ▁ of ▁ tree ▁ is ▁ % d " % ( maxDepth ( root ) ) ) NEW_LINE