codeparrot/xlcost-text-to-code huggingface dataset:
Lowest Common Ancestor in a Binary Tree | Set 1 | A binary tree node ; This function retturn pointer to LCA of two given values n1 and n2 v1 is set as true by this function if n1 is found v2 is set as true by this function if n2 is found ; Base Case ; IF either n1 or n2 matches ith root 's key, report  the presence by setting v1 or v2 as true and return  root (Note that if a key is ancestor of other, then the ancestor key becomes LCA) ; Look for keys in left and right subtree ; If both of the above calls return Non - NULL , then one key is present in once subtree and other is present in other , So this node is the LCA ; Otherwise check if left subtree or right subtree is LCA ; Returns true if key k is present in tree rooted with root ; This function returns LCA of n1 and n2 onlue if both n1 and n2 are present in tree , otherwise returns None ; Initialize n1 and n2 as not visited ; Find lac of n1 and n2 using the technique discussed above ; Returns LCA only if both n1 and n2 are present in tree ; Else return None ; Driver program to test above function
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def findLCAUtil ( root , n1 , n2 , v ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return None NEW_LINE DEDENT if root . key == n1 : NEW_LINE INDENT v [ 0 ] = True NEW_LINE return root NEW_LINE DEDENT if root . key == n2 : NEW_LINE INDENT v [ 1 ] = True NEW_LINE return root NEW_LINE DEDENT left_lca = findLCAUtil ( root . left , n1 , n2 , v ) NEW_LINE right_lca = findLCAUtil ( root . right , n1 , n2 , v ) NEW_LINE if left_lca and right_lca : NEW_LINE INDENT return root NEW_LINE DEDENT return left_lca if left_lca is not None else right_lca NEW_LINE DEDENT def find ( root , k ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return False NEW_LINE DEDENT if ( root . key == k or find ( root . left , k ) or find ( root . right , k ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def findLCA ( root , n1 , n2 ) : NEW_LINE INDENT v = [ False , False ] NEW_LINE lca = findLCAUtil ( root , n1 , n2 , v ) NEW_LINE if ( v [ 0 ] and v [ 1 ] or v [ 0 ] and find ( lca , n2 ) or v [ 1 ] and find ( lca , n1 ) ) : NEW_LINE INDENT return lca NEW_LINE DEDENT return None NEW_LINE 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 root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE lca = findLCA ( root , 4 , 5 ) NEW_LINE if lca is not None : NEW_LINE INDENT print " LCA ( 4 , ▁ 5 ) ▁ = ▁ " , lca . key NEW_LINE DEDENT else : NEW_LINE INDENT print " Keys ▁ are ▁ not ▁ present " NEW_LINE DEDENT lca = findLCA ( root , 4 , 10 ) NEW_LINE if lca is not None : NEW_LINE INDENT print " LCA ( 4,10 ) ▁ = ▁ " , lca . key NEW_LINE DEDENT else : NEW_LINE INDENT print " Keys ▁ are ▁ not ▁ present " NEW_LINE DEDENT
Print common nodes on path from root ( or common ancestors ) | Utility class to create a new tree Node ; Utility function to find the LCA of two given values n1 and n2 . ; Base case ; If either n1 or n2 matches with root 's key,   report the presence by returning root (Note   that if a key is ancestor of other, then the   ancestor key becomes LCA  ; Look for keys in left and right subtrees ; If both of the above calls return Non - None , then one key is present in once subtree and other is present in other , So this node is the LCA ; Otherwise check if left subtree or right subtree is LCA ; Utility Function to print all ancestors of LCA ; base cases ; If target is present in either left or right subtree of this node , then prthis node ; Else return False ; Function to find nodes common to given two nodes ; Driver Code ; Let us create binary tree given in the above example
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def findLCA ( root , n1 , n2 ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT if ( root . key == n1 or root . key == n2 ) : NEW_LINE INDENT return root NEW_LINE DEDENT left_lca = findLCA ( root . left , n1 , n2 ) NEW_LINE right_lca = findLCA ( root . right , n1 , n2 ) NEW_LINE if ( left_lca and right_lca ) : NEW_LINE INDENT return root NEW_LINE DEDENT if ( left_lca != None ) : NEW_LINE INDENT return left_lca NEW_LINE DEDENT else : NEW_LINE INDENT return right_lca NEW_LINE DEDENT DEDENT def printAncestors ( root , target ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( root . key == target ) : NEW_LINE INDENT print ( root . key , end = " ▁ " ) NEW_LINE return True NEW_LINE DEDENT if ( printAncestors ( root . left , target ) or printAncestors ( root . right , target ) ) : NEW_LINE INDENT print ( root . key , end = " ▁ " ) NEW_LINE return True NEW_LINE DEDENT return False NEW_LINE DEDENT def findCommonNodes ( root , first , second ) : NEW_LINE INDENT LCA = findLCA ( root , first , second ) NEW_LINE if ( LCA == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT printAncestors ( root , LCA . key ) NEW_LINE 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 . left = newNode ( 6 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE root . left . left . left = newNode ( 8 ) NEW_LINE root . right . left . left = newNode ( 9 ) NEW_LINE root . right . left . right = newNode ( 10 ) NEW_LINE if ( findCommonNodes ( root , 9 , 7 ) == False ) : NEW_LINE INDENT print ( " No ▁ Common ▁ nodes " ) NEW_LINE DEDENT DEDENT
Binary Tree | Set 1 ( Introduction ) | A Python class that represents an individual node in a Binary Tree
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
Maximum difference between node and its ancestor in Binary Tree | Python3 program to find maximum difference between node and its ancestor ; Helper function that allocates a new node with the given data and None left and right poers . ; Recursive function to calculate maximum ancestor - node difference in binary tree . It updates value at ' res ' to store the result . The returned value of this function is minimum value in subtree rooted with ' t ' ; Returning Maximum value if node is not there ( one child case ) ; If leaf node then just return node 's value  ; Recursively calling left and right subtree for minimum value ; Updating res if ( node value - minimum value from subtree ) is bigger than res ; Returning minimum value got so far ; This function mainly calls maxDiffUtil ( ) ; Initialising result with minimum value ; Helper function to print inorder traversal of binary tree ; Driver Code ; Let us create Binary Tree shown in above example
_MIN = - 2147483648 NEW_LINE _MAX = 2147483648 NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def maxDiffUtil ( t , res ) : NEW_LINE INDENT if ( t == None ) : NEW_LINE INDENT return _MAX , res NEW_LINE DEDENT if ( t . left == None and t . right == None ) : NEW_LINE INDENT return t . key , res NEW_LINE DEDENT a , res = maxDiffUtil ( t . left , res ) NEW_LINE b , res = maxDiffUtil ( t . right , res ) NEW_LINE val = min ( a , b ) NEW_LINE res = max ( res , t . key - val ) NEW_LINE return min ( val , t . key ) , res NEW_LINE DEDENT def maxDiff ( root ) : NEW_LINE INDENT res = _MIN NEW_LINE x , res = maxDiffUtil ( root , res ) NEW_LINE return res NEW_LINE DEDENT def inorder ( root ) : NEW_LINE INDENT if ( root ) : NEW_LINE INDENT inorder ( root . left ) NEW_LINE prf ( " % d ▁ " , root . key ) NEW_LINE inorder ( root . right ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 8 ) NEW_LINE root . left = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 6 ) NEW_LINE root . left . right . left = newNode ( 4 ) NEW_LINE root . left . right . right = newNode ( 7 ) NEW_LINE root . right = newNode ( 10 ) NEW_LINE root . right . right = newNode ( 14 ) NEW_LINE root . right . right . left = newNode ( 13 ) NEW_LINE print ( " Maximum ▁ difference ▁ between ▁ a ▁ node ▁ and " , " its ▁ ancestor ▁ is ▁ : " , maxDiff ( root ) ) NEW_LINE DEDENT
Query for ancestor | Python program to query whether two node has ancestor - descendant relationship or not ; Utility dfs method to assign in and out time to each node ; assign In - time to node u ; call dfs over all neighbors except parent ; assign Out - time to node u ; method to preprocess all nodes for assigning time ; construct array of vector data structure for tree ; call dfs method from root ; method returns " yes " if u is a ancestor node of v ; Driver Code
cnt = 0 NEW_LINE def dfs ( g : list , u : int , parent : int , timeIn : list , timeOut : list ) : NEW_LINE INDENT global cnt NEW_LINE timeIn [ u ] = cnt NEW_LINE cnt += 1 NEW_LINE for i in range ( len ( g [ u ] ) ) : NEW_LINE INDENT v = g [ u ] [ i ] NEW_LINE if v != parent : NEW_LINE INDENT dfs ( g , v , u , timeIn , timeOut ) NEW_LINE DEDENT DEDENT timeOut [ u ] = cnt NEW_LINE cnt += 1 NEW_LINE DEDENT def preProcess ( edges : list , V : int , timeIn : list , timeOut : list ) : NEW_LINE INDENT global cnt NEW_LINE g = [ [ ] for i in range ( V ) ] NEW_LINE for i in range ( V - 1 ) : NEW_LINE INDENT u = edges [ i ] [ 0 ] NEW_LINE v = edges [ i ] [ 1 ] NEW_LINE g [ u ] . append ( v ) NEW_LINE g [ v ] . append ( u ) NEW_LINE DEDENT cnt = 0 NEW_LINE dfs ( g , 0 , - 1 , timeIn , timeOut ) NEW_LINE DEDENT def isAncestor ( u : int , v : int , timeIn : list , timeOut : list ) -> str : NEW_LINE INDENT b = timeIn [ u ] <= timeIn [ u ] and timeOut [ v ] <= timeOut [ u ] NEW_LINE return " yes " if b else " no " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT edges = [ ( 0 , 1 ) , ( 0 , 2 ) , ( 1 , 3 ) , ( 1 , 4 ) , ( 2 , 5 ) , ( 4 , 6 ) , ( 5 , 7 ) ] NEW_LINE E = len ( edges ) NEW_LINE V = E + 1 NEW_LINE timeIn = [ 0 ] * V NEW_LINE timeOut = [ 0 ] * V NEW_LINE preProcess ( edges , V , timeIn , timeOut ) NEW_LINE u = 1 NEW_LINE v = 6 NEW_LINE print ( isAncestor ( u , v , timeIn , timeOut ) ) NEW_LINE u = 1 NEW_LINE v = 7 NEW_LINE print ( isAncestor ( u , v , timeIn , timeOut ) ) NEW_LINE DEDENT
Print path from root to a given node in a binary tree | Helper Class that allocates a new node with the given data and None left and right pointers . ; Returns true if there is a path from root to the given node . It also populates ' arr ' with the given path ; if root is None there is no path ; push the node ' s ▁ value ▁ in ▁ ' arr '  ; if it is the required node return true ; else check whether the required node lies in the left subtree or right subtree of the current node ; required node does not lie either in the left or right subtree of the current node . Thus , remove current node ' s ▁ value ▁ ▁ ▁ from ▁ ' arr 'and then return false  ; function to print the path from root to the given node if the node lies in the binary tree ; vector to store the path ; if required node ' x ' is present then print the path ; ' x ' is not present in the binary tree ; Driver Code ; binary tree formation
class getNode : 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 hasPath ( root , arr , x ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return False NEW_LINE DEDENT arr . append ( root . data ) NEW_LINE if ( root . data == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( hasPath ( root . left , arr , x ) or hasPath ( root . right , arr , x ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT arr . pop ( - 1 ) NEW_LINE return False NEW_LINE DEDENT def printPath ( root , x ) : NEW_LINE INDENT arr = [ ] NEW_LINE if ( hasPath ( root , arr , x ) ) : NEW_LINE INDENT for i in range ( len ( arr ) - 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " - > " ) NEW_LINE DEDENT print ( arr [ len ( arr ) - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ Path " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = getNode ( 1 ) NEW_LINE root . left = getNode ( 2 ) NEW_LINE root . right = getNode ( 3 ) NEW_LINE root . left . left = getNode ( 4 ) NEW_LINE root . left . right = getNode ( 5 ) NEW_LINE root . right . left = getNode ( 6 ) NEW_LINE root . right . right = getNode ( 7 ) NEW_LINE x = 5 NEW_LINE printPath ( root , x ) NEW_LINE DEDENT
Print Ancestors of a given node in Binary Tree | A Binary Tree node ; If target is present in tree , then prints the ancestors and returns true , otherwise returns false ; Base case ; If target is present in either left or right subtree of this node , then print this node ; Else return False ; Construct the following binary tree 1 / \ 2 3 / \ 4 5 / 7
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 printAncestors ( root , target ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return False NEW_LINE DEDENT if root . data == target : NEW_LINE INDENT return True NEW_LINE DEDENT if ( printAncestors ( root . left , target ) or printAncestors ( root . right , target ) ) : NEW_LINE INDENT print root . data , NEW_LINE return True NEW_LINE DEDENT return False NEW_LINE 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 root . left . left . left = Node ( 7 ) NEW_LINE printAncestors ( root , 7 ) NEW_LINE
Kth ancestor of a node in binary tree | Set 2 | A Binary Tree Node ; recursive function to calculate Kth ancestor ; Base case ; print the kth ancestor ; return None to stop further backtracking ; return current node to previous call ; Driver Code ; prkth ancestor of given node ; check if parent is not None , it means there is no Kth ancestor of the node
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 kthAncestorDFS ( root , node , k ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return None NEW_LINE DEDENT if ( root . data == node or ( kthAncestorDFS ( root . left , node , k ) ) or ( kthAncestorDFS ( root . right , node , k ) ) ) : NEW_LINE INDENT if ( k [ 0 ] > 0 ) : NEW_LINE INDENT k [ 0 ] -= 1 NEW_LINE DEDENT elif ( k [ 0 ] == 0 ) : NEW_LINE INDENT print ( " Kth ▁ ancestor ▁ is : " , root . data ) NEW_LINE return None NEW_LINE DEDENT return root NEW_LINE 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 k = [ 2 ] NEW_LINE node = 5 NEW_LINE parent = kthAncestorDFS ( root , node , k ) NEW_LINE if ( parent ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT
Succinct Encoding of Binary Tree | Node structure ; This function fills lists ' struc ' and ' data ' . ' struc ' list stores structure information . ' data ' list stores tree data ; If root is None , put 0 in structure array and return ; Else place 1 in structure array , key in ' data ' array and recur for left and right children ; Constructs tree from ' struc ' and 'data   ; Remove one item from structure list ; If removed bit is 1 ; Remove an item from data list ; Create a tree node with removed data ; And recur to create left and right subtrees ; A utility function to print tree ; Let us conthe Tree shown in the above figure ; Structure iterator ; Data iterator
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def EncodeSuccint ( root , struc , data ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT struc . append ( 0 ) NEW_LINE return NEW_LINE DEDENT struc . append ( 1 ) NEW_LINE data . append ( root . key ) NEW_LINE EncodeSuccint ( root . left , struc , data ) NEW_LINE EncodeSuccint ( root . right , struc , data ) NEW_LINE DEDENT def DecodeSuccinct ( struc , data ) : NEW_LINE INDENT if ( len ( struc ) <= 0 ) : NEW_LINE INDENT return None NEW_LINE DEDENT b = struc [ 0 ] NEW_LINE struc . pop ( 0 ) NEW_LINE if b == 1 : NEW_LINE INDENT key = data [ 0 ] NEW_LINE data . pop ( 0 ) NEW_LINE root = Node ( key ) NEW_LINE root . left = DecodeSuccinct ( struc , data ) ; NEW_LINE root . right = DecodeSuccinct ( struc , data ) ; NEW_LINE return root NEW_LINE DEDENT return None NEW_LINE DEDENT def preorder ( root ) : NEW_LINE INDENT if root is not None : NEW_LINE INDENT print " key : ▁ % d " % ( root . key ) , NEW_LINE if root . left is not None : NEW_LINE INDENT print " | ▁ left ▁ child : ▁ % d " % ( root . left . key ) , NEW_LINE DEDENT if root . right is not None : NEW_LINE INDENT print " | ▁ right ▁ child ▁ % d " % ( root . right . key ) , NEW_LINE DEDENT print " " NEW_LINE preorder ( root . left ) NEW_LINE preorder ( root . right ) NEW_LINE DEDENT DEDENT root = Node ( 10 ) NEW_LINE root . left = Node ( 20 ) NEW_LINE root . right = Node ( 30 ) NEW_LINE root . left . left = Node ( 40 ) NEW_LINE root . left . right = Node ( 50 ) NEW_LINE root . right . right = Node ( 70 ) NEW_LINE print " Given ▁ Tree " NEW_LINE preorder ( root ) NEW_LINE struc = [ ] NEW_LINE data = [ ] NEW_LINE EncodeSuccint ( root , struc , data ) NEW_LINE print   " NEW_LINE Encoded Tree " NEW_LINE print " Structure ▁ List " NEW_LINE for i in struc : NEW_LINE INDENT print i , NEW_LINE DEDENT print   " NEW_LINE DataList " NEW_LINE for value in data : NEW_LINE INDENT print value , NEW_LINE DEDENT newroot = DecodeSuccinct ( struc , data ) NEW_LINE print   " NEW_LINE Preorder Traversal of decoded tree " NEW_LINE preorder ( newroot ) NEW_LINE
Binary Indexed Tree : Range Updates and Point Queries | Updates such that getElement ( ) gets an increased value when queried from l to r . ; Get the element indexed at i ; To get ith element sum of all the elements from 0 to i need to be computed ; Driver Code ; Find the element at Index 4 ; Find the element at Index 3
def update ( arr , l , r , val ) : NEW_LINE INDENT arr [ l ] += val NEW_LINE if r + 1 < len ( arr ) : NEW_LINE INDENT arr [ r + 1 ] -= val NEW_LINE DEDENT DEDENT def getElement ( arr , i ) : NEW_LINE INDENT res = 0 NEW_LINE for j in range ( i + 1 ) : NEW_LINE INDENT res += arr [ j ] NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 0 , 0 , 0 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE l = 2 NEW_LINE r = 4 NEW_LINE val = 2 NEW_LINE update ( arr , l , r , val ) NEW_LINE index = 4 NEW_LINE print ( " Element ▁ at ▁ index " , index , " is " , getElement ( arr , index ) ) NEW_LINE l = 0 NEW_LINE r = 3 NEW_LINE val = 4 NEW_LINE update ( arr , l , r , val ) NEW_LINE index = 3 NEW_LINE print ( " Element ▁ at ▁ index " , index , " is " , getElement ( arr , index ) ) NEW_LINE DEDENT
Binary Indexed Tree : Range Updates and Point Queries | Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse all ancestors and add 'val   ; Add ' val ' to current node of BI Tree ; Update index to that of parent in update View ; Constructs and returns a Binary Indexed Tree for given array of size n . ; Create and initialize BITree [ ] as 0 ; Store the actual values in BITree [ ] using update ( ) ; SERVES THE PURPOSE OF getElement ( ) Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] ; Iniialize result ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse ancestors of BITree [ index ] ; Add current element of BITree to sum ; Move index to parent node in getSum View ; Return the sum ; Updates such that getElement ( ) gets an increased value when queried from l to r . ; Increase value at ' l ' by 'val   ; Decrease value at ' r + 1' by 'val   ; Driver code ; Add 2 to all the element from [ 2 , 4 ] ; Find the element at Index 4 ; Add 2 to all the element from [ 0 , 3 ] ; Find the element at Index 3
def updateBIT ( BITree , n , index , val ) : NEW_LINE INDENT index = index + 1 NEW_LINE while ( index <= n ) : NEW_LINE INDENT BITree [ index ] += val NEW_LINE index += index & ( - index ) NEW_LINE DEDENT DEDENT def constructBITree ( arr , n ) : NEW_LINE INDENT BITree = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT updateBIT ( BITree , n , i , arr [ i ] ) NEW_LINE DEDENT return BITree NEW_LINE DEDENT def getSum ( BITree , index ) : NEW_LINE INDENT sum = 0 NEW_LINE index = index + 1 NEW_LINE while ( index > 0 ) : NEW_LINE INDENT sum += BITree [ index ] NEW_LINE index -= index & ( - index ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def update ( BITree , l , r , n , val ) : NEW_LINE INDENT updateBIT ( BITree , n , l , val ) NEW_LINE updateBIT ( BITree , n , r + 1 , - val ) NEW_LINE DEDENT arr = [ 0 , 0 , 0 , 0 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE BITree = constructBITree ( arr , n ) NEW_LINE l = 2 NEW_LINE r = 4 NEW_LINE val = 2 NEW_LINE update ( BITree , l , r , n , val ) NEW_LINE index = 4 NEW_LINE print ( " Element ▁ at ▁ index " , index , " is " , getSum ( BITree , index ) ) NEW_LINE l = 0 NEW_LINE r = 3 NEW_LINE val = 4 NEW_LINE update ( BITree , l , r , n , val ) NEW_LINE index = 3 NEW_LINE print ( " Element ▁ at ▁ index " , index , " is " , getSum ( BITree , index ) ) NEW_LINE
Print Postorder traversal from given Inorder and Preorder traversals | Python3 program to prPostorder traversal from given Inorder and Preorder traversals . ; Find index of next item in preorder traversal in inorder . ; traverse left tree ; traverse right tree ; prroot node at the end of traversal ; Driver code
def printPost ( inn , pre , inStrt , inEnd ) : NEW_LINE INDENT global preIndex , hm NEW_LINE if ( inStrt > inEnd ) : NEW_LINE INDENT return NEW_LINE DEDENT inIndex = hm [ pre [ preIndex ] ] NEW_LINE preIndex += 1 NEW_LINE printPost ( inn , pre , inStrt , inIndex - 1 ) NEW_LINE printPost ( inn , pre , inIndex + 1 , inEnd ) NEW_LINE print ( inn [ inIndex ] , end = " ▁ " ) NEW_LINE DEDENT def printPostMain ( inn , pre , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT hm [ inn [ i ] ] = i NEW_LINE DEDENT printPost ( inn , pre , 0 , n - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT hm = { } NEW_LINE preIndex = 0 NEW_LINE inn = [ 4 , 2 , 5 , 1 , 3 , 6 ] NEW_LINE pre = [ 1 , 2 , 4 , 5 , 3 , 6 ] NEW_LINE n = len ( pre ) NEW_LINE printPostMain ( inn , pre , n ) NEW_LINE DEDENT