codeparrot/xlcost-text-to-code huggingface dataset:
Find right sibling of a binary tree with parent pointers | A class to create a new Binary Tree Node ; Method to find right sibling ; GET Parent pointer whose right child is not a parent or itself of this node . There might be case when parent has no right child , but , current node is left child of the parent ( second condition is for that ) . ; Move to the required child , where right sibling can be present ; find right sibling in the given subtree ( from current node ) , when level will be 0 ; Iterate through subtree ; if no child are there , we cannot have right sibling in this path ; This is the case when we reach 9 node in the tree , where we need to again recursively find the right sibling ; Driver Code ; passing 10
class newNode : NEW_LINE INDENT def __init__ ( self , item , parent ) : NEW_LINE INDENT self . data = item NEW_LINE self . left = self . right = None NEW_LINE self . parent = parent NEW_LINE DEDENT DEDENT def findRightSibling ( node , level ) : NEW_LINE INDENT if ( node == None or node . parent == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT while ( node . parent . right == node or ( node . parent . right == None and node . parent . left == node ) ) : NEW_LINE INDENT if ( node . parent == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT node = node . parent NEW_LINE level -= 1 NEW_LINE DEDENT node = node . parent . right NEW_LINE while ( level < 0 ) : NEW_LINE INDENT if ( node . left != None ) : NEW_LINE INDENT node = node . left NEW_LINE DEDENT elif ( node . right != None ) : NEW_LINE INDENT node = node . right NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT level += 1 NEW_LINE DEDENT if ( level == 0 ) : NEW_LINE INDENT return node NEW_LINE DEDENT return findRightSibling ( node , level ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 , None ) NEW_LINE root . left = newNode ( 2 , root ) NEW_LINE root . right = newNode ( 3 , root ) NEW_LINE root . left . left = newNode ( 4 , root . left ) NEW_LINE root . left . right = newNode ( 6 , root . left ) NEW_LINE root . left . left . left = newNode ( 7 , root . left . left ) NEW_LINE root . left . left . left . left = newNode ( 10 , root . left . left . left ) NEW_LINE root . left . right . right = newNode ( 9 , root . left . right ) NEW_LINE root . right . right = newNode ( 5 , root . right ) NEW_LINE root . right . right . right = newNode ( 8 , root . right . right ) NEW_LINE root . right . right . right . right = newNode ( 12 , root . right . right . right ) NEW_LINE res = findRightSibling ( root . left . left . left . left , 0 ) NEW_LINE if ( res == None ) : NEW_LINE INDENT print ( " No ▁ right ▁ sibling " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( res . data ) NEW_LINE DEDENT DEDENT
Find next right node of a given key | Set 2 | class to create a new tree node ; Function to find next node for given node in same level in a binary tree by using pre - order traversal ; return None if tree is empty ; if desired node is found , set value_level to current level ; if value_level is already set , then current node is the next right node ; recurse for left subtree by increasing level by 1 ; if node is found in left subtree , return it ; recurse for right subtree by increasing level by 1 ; Function to find next node of given node in the same level in given binary tree ; A utility function to test above functions ; 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 nextRightNode ( root , k , level , value_level ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT if ( root . key == k ) : NEW_LINE INDENT value_level [ 0 ] = level NEW_LINE return None NEW_LINE DEDENT elif ( value_level [ 0 ] ) : NEW_LINE INDENT if ( level == value_level [ 0 ] ) : NEW_LINE INDENT return root NEW_LINE DEDENT DEDENT leftNode = nextRightNode ( root . left , k , level + 1 , value_level ) NEW_LINE if ( leftNode ) : NEW_LINE INDENT return leftNode NEW_LINE DEDENT return nextRightNode ( root . right , k , level + 1 , value_level ) NEW_LINE DEDENT def nextRightNodeUtil ( root , k ) : NEW_LINE INDENT value_level = [ 0 ] NEW_LINE return nextRightNode ( root , k , 1 , value_level ) NEW_LINE DEDENT def test ( root , k ) : NEW_LINE INDENT nr = nextRightNodeUtil ( root , k ) NEW_LINE if ( nr != None ) : NEW_LINE INDENT print ( " Next ▁ Right ▁ of " , k , " is " , nr . key ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ next ▁ right ▁ node ▁ found ▁ for " , k ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 6 ) NEW_LINE root . right . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 8 ) NEW_LINE root . left . right = newNode ( 4 ) NEW_LINE test ( root , 10 ) NEW_LINE test ( root , 2 ) NEW_LINE test ( root , 6 ) NEW_LINE test ( root , 5 ) NEW_LINE test ( root , 8 ) NEW_LINE test ( root , 4 ) NEW_LINE DEDENT
Top three elements in binary tree | Helper function that allocates a new Node with the given data and None left and right pointers . ; function to find three largest element ; if data is greater than first large number update the top three list ; if data is greater than second large number and not equal to first update the bottom two list ; if data is greater than third large number and not equal to first & second update the third highest list ; Driver Code
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 threelargest ( root , first , second , third ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . data > first [ 0 ] ) : NEW_LINE INDENT third [ 0 ] = second [ 0 ] NEW_LINE second [ 0 ] = first [ 0 ] NEW_LINE first [ 0 ] = root . data NEW_LINE DEDENT elif ( root . data > second [ 0 ] and root . data != first [ 0 ] ) : NEW_LINE INDENT third [ 0 ] = second [ 0 ] NEW_LINE second [ 0 ] = root . data NEW_LINE DEDENT elif ( root . data > third [ 0 ] and root . data != first [ 0 ] and root . data != second [ 0 ] ) : NEW_LINE INDENT third [ 0 ] = root . data NEW_LINE DEDENT threelargest ( root . left , first , second , third ) NEW_LINE threelargest ( root . right , first , second , third ) 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 ( 4 ) NEW_LINE root . right . right = newNode ( 5 ) NEW_LINE first = [ 0 ] NEW_LINE second = [ 0 ] NEW_LINE third = [ 0 ] NEW_LINE threelargest ( root , first , second , third ) NEW_LINE print ( " three ▁ largest ▁ elements ▁ are " , first [ 0 ] , second [ 0 ] , third [ 0 ] ) NEW_LINE DEDENT
Find maximum ( or minimum ) in Binary Tree | A class to create a new node ; Constructor that allocates a new node with the given data and NULL left and right pointers . ; Returns maximum value in a given Binary Tree ; Base case ; Return maximum of 3 values : 1 ) Root 's data 2) Max in Left Subtree 3) Max in right subtree ; Driver Code ; Function call
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 findMax ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return float ( ' - inf ' ) NEW_LINE DEDENT res = root . data NEW_LINE lres = findMax ( root . left ) NEW_LINE rres = findMax ( root . right ) NEW_LINE if ( lres > res ) : NEW_LINE INDENT res = lres NEW_LINE DEDENT if ( rres > res ) : NEW_LINE INDENT res = rres NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 2 ) NEW_LINE root . left = newNode ( 7 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . right = newNode ( 6 ) NEW_LINE root . left . right . left = newNode ( 1 ) NEW_LINE root . left . right . right = newNode ( 11 ) NEW_LINE root . right . right = newNode ( 9 ) NEW_LINE root . right . right . left = newNode ( 4 ) NEW_LINE print ( " Maximum ▁ element ▁ is " , findMax ( root ) ) NEW_LINE DEDENT
Find maximum ( or minimum ) in Binary Tree | Returns the min value in a binary tree
def find_min_in_BT ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return float ( ' inf ' ) NEW_LINE DEDENT res = root . data NEW_LINE lres = find_min_in_BT ( root . leftChild ) NEW_LINE rres = find_min_in_BT ( root . rightChild ) NEW_LINE if lres < res : NEW_LINE INDENT res = lres NEW_LINE DEDENT if rres < res : NEW_LINE INDENT res = rres NEW_LINE DEDENT return res NEW_LINE DEDENT
Extract Leaves of a Binary Tree in a Doubly Linked List | A binary tree node ; Main function which extracts all leaves from given Binary Tree . The function returns new root of Binary Tree ( Note thatroot may change if Binary Tree has only one node ) . The function also sets * head_ref as head of doubly linked list . left pointer of tree is used as prev in DLL and right pointer is used as next ; Utility function for printing tree in InOrder ; Utility function for printing double linked list . ; 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 extractLeafList ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return None NEW_LINE DEDENT if root . left is None and root . right is None : NEW_LINE INDENT root . right = extractLeafList . head NEW_LINE if extractLeafList . head is not None : NEW_LINE INDENT extractLeafList . head . left = root NEW_LINE DEDENT extractLeafList . head = root NEW_LINE return None NEW_LINE DEDENT root . right = extractLeafList ( root . right ) NEW_LINE root . left = extractLeafList ( root . left ) NEW_LINE return root NEW_LINE DEDENT def printInorder ( root ) : NEW_LINE INDENT if root is not None : NEW_LINE INDENT printInorder ( root . left ) NEW_LINE print root . data , NEW_LINE printInorder ( root . right ) NEW_LINE DEDENT DEDENT def printList ( head ) : NEW_LINE INDENT while ( head ) : NEW_LINE INDENT if head . data is not None : NEW_LINE INDENT print head . data , NEW_LINE DEDENT head = head . right NEW_LINE DEDENT DEDENT extractLeafList . head = Node ( None ) NEW_LINE 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 . right = Node ( 6 ) NEW_LINE root . left . left . left = Node ( 7 ) NEW_LINE root . left . left . right = Node ( 8 ) NEW_LINE root . right . right . left = Node ( 9 ) NEW_LINE root . right . right . right = Node ( 10 ) NEW_LINE print " Inorder ▁ traversal ▁ of ▁ given ▁ tree ▁ is : " NEW_LINE printInorder ( root ) NEW_LINE root = extractLeafList ( root ) NEW_LINE print   " NEW_LINE Extract Double Linked List is : " NEW_LINE printList ( extractLeafList . head ) NEW_LINE print   " NEW_LINE Inorder traversal of modified tree is : " NEW_LINE printInorder ( root ) NEW_LINE
Inorder Successor of a node in Binary Tree | A Binary Tree Node ; Function to create a new Node . ; function that prints the inorder successor of a target node . next will point the last tracked node , which will be the answer . ; if root is None then return ; if target node found , then enter this condition ; Driver Code ; Let 's construct the binary tree   as shown in above diagram. ; Case 1 ; case 2 ; case 3
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 newNode ( val ) : NEW_LINE INDENT temp = Node ( 0 ) NEW_LINE temp . data = val NEW_LINE temp . left = None NEW_LINE temp . right = None NEW_LINE return temp NEW_LINE DEDENT def inorderSuccessor ( root , target_node ) : NEW_LINE INDENT global next NEW_LINE if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT inorderSuccessor ( root . right , target_node ) NEW_LINE if ( root . data == target_node . data ) : NEW_LINE INDENT if ( next == None ) : NEW_LINE INDENT print ( " inorder ▁ successor ▁ of " , root . data , " ▁ is : ▁ None " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " inorder ▁ successor ▁ of " , root . data , " is : " , next . data ) NEW_LINE DEDENT DEDENT next = root NEW_LINE inorderSuccessor ( root . left , target_node ) NEW_LINE DEDENT next = None NEW_LINE 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 next = None NEW_LINE inorderSuccessor ( root , root . right ) NEW_LINE next = None NEW_LINE inorderSuccessor ( root , root . left . left ) NEW_LINE next = None NEW_LINE inorderSuccessor ( root , root . right . right ) NEW_LINE DEDENT
Graph representations using set and hash |  ; Adds an edge to undirected graph ; Add an edge from source to destination . A new element is inserted to adjacent list of source . ; Add an dge from destination to source . A new element is inserted to adjacent list of destination . ; A utility function to print the adjacency list representation of graph ; Search for a given edge in graph ; Driver code ; Create the graph given in the above figure ; Print adjacenecy list representation of graph ; Search the given edge in a graph
class graph ( object ) : NEW_LINE INDENT def __init__ ( self , v ) : NEW_LINE INDENT self . v = v NEW_LINE self . graph = dict ( ) NEW_LINE DEDENT def addEdge ( self , source , destination ) : NEW_LINE INDENT if source not in self . graph : NEW_LINE INDENT self . graph = { destination } NEW_LINE DEDENT else : NEW_LINE INDENT self . graph . add ( destination ) NEW_LINE DEDENT if destination not in self . graph : NEW_LINE INDENT self . graph [ destination ] = { source } NEW_LINE DEDENT else : NEW_LINE INDENT self . graph [ destination ] . add ( source ) NEW_LINE DEDENT DEDENT def print ( self ) : NEW_LINE INDENT for i , adjlist in sorted ( self . graph . items ( ) ) : NEW_LINE INDENT print ( " Adjacency ▁ list ▁ of ▁ vertex ▁ " , i ) NEW_LINE for j in sorted ( adjlist , reverse = True ) : NEW_LINE INDENT print ( j , end = " ▁ " ) NEW_LINE DEDENT print ( ' ' ) NEW_LINE DEDENT DEDENT def searchEdge ( self , source , destination ) : NEW_LINE INDENT if source in self . graph : NEW_LINE INDENT if destination in self . graph : NEW_LINE INDENT print ( " Edge from { 0 } to { 1 } found . " . format ( source , destination ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Edge from { 0 } to { 1 } not found . " . format ( source , destination ) ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " Source vertex { } is not present in graph . " . format ( source ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT g = graph ( 5 ) NEW_LINE g . addEdge ( 0 , 1 ) NEW_LINE g . addEdge ( 0 , 4 ) NEW_LINE g . addEdge ( 1 , 2 ) NEW_LINE g . addEdge ( 1 , 3 ) NEW_LINE g . addEdge ( 1 , 4 ) NEW_LINE g . addEdge ( 2 , 3 ) NEW_LINE g . addEdge ( 3 , 4 ) NEW_LINE g . print ( ) NEW_LINE g . searchEdge ( 2 , 1 ) NEW_LINE g . searchEdge ( 0 , 3 ) NEW_LINE DEDENT
Find n | Python3 program for nth node of inorder traversal ; A Binary Tree Node ; Given a binary tree , print the nth node of inorder traversal ; first recur on left child ; when count = n then prelement ; now recur on right child ; Driver Code
count = [ 0 ] NEW_LINE 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 self . visited = False NEW_LINE DEDENT DEDENT def NthInorder ( node , n ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( count [ 0 ] <= n ) : NEW_LINE INDENT NthInorder ( node . left , n ) NEW_LINE count [ 0 ] += 1 NEW_LINE if ( count [ 0 ] == n ) : NEW_LINE INDENT print ( node . data , end = " ▁ " ) NEW_LINE DEDENT NthInorder ( node . right , n ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 20 ) NEW_LINE root . right = newNode ( 30 ) NEW_LINE root . left . left = newNode ( 40 ) NEW_LINE root . left . right = newNode ( 50 ) NEW_LINE n = 4 NEW_LINE NthInorder ( root , n ) NEW_LINE DEDENT
Minimum initial vertices to traverse whole matrix with given conditions | Python3 program to find minimum initial vertices to reach whole matrix ; ( n , m ) is current source cell from which we need to do DFS . N and M are total no . of rows and columns . ; Marking the vertex as visited ; If below neighbor is valid and has value less than or equal to current cell 's value ; If right neighbor is valid and has value less than or equal to current cell 's value ; If above neighbor is valid and has value less than or equal to current cell 's value ; If left neighbor is valid and has value less than or equal to current cell 's value ; Storing the cell value and cell indices in a vector . ; Sorting the newly created array according to cell values ; Create a visited array for DFS and initialize it as false . ; Applying dfs for each vertex with highest value ; If the given vertex is not visited then include it in the set ; Driver code
MAX = 100 NEW_LINE def dfs ( n , m , visit , adj , N , M ) : NEW_LINE INDENT visit [ n ] [ m ] = 1 NEW_LINE if ( n + 1 < N and adj [ n ] [ m ] >= adj [ n + 1 ] [ m ] and not visit [ n + 1 ] [ m ] ) : NEW_LINE INDENT dfs ( n + 1 , m , visit , adj , N , M ) NEW_LINE DEDENT if ( m + 1 < M and adj [ n ] [ m ] >= adj [ n ] [ m + 1 ] and not visit [ n ] [ m + 1 ] ) : NEW_LINE INDENT dfs ( n , m + 1 , visit , adj , N , M ) NEW_LINE DEDENT if ( n - 1 >= 0 and adj [ n ] [ m ] >= adj [ n - 1 ] [ m ] and not visit [ n - 1 ] [ m ] ) : NEW_LINE INDENT dfs ( n - 1 , m , visit , adj , N , M ) NEW_LINE DEDENT if ( m - 1 >= 0 and adj [ n ] [ m ] >= adj [ n ] [ m - 1 ] and not visit [ n ] [ m - 1 ] ) : NEW_LINE INDENT dfs ( n , m - 1 , visit , adj , N , M ) NEW_LINE DEDENT DEDENT def printMinSources ( adj , N , M ) : NEW_LINE INDENT x = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT x . append ( [ adj [ i ] [ j ] , [ i , j ] ] ) NEW_LINE DEDENT DEDENT x . sort ( ) NEW_LINE visit = [ [ False for i in range ( MAX ) ] for i in range ( N ) ] NEW_LINE for i in range ( len ( x ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( not visit [ x [ i ] [ 1 ] [ 0 ] ] [ x [ i ] [ 1 ] [ 1 ] ] ) : NEW_LINE INDENT print ( ' { } ▁ { } ' . format ( x [ i ] [ 1 ] [ 0 ] , x [ i ] [ 1 ] [ 1 ] ) ) NEW_LINE dfs ( x [ i ] [ 1 ] [ 0 ] , x [ i ] [ 1 ] [ 1 ] , visit , adj , N , M ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE M = 2 NEW_LINE adj = [ [ 3 , 3 ] , [ 1 , 1 ] ] NEW_LINE printMinSources ( adj , N , M ) NEW_LINE DEDENT
Level of Each node in a Tree from source node ( using BFS ) | Python3 Program to determine level of each node and print level ; function to determine level of each node starting from x using BFS ; array to store level of each node ; create a queue ; enqueue element x ; initialize level of source node to 0 ; marked it as visited ; do until queue is empty ; get the first element of queue ; traverse neighbors of node x ; b is neighbor of node x ; if b is not marked already ; enqueue b in queue ; level of b is level of x + 1 ; mark b ; display all nodes and their levels ; Driver Code ; adjacency graph for tree ; call levels function with source as 0
import queue NEW_LINE def printLevels ( graph , V , x ) : NEW_LINE INDENT level = [ None ] * V NEW_LINE marked = [ False ] * V NEW_LINE que = queue . Queue ( ) NEW_LINE que . put ( x ) NEW_LINE level [ x ] = 0 NEW_LINE marked [ x ] = True NEW_LINE while ( not que . empty ( ) ) : NEW_LINE INDENT x = que . get ( ) NEW_LINE for i in range ( len ( graph [ x ] ) ) : NEW_LINE INDENT b = graph [ x ] [ i ] NEW_LINE if ( not marked [ b ] ) : NEW_LINE INDENT que . put ( b ) NEW_LINE level [ b ] = level [ x ] + 1 NEW_LINE marked [ b ] = True NEW_LINE DEDENT DEDENT DEDENT print ( " Nodes " , " ▁ " , " Level " ) NEW_LINE for i in range ( V ) : NEW_LINE INDENT print ( " ▁ " , i , " ▁ - - > ▁ " , level [ i ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V = 8 NEW_LINE graph = [ [ ] for i in range ( V ) ] NEW_LINE graph [ 0 ] . append ( 1 ) NEW_LINE graph [ 0 ] . append ( 2 ) NEW_LINE graph [ 1 ] . append ( 3 ) NEW_LINE graph [ 1 ] . append ( 4 ) NEW_LINE graph [ 1 ] . append ( 5 ) NEW_LINE graph [ 2 ] . append ( 5 ) NEW_LINE graph [ 2 ] . append ( 6 ) NEW_LINE graph [ 6 ] . append ( 7 ) NEW_LINE printLevels ( graph , V , 0 ) NEW_LINE DEDENT
Construct binary palindrome by repeated appending and trimming | function to apply DFS ; set the parent marked ; if the node has not been visited set it and its children marked ; link which digits must be equal ; connect the two indices ; set everything connected to first character as 1 ; Driver Code
def dfs ( parent , ans , connectchars ) : NEW_LINE INDENT ans [ parent ] = 1 NEW_LINE for i in range ( len ( connectchars [ parent ] ) ) : NEW_LINE INDENT if ( not ans [ connectchars [ parent ] [ i ] ] ) : NEW_LINE INDENT dfs ( connectchars [ parent ] [ i ] , ans , connectchars ) NEW_LINE DEDENT DEDENT DEDENT def printBinaryPalindrome ( n , k ) : NEW_LINE INDENT arr = [ 0 ] * n NEW_LINE ans = [ 0 ] * n NEW_LINE connectchars = [ [ ] for i in range ( k ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = i % k NEW_LINE DEDENT for i in range ( int ( n / 2 ) ) : NEW_LINE INDENT connectchars [ arr [ i ] ] . append ( arr [ n - i - 1 ] ) NEW_LINE connectchars [ arr [ n - i - 1 ] ] . append ( arr [ i ] ) NEW_LINE DEDENT dfs ( 0 , ans , connectchars ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( ans [ arr [ i ] ] , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE k = 4 NEW_LINE printBinaryPalindrome ( n , k ) NEW_LINE DEDENT
Find n | A Binary Tree Node Utility function to create a new tree node ; function to find the N - th node in the postorder traversal of a given binary tree ; left recursion ; right recursion ; prints the n - th node of preorder traversal ; Driver Code ; prints n - th node found
class createNode : 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 flag = [ 0 ] NEW_LINE def NthPostordernode ( root , N ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( flag [ 0 ] <= N [ 0 ] ) : NEW_LINE INDENT NthPostordernode ( root . left , N ) NEW_LINE NthPostordernode ( root . right , N ) NEW_LINE flag [ 0 ] += 1 NEW_LINE if ( flag [ 0 ] == N [ 0 ] ) : NEW_LINE INDENT print ( root . data ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = createNode ( 25 ) NEW_LINE root . left = createNode ( 20 ) NEW_LINE root . right = createNode ( 30 ) NEW_LINE root . left . left = createNode ( 18 ) NEW_LINE root . left . right = createNode ( 22 ) NEW_LINE root . right . left = createNode ( 24 ) NEW_LINE root . right . right = createNode ( 32 ) NEW_LINE N = [ 6 ] NEW_LINE NthPostordernode ( root , N ) NEW_LINE DEDENT