codeparrot/xlcost-text-to-code huggingface dataset:
Recursive selection sort for singly linked list | Swapping node links | Linked List node ; function to swap nodes ' currX ' and ' currY ' in a linked list without swapping data ; make ' currY ' as new head ; adjust links ; Swap next pointers ; function to sort the linked list using recursive selection sort technique ; if there is only a single node ; ' min ' - pointer to store the node having minimum data value ; ' beforeMin ' - pointer to store node previous to ' min ' node ; traverse the list till the last node ; if true , then update ' min ' and 'beforeMin   ; if ' min ' and ' head ' are not same , swap the head node with the ' min ' node ; recursively sort the remaining list ; function to sort the given linked list ; if list is empty ; sort the list using recursive selection sort technique ; function to insert a node at the beginning of the linked list ; allocate node ; put in the data ; link the old list to the new node ; move the head to point to the new node ; function to print the linked list ; Driver code ; create linked list 10.12 .8 .4 .6 ; sort the linked list
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def swapNodes ( head_ref , currX , currY , prevY ) : NEW_LINE INDENT head_ref = currY NEW_LINE prevY . next = currX NEW_LINE temp = currY . next NEW_LINE currY . next = currX . next NEW_LINE currX . next = temp NEW_LINE return head_ref NEW_LINE DEDENT def recurSelectionSort ( head ) : NEW_LINE INDENT if ( head . next == None ) : NEW_LINE INDENT return head NEW_LINE DEDENT min = head NEW_LINE beforeMin = None NEW_LINE ptr = head NEW_LINE while ( ptr . next != None ) : NEW_LINE INDENT if ( ptr . next . data < min . data ) : NEW_LINE INDENT min = ptr . next NEW_LINE beforeMin = ptr NEW_LINE DEDENT ptr = ptr . next NEW_LINE DEDENT if ( min != head ) : NEW_LINE INDENT head = swapNodes ( head , head , min , beforeMin ) NEW_LINE DEDENT head . next = recurSelectionSort ( head . next ) NEW_LINE return head NEW_LINE DEDENT def sort ( head_ref ) : NEW_LINE INDENT if ( ( head_ref ) == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT head_ref = recurSelectionSort ( head_ref ) NEW_LINE return head_ref NEW_LINE DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( 0 ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = ( head_ref ) NEW_LINE ( head_ref ) = new_node NEW_LINE return head_ref NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT while ( head != None ) : NEW_LINE INDENT print ( head . data , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT DEDENT head = None NEW_LINE head = push ( head , 6 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 12 ) NEW_LINE head = push ( head , 10 ) NEW_LINE print ( " Linked ▁ list ▁ before ▁ sorting : " ) NEW_LINE printList ( head ) NEW_LINE head = sort ( head ) NEW_LINE print (   " Linked list after sorting : " ) NEW_LINE printList ( head ) NEW_LINE
Insert a node after the n | Linked List node ; function to get a new node ; allocate memory for the node ; put in the data ; function to insert a node after the nth node from the end ; if list is empty ; get a new node for the value 'x   ; find length of the list , i . e , the number of nodes in the list ; traverse up to the nth node from the end ; insert the ' newNode ' by making the necessary adjustment in the links ; function to print the list ; Creating list 1 -> 3 -> 4 -> 5
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getNode ( data ) : NEW_LINE INDENT newNode = Node ( 0 ) NEW_LINE newNode . data = data NEW_LINE newNode . next = None NEW_LINE return newNode NEW_LINE DEDENT def insertAfterNthNode ( head , n , x ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return NEW_LINE DEDENT newNode = getNode ( x ) NEW_LINE ptr = head NEW_LINE len = 0 NEW_LINE i = 0 NEW_LINE while ( ptr != None ) : NEW_LINE INDENT len = len + 1 NEW_LINE ptr = ptr . next NEW_LINE DEDENT ptr = head NEW_LINE i = 1 NEW_LINE while ( i <= ( len - n ) ) : NEW_LINE INDENT ptr = ptr . next NEW_LINE i = i + 1 NEW_LINE DEDENT newNode . next = ptr . next NEW_LINE ptr . next = newNode NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT while ( head != None ) : NEW_LINE INDENT print ( head . data , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT DEDENT head = getNode ( 1 ) NEW_LINE head . next = getNode ( 3 ) NEW_LINE head . next . next = getNode ( 4 ) NEW_LINE head . next . next . next = getNode ( 5 ) NEW_LINE n = 4 NEW_LINE x = 2 NEW_LINE print ( " Original ▁ Linked ▁ List : ▁ " ) NEW_LINE printList ( head ) NEW_LINE insertAfterNthNode ( head , n , x ) NEW_LINE print ( ) NEW_LINE print ( " Linked ▁ List ▁ After ▁ Insertion : ▁ " ) NEW_LINE printList ( head ) NEW_LINE
Insert a node after the n | Structure of a node ; Function to get a new node ; Allocate memory for the node ; Function to insert a node after the nth node from the end ; If list is empty ; Get a new node for the value 'x   ; Initializing the slow and fast pointers ; Move ' fast _ ptr ' to point to the nth node from the beginning ; Iterate until ' fast _ ptr ' points to the last node ; Move both the pointers to the respective next nodes ; Insert the ' newNode ' by making the necessary adjustment in the links ; Function to print the list ; Driver code ; Creating list 1.3 . 4.5
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getNode ( data ) : NEW_LINE INDENT newNode = Node ( data ) NEW_LINE return newNode NEW_LINE DEDENT def insertAfterNthNode ( head , n , x ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return NEW_LINE DEDENT newNode = getNode ( x ) NEW_LINE slow_ptr = head NEW_LINE fast_ptr = head NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT fast_ptr = fast_ptr . next NEW_LINE DEDENT while ( fast_ptr . next != None ) : NEW_LINE INDENT slow_ptr = slow_ptr . next NEW_LINE fast_ptr = fast_ptr . next NEW_LINE DEDENT newNode . next = slow_ptr . next NEW_LINE slow_ptr . next = newNode NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT while ( head != None ) : NEW_LINE INDENT print ( head . data , end = ' ▁ ' ) NEW_LINE head = head . next NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = getNode ( 1 ) NEW_LINE head . next = getNode ( 3 ) NEW_LINE head . next . next = getNode ( 4 ) NEW_LINE head . next . next . next = getNode ( 5 ) NEW_LINE n = 4 NEW_LINE x = 2 NEW_LINE print ( " Original ▁ Linked ▁ List : ▁ " , end = ' ' ) NEW_LINE printList ( head ) NEW_LINE insertAfterNthNode ( head , n , x ) NEW_LINE print ( " Linked List After Insertion :   " , end = ' ' ) NEW_LINE printList ( head ) NEW_LINE DEDENT
Make middle node head in a linked list | Linked List node ; function to get the middle node set it as the beginning of the linked list ; to traverse nodes one by one ; to traverse nodes by skipping one ; to keep track of previous middle ; for previous node of middle node ; move one node each time ; move two nodes each time ; set middle node at head ; To insert a node at the beginning of linked list . ; allocate new node ; link the old list to new node ; move the head to point the new node ; A function to print a given linked list ; Create a list of 5 nodes
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def setMiddleHead ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT one_node = head NEW_LINE two_node = head NEW_LINE prev = None NEW_LINE while ( two_node != None and two_node . next != None ) : NEW_LINE INDENT prev = one_node NEW_LINE one_node = one_node . next NEW_LINE two_node = two_node . next . next NEW_LINE DEDENT prev . next = prev . next . next NEW_LINE one_node . next = head NEW_LINE head = one_node NEW_LINE return head NEW_LINE DEDENT def push ( head , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . next = head NEW_LINE head = new_node NEW_LINE return head NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT temp = head NEW_LINE while ( temp != None ) : NEW_LINE INDENT print ( str ( temp . data ) , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT head = None NEW_LINE for i in range ( 5 , 0 , - 1 ) : NEW_LINE INDENT head = push ( head , i ) NEW_LINE DEDENT print ( " ▁ list ▁ before : ▁ " , end = " " ) NEW_LINE printList ( head ) NEW_LINE head = setMiddleHead ( head ) NEW_LINE print ( " ▁ list ▁ After : ▁ " , end = " " ) NEW_LINE printList ( head ) NEW_LINE
Reverse a Doubly Linked List | Set 4 ( Swapping Data ) | Python3 Program to Reverse a List using Data Swapping ; Insert a new node at the head of the list ; Function to reverse the list ; Traverse the list and set right pointer to end of list ; Swap data of left and right pointer and move them towards each other until they meet or cross each other ; Swap data of left and right pointer ; Advance left pointer ; Advance right pointer ; Driver code
import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def newNode ( val ) : NEW_LINE INDENT temp = Node ( val ) NEW_LINE temp . data = val NEW_LINE temp . prev = None NEW_LINE temp . next = None NEW_LINE return temp NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT while ( head . next != None ) : NEW_LINE INDENT print ( head . data , end = " < - - > " ) NEW_LINE head = head . next NEW_LINE DEDENT print ( head . data ) NEW_LINE DEDENT def insert ( head , val ) : NEW_LINE INDENT temp = newNode ( val ) NEW_LINE temp . next = head NEW_LINE ( head ) . prev = temp NEW_LINE ( head ) = temp NEW_LINE return head NEW_LINE DEDENT def reverseList ( head ) : NEW_LINE INDENT left = head NEW_LINE right = head NEW_LINE while ( right . next != None ) : NEW_LINE INDENT right = right . next NEW_LINE DEDENT while ( left != right and left . prev != right ) : NEW_LINE INDENT t = left . data NEW_LINE left . data = right . data NEW_LINE right . data = t NEW_LINE left = left . next NEW_LINE right = right . prev NEW_LINE DEDENT return head NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = newNode ( 5 ) NEW_LINE head = insert ( head , 4 ) NEW_LINE head = insert ( head , 3 ) NEW_LINE head = insert ( head , 2 ) NEW_LINE head = insert ( head , 1 ) NEW_LINE printList ( head ) NEW_LINE print ( " List ▁ After ▁ Reversing " ) NEW_LINE head = reverseList ( head ) NEW_LINE printList ( head ) NEW_LINE DEDENT
Check if a doubly linked list of characters is palindrome or not | Structure of node ; Given a reference ( pointer to pointer ) to the head of a list and an int , inserts a new node on the front of the list . ; Function to check if list is palindrome or not ; Find rightmost node ; Driver program
class Node : NEW_LINE INDENT def __init__ ( self , data , next , prev ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE self . prev = prev NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( new_data , head_ref , None ) NEW_LINE if head_ref != None : NEW_LINE INDENT head_ref . prev = new_node NEW_LINE head_ref = new_node NEW_LINE DEDENT return head_ref NEW_LINE DEDENT def isPalindrome ( left ) : NEW_LINE INDENT if left == None : NEW_LINE INDENT return True NEW_LINE DEDENT right = left NEW_LINE while right . next != None : NEW_LINE INDENT right = right . next NEW_LINE DEDENT while left != right : NEW_LINE INDENT if left . data != right . data : NEW_LINE INDENT return False NEW_LINE DEDENT left = left . next NEW_LINE right = right . prev NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT head = None NEW_LINE head = push ( head , ' l ' ) NEW_LINE head = push ( head , ' e ' ) NEW_LINE head = push ( head , ' v ' ) NEW_LINE head = push ( head , ' e ' ) NEW_LINE head = push ( head , ' l ' ) NEW_LINE if isPalindrome ( head ) : NEW_LINE INDENT print ( " It ▁ is ▁ Palindrome " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Palindrome " ) NEW_LINE DEDENT DEDENT
Print nodes between two given level numbers of a binary tree | A binary tree node ; Given a binary tree , print nodes form level number ' low ' to level number 'high   ; Marker node to indicate end of level ; Initialize level number ; Enqueue the only first level node and marker node for end of level ; print Q Simple level order traversal loop ; Remove the front item from queue ; print Q Check if end of level is reached ; print a new line and increment level number ; Check if marker node was last node in queue or level nubmer is beyond the given upper limit ; Enqueue the marker for end of next level ; If this is marker , then we don 't need print it  and enqueue its children ; If level is equal to or greater than given lower level , print it ; Enqueue children of non - marker node ; Driver program to test the 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 printLevels ( root , low , high ) : NEW_LINE INDENT Q = [ ] NEW_LINE marker = Node ( 11114 ) NEW_LINE level = 1 NEW_LINE Q . append ( root ) NEW_LINE Q . append ( marker ) NEW_LINE while ( len ( Q ) > 0 ) : NEW_LINE INDENT n = Q [ 0 ] NEW_LINE Q . pop ( 0 ) NEW_LINE if n == marker : NEW_LINE INDENT print NEW_LINE level += 1 NEW_LINE if len ( Q ) == 0 or level > high : NEW_LINE INDENT break NEW_LINE DEDENT Q . append ( marker ) NEW_LINE continue NEW_LINE DEDENT if level >= low : NEW_LINE INDENT print n . key , NEW_LINE DEDENT if n . left is not None : NEW_LINE INDENT Q . append ( n . left ) NEW_LINE Q . append ( n . right ) NEW_LINE DEDENT DEDENT DEDENT root = Node ( 20 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 22 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 12 ) NEW_LINE root . left . right . left = Node ( 10 ) NEW_LINE root . left . right . right = Node ( 14 ) NEW_LINE print " Level ▁ Order ▁ Traversal ▁ between ▁ given ▁ two ▁ levels ▁ is " , NEW_LINE printLevels ( root , 2 , 3 ) NEW_LINE
Print nodes at k distance from root | A Binary tree node ; Constructed binary tree is 1 / \ 2 3 / \ / 4 5 8
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 printKDistant ( root , k ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if k == 0 : NEW_LINE INDENT print root . data , NEW_LINE DEDENT else : NEW_LINE INDENT printKDistant ( root . left , k - 1 ) NEW_LINE printKDistant ( root . right , k - 1 ) 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 root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 8 ) NEW_LINE printKDistant ( root , 2 ) NEW_LINE
Print nodes at k distance from root | Iterative | Node of binary tree Function to add a new node ; Function to prnodes of given level ; extra None is appended to keep track of all the nodes to be appended before level is incremented by 1 ; prwhen level is equal to k ; break the loop if level exceeds the given level number ; Driver Code ; create a binary tree
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 printKDistant ( root , klevel ) : NEW_LINE INDENT q = [ ] NEW_LINE level = 1 NEW_LINE flag = False NEW_LINE q . append ( root ) NEW_LINE q . append ( None ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE if ( level == klevel and temp != None ) : NEW_LINE INDENT flag = True NEW_LINE print ( temp . data , end = " ▁ " ) NEW_LINE DEDENT q . pop ( 0 ) NEW_LINE if ( temp == None ) : NEW_LINE INDENT if ( len ( q ) ) : NEW_LINE INDENT q . append ( None ) NEW_LINE DEDENT level += 1 NEW_LINE if ( level > klevel ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( temp . left ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT DEDENT DEDENT print ( ) NEW_LINE return flag NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 20 ) NEW_LINE root . left = newNode ( 10 ) NEW_LINE root . right = newNode ( 30 ) NEW_LINE root . left . left = newNode ( 5 ) NEW_LINE root . left . right = newNode ( 15 ) NEW_LINE root . left . right . left = newNode ( 12 ) NEW_LINE root . right . left = newNode ( 25 ) NEW_LINE root . right . right = newNode ( 40 ) NEW_LINE print ( " data ▁ at ▁ level ▁ 1 ▁ : ▁ " , end = " " ) NEW_LINE ret = printKDistant ( root , 1 ) NEW_LINE if ( ret == False ) : NEW_LINE INDENT print ( " Number ▁ exceeds ▁ total " , " number ▁ of ▁ levels " ) NEW_LINE DEDENT print ( " data ▁ at ▁ level ▁ 2 ▁ : ▁ " , end = " " ) NEW_LINE ret = printKDistant ( root , 2 ) NEW_LINE if ( ret == False ) : NEW_LINE INDENT print ( " Number ▁ exceeds ▁ total " , " number ▁ of ▁ levels " ) NEW_LINE DEDENT print ( " data ▁ at ▁ level ▁ 3 ▁ : ▁ " , end = " " ) NEW_LINE ret = printKDistant ( root , 3 ) NEW_LINE if ( ret == False ) : NEW_LINE INDENT print ( " Number ▁ exceeds ▁ total " , " number ▁ of ▁ levels " ) NEW_LINE DEDENT print ( " data ▁ at ▁ level ▁ 6 ▁ : ▁ " , end = " " ) NEW_LINE ret = printKDistant ( root , 6 ) NEW_LINE if ( ret == False ) : NEW_LINE INDENT print ( " Number ▁ exceeds ▁ total ▁ number ▁ of ▁ levels " ) NEW_LINE DEDENT DEDENT
Print all leaf nodes of a Binary Tree from left to right | Binary tree node ; Function to print leaf nodes from left to right ; If node is null , return ; If node is leaf node , print its data ; If left child exists , check for leaf recursively ; If right child exists , check for leaf recursively ; Driver Code ; Let us create binary tree shown in above diagram ; print leaf nodes of the given 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 printLeafNodes ( root : Node ) -> None : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( not root . left and not root . right ) : NEW_LINE INDENT print ( root . data , end = " ▁ " ) NEW_LINE return NEW_LINE DEDENT if root . left : NEW_LINE INDENT printLeafNodes ( root . left ) NEW_LINE DEDENT if root . right : NEW_LINE INDENT printLeafNodes ( root . right ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT 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 . right . left = Node ( 5 ) NEW_LINE root . right . right = Node ( 8 ) NEW_LINE root . right . left . left = Node ( 6 ) NEW_LINE root . right . left . right = Node ( 7 ) NEW_LINE root . right . right . left = Node ( 9 ) NEW_LINE root . right . right . right = Node ( 10 ) NEW_LINE printLeafNodes ( root ) NEW_LINE DEDENT
Print all nodes at distance k from a given node | A binary tree node ; Recursive function to print all the nodes at distance k int the tree ( or subtree ) rooted with given root . See ; Base Case ; If we reach a k distant node , print it ; Recur for left and right subtee ; Prints all nodes at distance k from a given target node The k distant nodes may be upward or downward . This function returns distance of root from target node , it returns - 1 if target node is not present in tree rooted with root ; Base Case 1 : IF tree is empty return - 1 ; If target is same as root . Use the downward function to print all nodes at distance k in subtree rooted with target or root ; Recur for left subtree ; Check if target node was found in left subtree ; If root is at distance k from target , print root Note : dl is distance of root 's left child  from target ; Else go to right subtreee and print all k - dl - 2 distant nodes Note : that the right child is 2 edges away from left chlid ; Add 1 to the distance and return value for for parent calls ; MIRROR OF ABOVE CODE FOR RIGHT SUBTREE Note that we reach here only when node was not found in left subtree ; If target was neither present in left nor in right subtree ; Let us construct the tree shown in above diagram
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 printkDistanceNodeDown ( root , k ) : NEW_LINE INDENT if root is None or k < 0 : NEW_LINE INDENT return NEW_LINE DEDENT if k == 0 : NEW_LINE INDENT print root . data NEW_LINE return NEW_LINE DEDENT printkDistanceNodeDown ( root . left , k - 1 ) NEW_LINE printkDistanceNodeDown ( root . right , k - 1 ) NEW_LINE DEDENT def printkDistanceNode ( root , target , k ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if root == target : NEW_LINE INDENT printkDistanceNodeDown ( root , k ) NEW_LINE return 0 NEW_LINE DEDENT dl = printkDistanceNode ( root . left , target , k ) NEW_LINE if dl != - 1 : NEW_LINE INDENT if dl + 1 == k : NEW_LINE INDENT print root . data NEW_LINE DEDENT else : NEW_LINE INDENT printkDistanceNodeDown ( root . right , k - dl - 2 ) NEW_LINE DEDENT return 1 + dl NEW_LINE DEDENT dr = printkDistanceNode ( root . right , target , k ) NEW_LINE if dr != - 1 : NEW_LINE INDENT if ( dr + 1 == k ) : NEW_LINE INDENT print root . data NEW_LINE DEDENT else : NEW_LINE INDENT printkDistanceNodeDown ( root . left , k - dr - 2 ) NEW_LINE DEDENT return 1 + dr NEW_LINE DEDENT return - 1 NEW_LINE DEDENT root = Node ( 20 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 22 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 12 ) NEW_LINE root . left . right . left = Node ( 10 ) NEW_LINE root . left . right . right = Node ( 14 ) NEW_LINE target = root . left . right NEW_LINE printkDistanceNode ( root , target , 2 ) NEW_LINE
Print all nodes that don 't have sibling | A Binary Tree Node ; Function to print all non - root nodes that don 't have a sibling ; Base Case ; If this is an internal node , recur for left and right subtrees ; If left child is NULL , and right is not , print right child and recur for right child ; If right child is NULL and left is not , print left child and recur for left child ; Let us create binary tree given in the above example
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 printSingles ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if root . left is not None and root . right is not None : NEW_LINE INDENT printSingles ( root . left ) NEW_LINE printSingles ( root . right ) NEW_LINE DEDENT elif root . right is not None : NEW_LINE INDENT print root . right . key , NEW_LINE printSingles ( root . right ) NEW_LINE DEDENT elif root . left is not None : NEW_LINE INDENT print root . left . key , NEW_LINE printSingles ( root . left ) NEW_LINE DEDENT DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . right = Node ( 4 ) NEW_LINE root . right . left = Node ( 5 ) NEW_LINE root . right . left . left = Node ( 6 ) NEW_LINE printSingles ( root ) NEW_LINE