codeparrot/xlcost-text-to-code huggingface dataset:
Binary Tree ( Array implementation ) | Python3 implementation of tree using array numbering starting from 0 to n - 1. ; create root ; create left son of root ; create right son of root ; Print tree ; Driver Code
tree = [ None ] * 10 NEW_LINE def root ( key ) : NEW_LINE INDENT if tree [ 0 ] != None : NEW_LINE INDENT print ( " Tree ▁ already ▁ had ▁ root " ) NEW_LINE DEDENT else : NEW_LINE INDENT tree [ 0 ] = key NEW_LINE DEDENT DEDENT def set_left ( key , parent ) : NEW_LINE INDENT if tree [ parent ] == None : NEW_LINE INDENT print ( " Can ' t ▁ set ▁ child ▁ at " , ( parent * 2 ) + 1 , " , ▁ no ▁ parent ▁ found " ) NEW_LINE DEDENT else : NEW_LINE INDENT tree [ ( parent * 2 ) + 1 ] = key NEW_LINE DEDENT DEDENT def set_right ( key , parent ) : NEW_LINE INDENT if tree [ parent ] == None : NEW_LINE INDENT print ( " Can ' t ▁ set ▁ child ▁ at " , ( parent * 2 ) + 2 , " , ▁ no ▁ parent ▁ found " ) NEW_LINE DEDENT else : NEW_LINE INDENT tree [ ( parent * 2 ) + 2 ] = key NEW_LINE DEDENT DEDENT def print_tree ( ) : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT if tree [ i ] != None : NEW_LINE INDENT print ( tree [ i ] , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - " , end = " " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT root ( ' A ' ) NEW_LINE set_right ( ' C ' , 0 ) NEW_LINE set_left ( ' D ' , 1 ) NEW_LINE set_right ( ' E ' , 1 ) NEW_LINE set_right ( ' F ' , 2 ) NEW_LINE print_tree ( ) NEW_LINE
Swap Kth node from beginning with Kth node from end in a Linked List | A Linked List node ; Utility function to insert a node at the beginning @ args : data : value of node ; Print linked list ; count number of node in linked list ; Function for swapping kth nodes from both ends of linked list ; Count nodes in linked list ; check if k is valid ; If x ( kth node from start ) and y ( kth node from end ) are same ; Find the kth node from beginning of linked list . We also find previous of kth node because we need to update next pointer of the previous . ; Similarly , find the kth node from end and its previous . kth node from end is ( n - k + 1 ) th node from beginning ; If x_prev exists , then new next of it will be y . Consider the case when y -> next is x , in this case , x_prev and y are same . So the statement " x _ prev - > next ▁ = ▁ y " creates a self loop . This self loop will be broken when we change y -> next . ; Same thing applies to y_prev ; Swap next pointers of x and y . These statements also break self loop if x -> next is y or y -> next is x ; Change head pointers when k is 1 or n ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self , data , next = None ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self , * args , ** kwargs ) : NEW_LINE INDENT self . head = Node ( None ) NEW_LINE DEDENT def push ( self , data ) : NEW_LINE INDENT node = Node ( data ) NEW_LINE node . next = self . head NEW_LINE self . head = node NEW_LINE DEDENT def printList ( self ) : NEW_LINE INDENT node = self . head NEW_LINE while node . next is not None : NEW_LINE INDENT print ( node . data , end = " ▁ " ) NEW_LINE node = node . next NEW_LINE DEDENT DEDENT def countNodes ( self ) : NEW_LINE INDENT count = 0 NEW_LINE node = self . head NEW_LINE while node . next is not None : NEW_LINE INDENT count += 1 NEW_LINE node = node . next NEW_LINE DEDENT return count NEW_LINE DEDENT def swapKth ( self , k ) : NEW_LINE INDENT n = self . countNodes ( ) NEW_LINE if n < k : NEW_LINE INDENT return NEW_LINE DEDENT if ( 2 * k - 1 ) == n : NEW_LINE INDENT return NEW_LINE DEDENT x = self . head NEW_LINE x_prev = Node ( None ) NEW_LINE for i in range ( k - 1 ) : NEW_LINE INDENT x_prev = x NEW_LINE x = x . next NEW_LINE DEDENT y = self . head NEW_LINE y_prev = Node ( None ) NEW_LINE for i in range ( n - k ) : NEW_LINE INDENT y_prev = y NEW_LINE y = y . next NEW_LINE DEDENT if x_prev is not None : NEW_LINE INDENT x_prev . next = y NEW_LINE DEDENT if y_prev is not None : NEW_LINE INDENT y_prev . next = x NEW_LINE DEDENT temp = x . next NEW_LINE x . next = y . next NEW_LINE y . next = temp NEW_LINE if k == 1 : NEW_LINE INDENT self . head = y NEW_LINE DEDENT if k == n : NEW_LINE INDENT self . head = x NEW_LINE DEDENT DEDENT DEDENT llist = LinkedList ( ) NEW_LINE for i in range ( 8 , 0 , - 1 ) : NEW_LINE INDENT llist . push ( i ) NEW_LINE DEDENT llist . printList ( ) NEW_LINE for i in range ( 1 , 9 ) : NEW_LINE INDENT llist . swapKth ( i ) NEW_LINE print ( " Modified ▁ List ▁ for ▁ k ▁ = ▁ " , i ) NEW_LINE llist . printList ( ) NEW_LINE print ( " " ) NEW_LINE DEDENT
Find pairs with given sum in doubly linked list | Structure of node of doubly linked list ; Function to find pair whose sum equal to given value x . ; Set two pointers , first to the beginning of DLL and second to the end of DLL . ; To track if we find a pair or not ; The loop terminates when they cross each other ( second . next == first ) , or they become same ( first == second ) ; Pair found ; Move first in forward direction ; Move second in backward direction ; If pair is not present ; A utility function to insert a new node at the beginning of doubly linked list ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . next = None NEW_LINE self . prev = None NEW_LINE DEDENT DEDENT def pairSum ( head , x ) : NEW_LINE INDENT first = head NEW_LINE second = head NEW_LINE while ( second . next != None ) : NEW_LINE INDENT second = second . next NEW_LINE DEDENT found = False NEW_LINE while ( first != second and second . next != first ) : NEW_LINE INDENT if ( ( first . data + second . data ) == x ) : NEW_LINE INDENT found = True NEW_LINE print ( " ( " , first . data , " , " , second . data , " ) " ) NEW_LINE first = first . next NEW_LINE second = second . prev NEW_LINE DEDENT else : NEW_LINE INDENT if ( ( first . data + second . data ) < x ) : NEW_LINE INDENT first = first . next NEW_LINE DEDENT else : NEW_LINE INDENT second = second . prev NEW_LINE DEDENT DEDENT DEDENT if ( found == False ) : NEW_LINE INDENT print ( " No ▁ pair ▁ found " ) NEW_LINE DEDENT DEDENT def insert ( head , data ) : NEW_LINE INDENT temp = Node ( data ) NEW_LINE if not head : NEW_LINE INDENT head = temp NEW_LINE DEDENT else : NEW_LINE INDENT temp . next = head NEW_LINE head . prev = temp NEW_LINE head = temp NEW_LINE DEDENT return head NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = insert ( head , 9 ) NEW_LINE head = insert ( head , 8 ) NEW_LINE head = insert ( head , 6 ) NEW_LINE head = insert ( head , 5 ) NEW_LINE head = insert ( head , 4 ) NEW_LINE head = insert ( head , 2 ) NEW_LINE head = insert ( head , 1 ) NEW_LINE x = 7 NEW_LINE pairSum ( head , x ) NEW_LINE DEDENT
Iterative diagonal traversal of binary tree | A binary tree node has data , pointer to left child and a pointer to right child ; Function to print diagonal view ; base case ; queue of treenode ; Append root ; Append delimiter ; If current is delimiter then insert another for next diagonal and cout nextline ; If queue is empty then return ; Print output on nextline ; append delimiter again ; If left child is present append into queue ; current equals to right child ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . val = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def diagonalprint ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE q . append ( None ) NEW_LINE while len ( q ) > 0 : NEW_LINE INDENT temp = q . pop ( 0 ) NEW_LINE if not temp : NEW_LINE INDENT if len ( q ) == 0 : NEW_LINE INDENT return NEW_LINE DEDENT print ( ' ▁ ' ) NEW_LINE q . append ( None ) NEW_LINE DEDENT else : NEW_LINE INDENT while temp : NEW_LINE INDENT print ( temp . val , end = ' ▁ ' ) NEW_LINE if temp . left : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT temp = temp . right NEW_LINE DEDENT DEDENT DEDENT DEDENT root = Node ( 8 ) NEW_LINE root . left = Node ( 3 ) NEW_LINE root . right = Node ( 10 ) NEW_LINE root . left . left = Node ( 1 ) NEW_LINE root . left . right = Node ( 6 ) NEW_LINE root . right . right = Node ( 14 ) NEW_LINE root . right . right . left = Node ( 13 ) NEW_LINE root . left . right . left = Node ( 4 ) NEW_LINE root . left . right . right = Node ( 7 ) NEW_LINE diagonalprint ( root ) NEW_LINE
Count triplets in a sorted doubly linked list whose sum is equal to a given value x | Structure of node of doubly linked list ; Function to count pairs whose sum equal to given 'value   ; The loop terminates when either of two pointers become None , or they cross each other ( second . next == first ) , or they become same ( first == second ) ; Pair found ; Increment count ; Move first in forward direction ; Move second in backward direction ; If sum is greater than ' value ' move second in backward direction ; Else move first in forward direction ; Required count of pairs ; Function to count triplets in a sorted doubly linked list whose sum is equal to a given value 'x   ; If list is empty ; Get pointer to the last node of the doubly linked list ; Traversing the doubly linked list ; For each current node ; count pairs with sum ( x - current . data ) in the range first to last and add it to the ' count ' of triplets ; Required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; Allocate node ; Put in the data temp . next = temp . prev = None ; Driver code ; Start with an empty doubly linked list ; Insert values in sorted order
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . next = None NEW_LINE self . prev = None NEW_LINE DEDENT DEDENT def countPairs ( first , second , value ) : NEW_LINE INDENT count = 0 NEW_LINE while ( first != None and second != None and first != second and second . next != first ) : NEW_LINE INDENT if ( ( first . data + second . data ) == value ) : NEW_LINE INDENT count += 1 NEW_LINE first = first . next NEW_LINE second = second . prev NEW_LINE DEDENT elif ( ( first . data + second . data ) > value ) : NEW_LINE INDENT second = second . prev NEW_LINE DEDENT else : NEW_LINE INDENT first = first . next NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def countTriplets ( head , x ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT current , first , last = head , None , None NEW_LINE count = 0 NEW_LINE last = head NEW_LINE while ( last . next != None ) : NEW_LINE INDENT last = last . next NEW_LINE DEDENT while current != None : NEW_LINE INDENT first = current . next NEW_LINE count , current = count + countPairs ( first , last , x - current . data ) , current . next NEW_LINE DEDENT return count NEW_LINE DEDENT def insert ( head , data ) : NEW_LINE INDENT temp = Node ( data ) NEW_LINE if ( head == None ) : NEW_LINE INDENT head = temp NEW_LINE DEDENT else : NEW_LINE INDENT temp . next = head NEW_LINE head . prev = temp NEW_LINE head = temp NEW_LINE DEDENT return head NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = insert ( head , 9 ) NEW_LINE head = insert ( head , 8 ) NEW_LINE head = insert ( head , 6 ) NEW_LINE head = insert ( head , 5 ) NEW_LINE head = insert ( head , 4 ) NEW_LINE head = insert ( head , 2 ) NEW_LINE head = insert ( head , 1 ) NEW_LINE x = 17 NEW_LINE print ( " Count ▁ = ▁ " , countTriplets ( head , x ) ) NEW_LINE DEDENT
Sorted insert in a doubly linked list with head and tail pointers | Linked List node ; Function to insetail new node ; If first node to be insetailed in doubly linked list ; If node to be insetailed has value less than first node ; If node to be insetailed has value more than last node ; Find the node before which we need to insert p . ; Insert new node before temp ; Function to print nodes in from left to right ; Driver program to test above functions
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . info = data NEW_LINE self . next = None NEW_LINE self . prev = None NEW_LINE DEDENT DEDENT head = None NEW_LINE tail = None NEW_LINE def nodeInsetail ( key ) : NEW_LINE INDENT global head NEW_LINE global tail NEW_LINE p = Node ( 0 ) NEW_LINE p . info = key NEW_LINE p . next = None NEW_LINE if ( ( head ) == None ) : NEW_LINE INDENT ( head ) = p NEW_LINE ( tail ) = p NEW_LINE ( head ) . prev = None NEW_LINE return NEW_LINE DEDENT if ( ( p . info ) < ( ( head ) . info ) ) : NEW_LINE INDENT p . prev = None NEW_LINE ( head ) . prev = p NEW_LINE p . next = ( head ) NEW_LINE ( head ) = p NEW_LINE return NEW_LINE DEDENT if ( ( p . info ) > ( ( tail ) . info ) ) : NEW_LINE INDENT p . prev = ( tail ) NEW_LINE ( tail ) . next = p NEW_LINE ( tail ) = p NEW_LINE return NEW_LINE DEDENT temp = ( head ) . next NEW_LINE while ( ( temp . info ) < ( p . info ) ) : NEW_LINE INDENT temp = temp . next NEW_LINE DEDENT ( temp . prev ) . next = p NEW_LINE p . prev = temp . prev NEW_LINE temp . prev = p NEW_LINE p . next = temp NEW_LINE DEDENT def printList ( temp ) : NEW_LINE INDENT while ( temp != None ) : NEW_LINE INDENT print ( temp . info , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT DEDENT nodeInsetail ( 30 ) NEW_LINE nodeInsetail ( 50 ) NEW_LINE nodeInsetail ( 90 ) NEW_LINE nodeInsetail ( 10 ) NEW_LINE nodeInsetail ( 40 ) NEW_LINE nodeInsetail ( 110 ) NEW_LINE nodeInsetail ( 60 ) NEW_LINE nodeInsetail ( 95 ) NEW_LINE nodeInsetail ( 23 ) NEW_LINE print ( " Doubly linked list on printing from left to right " ) NEW_LINE printList ( head ) NEW_LINE
Boundary Traversal of binary tree | A binary tree node ; A simple function to print leaf nodes of a Binary Tree ; Print it if it is a leaf node ; A function to print all left boundary nodes , except a leaf node . Print the nodes in TOP DOWN manner ; to ensure top down order , print the node before calling itself for left subtree ; A function to print all right boundary nodes , except a leaf node . Print the nodes in BOTTOM UP manner ; to ensure bottom up order , first call for right subtree , then print this node ; A function to do boundary traversal of a given binary tree ; Print the left boundary in top - down manner ; Print all leaf nodes ; Print the right boundary in bottom - up manner ; 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 printLeaves ( root ) : NEW_LINE INDENT if ( root ) : NEW_LINE INDENT printLeaves ( root . left ) NEW_LINE if root . left is None and root . right is None : NEW_LINE INDENT print ( root . data ) , NEW_LINE DEDENT printLeaves ( root . right ) NEW_LINE DEDENT DEDENT def printBoundaryLeft ( root ) : NEW_LINE INDENT if ( root ) : NEW_LINE INDENT if ( root . left ) : NEW_LINE INDENT print ( root . data ) NEW_LINE printBoundaryLeft ( root . left ) NEW_LINE DEDENT elif ( root . right ) : NEW_LINE INDENT print ( root . data ) NEW_LINE printBoundaryLeft ( root . right ) NEW_LINE DEDENT DEDENT DEDENT def printBoundaryRight ( root ) : NEW_LINE INDENT if ( root ) : NEW_LINE INDENT if ( root . right ) : NEW_LINE INDENT printBoundaryRight ( root . right ) NEW_LINE print ( root . data ) NEW_LINE DEDENT elif ( root . left ) : NEW_LINE INDENT printBoundaryRight ( root . left ) NEW_LINE print ( root . data ) NEW_LINE DEDENT DEDENT DEDENT def printBoundary ( root ) : NEW_LINE INDENT if ( root ) : NEW_LINE INDENT print ( root . data ) NEW_LINE printBoundaryLeft ( root . left ) NEW_LINE printLeaves ( root . left ) NEW_LINE printLeaves ( root . right ) NEW_LINE printBoundaryRight ( root . right ) NEW_LINE DEDENT DEDENT root = Node ( 20 ) NEW_LINE root . left = Node ( 8 ) 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 root . right = Node ( 22 ) NEW_LINE root . right . right = Node ( 25 ) NEW_LINE printBoundary ( root ) NEW_LINE
An interesting method to print reverse of a linked list | Link list node ; Function to reverse the linked list ; For each node , print proper number of spaces before printing it ; use of carriage return to move back and print . ; Function to push a node ; Function to print linked list and find its length ; i for finding length of list ; Start with the empty list ; list nodes are as 6 5 4 3 2 1 ; printlist print the list and return the size of list ; print reverse list with help of carriage return function
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def printReverse ( head_ref , n ) : NEW_LINE INDENT j = 0 NEW_LINE current = head_ref NEW_LINE while ( current != None ) : NEW_LINE INDENT i = 0 NEW_LINE while ( i < 2 * ( n - j ) ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE i = i + 1 NEW_LINE DEDENT print ( current . data , end = " " ) NEW_LINE current = current . next NEW_LINE j = j + 1 NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( ) 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 i = 0 NEW_LINE temp = head NEW_LINE while ( temp != None ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE i = i + 1 NEW_LINE DEDENT return i NEW_LINE DEDENT head = None NEW_LINE head = push ( head , 1 ) NEW_LINE head = push ( head , 2 ) NEW_LINE head = push ( head , 3 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 5 ) NEW_LINE head = push ( head , 6 ) NEW_LINE print ( " Given ▁ linked ▁ list : " ) NEW_LINE n = printList ( head ) NEW_LINE print ( " Reversed Linked list : " ) NEW_LINE printReverse ( head , n ) NEW_LINE print ( ) NEW_LINE
Practice questions for Linked List and Recursion |
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT
Practice questions for Linked List and Recursion |
def fun1 ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return NEW_LINE DEDENT fun1 ( head . next ) NEW_LINE print ( head . data , end = " ▁ " ) NEW_LINE DEDENT
Practice questions for Linked List and Recursion |
def fun2 ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return NEW_LINE DEDENT print ( head . data , end = " ▁ " ) NEW_LINE if ( head . next != None ) : NEW_LINE INDENT fun2 ( head . next . next ) NEW_LINE DEDENT print ( head . data , end = " ▁ " ) NEW_LINE DEDENT
Practice questions for Linked List and Recursion | A linked list node ; Prints a linked list in reverse manner ; prints alternate nodes of a Linked List , first from head to end , and then from end to head . ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; put in the data ; link the old list off the new node ; move the head to poto the new node ; Start with the empty list ; Using push ( ) to construct below list 1.2 . 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 fun1 ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return NEW_LINE DEDENT fun1 ( head . next ) NEW_LINE print ( head . data , end = " ▁ " ) NEW_LINE DEDENT def fun2 ( start ) : NEW_LINE INDENT if ( start == None ) : NEW_LINE INDENT return NEW_LINE DEDENT print ( start . data , end = " ▁ " ) NEW_LINE if ( start . next != None ) : NEW_LINE INDENT fun2 ( start . next . next ) NEW_LINE DEDENT print ( start . data , end = " ▁ " ) 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 head = None NEW_LINE head = Node ( 5 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 3 ) NEW_LINE head = push ( head , 2 ) NEW_LINE head = push ( head , 1 ) NEW_LINE print ( " Output ▁ of ▁ fun1 ( ) ▁ for ▁ list ▁ 1 - > 2 - > 3 - > 4 - > 5" ) NEW_LINE fun1 ( head ) NEW_LINE print ( " Output of fun2 ( ) for list 1 -> 2 -> 3 -> 4 -> 5 " ) NEW_LINE fun2 ( head ) NEW_LINE
Density of Binary Tree in One Traversal | A binary tree node ; Function to compute height and size of a binary tree ; compute height of each subtree ; increase size by 1 ; return larger of the two ; function to calculate density of a binary tree ; To store size ; Finds height and size ; 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 heighAndSize ( node , size ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT l = heighAndSize ( node . left , size ) NEW_LINE r = heighAndSize ( node . right , size ) NEW_LINE size [ 0 ] += 1 NEW_LINE return l + 1 if ( l > r ) else r + 1 NEW_LINE DEDENT def density ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT size = [ 0 ] NEW_LINE _height = heighAndSize ( root , size ) NEW_LINE return size [ 0 ] / _height 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 print ( " Density ▁ of ▁ given ▁ binary ▁ tree ▁ is ▁ " , density ( root ) ) NEW_LINE DEDENT
Squareroot ( n ) | Python3 program to find sqrt ( n ) 'th node  of a linked list  Node class  ; Function to initialise the node object ; Function to get the sqrt ( n ) th node of a linked list ; Traverse the list ; check if j = sqrt ( i ) ; for first node ; increment j if j = sqrt ( i ) ; return node 's data  ; function to add a new node at the beginning of the list ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver Code ; Start with the empty 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 printsqrtn ( head ) : NEW_LINE INDENT sqrtn = None NEW_LINE i = 1 NEW_LINE j = 1 NEW_LINE while ( head != None ) : NEW_LINE INDENT if ( i == j * j ) : NEW_LINE INDENT if ( sqrtn == None ) : NEW_LINE INDENT sqrtn = head NEW_LINE DEDENT else : NEW_LINE INDENT sqrtn = sqrtn . next NEW_LINE DEDENT j = j + 1 NEW_LINE DEDENT i = i + 1 NEW_LINE head = head . next NEW_LINE DEDENT return sqrtn . data NEW_LINE DEDENT def print_1 ( head ) : NEW_LINE INDENT while ( head != None ) : NEW_LINE INDENT print ( head . data , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT print ( " ▁ " ) 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 if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 40 ) NEW_LINE head = push ( head , 30 ) NEW_LINE head = push ( head , 20 ) NEW_LINE head = push ( head , 10 ) NEW_LINE print ( " Given ▁ linked ▁ list ▁ is : " ) NEW_LINE print_1 ( head ) NEW_LINE print ( " sqrt ( n ) th ▁ node ▁ is ▁ " , printsqrtn ( head ) ) NEW_LINE DEDENT