codeparrot/xlcost-text-to-code huggingface dataset:
Find the fractional ( or n / k | Python3 program to find fractional node in a linked list ; Linked list node ; Function to create a new node with given data ; Function to find fractional node in the linked list ; Corner cases ; Traverse the given list ; For every k nodes , we move fractionalNode one step ahead . ; First time we see a multiple of k ; A utility function to print a linked list ; 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 ( data ) : NEW_LINE INDENT new_node = Node ( data ) NEW_LINE new_node . data = data NEW_LINE new_node . next = None NEW_LINE return new_node NEW_LINE DEDENT def fractionalNodes ( head , k ) : NEW_LINE INDENT if ( k <= 0 or head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT fractionalNode = None NEW_LINE i = 0 NEW_LINE temp = head NEW_LINE while ( temp != None ) : NEW_LINE INDENT if ( i % k == 0 ) : NEW_LINE INDENT if ( fractionalNode == None ) : NEW_LINE INDENT fractionalNode = head NEW_LINE DEDENT else : NEW_LINE INDENT fractionalNode = fractionalNode . next NEW_LINE DEDENT DEDENT i = i + 1 NEW_LINE temp = temp . next NEW_LINE DEDENT return fractionalNode NEW_LINE DEDENT def prList ( node ) : NEW_LINE INDENT while ( node != None ) : NEW_LINE INDENT print ( node . data , end = ' ▁ ' ) NEW_LINE node = node . next NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = newNode ( 1 ) NEW_LINE head . next = newNode ( 2 ) NEW_LINE head . next . next = newNode ( 3 ) NEW_LINE head . next . next . next = newNode ( 4 ) NEW_LINE head . next . next . next . next = newNode ( 5 ) NEW_LINE k = 2 NEW_LINE print ( " List ▁ is " , end = ' ▁ ' ) NEW_LINE prList ( head ) NEW_LINE answer = fractionalNodes ( head , k ) NEW_LINE print ( " Fractional node is " , end = ' ▁ ' ) NEW_LINE print ( answer . data ) NEW_LINE DEDENT
Find modular node in a linked list | Python3 program to find modular node in a linked list ; Linked list node ; Function to create a new node with given data ; Function to find modular node in the linked list ; Corner cases ; Traverse the given list ; 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 ( data ) : NEW_LINE INDENT new_node = Node ( data ) NEW_LINE new_node . data = data NEW_LINE new_node . next = None NEW_LINE return new_node NEW_LINE DEDENT def modularNode ( head , k ) : NEW_LINE INDENT if ( k <= 0 or head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT i = 1 NEW_LINE modularNode = None NEW_LINE temp = head NEW_LINE while ( temp != None ) : NEW_LINE INDENT if ( i % k == 0 ) : NEW_LINE INDENT modularNode = temp NEW_LINE DEDENT i = i + 1 NEW_LINE temp = temp . next NEW_LINE DEDENT return modularNode NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = newNode ( 1 ) NEW_LINE head . next = newNode ( 2 ) NEW_LINE head . next . next = newNode ( 3 ) NEW_LINE head . next . next . next = newNode ( 4 ) NEW_LINE head . next . next . next . next = newNode ( 5 ) NEW_LINE k = 2 NEW_LINE answer = modularNode ( head , k ) NEW_LINE print ( " Modular ▁ node ▁ is " , end = ' ▁ ' ) NEW_LINE if ( answer != None ) : NEW_LINE INDENT print ( answer . data , end = ' ▁ ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " None " ) NEW_LINE DEDENT DEDENT
Modify contents of Linked List | Linked list node ; Function to insert a node at the beginning of the linked list ; Allocate node ; Put in the data ; Link the old list at the end of the new node ; Move the head to point to the new node ; Function to print the linked list ; Function to middle node of list . ; Advance ' fast ' two nodes , and advance ' slow ' one node ; If number of nodes are odd then update slow by slow . next ; ; Function to modify the contents of the linked list . ; Create Stack . ; Traverse the list by using temp until stack is empty . ; Driver code ; creating the linked list ; Call Function to Find the starting point of second half of list . ; Call function to modify the contents of the linked list . ; Print the modified linked list
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def append ( 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 if ( not head ) : NEW_LINE INDENT return ; NEW_LINE DEDENT 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 find_mid ( head ) : NEW_LINE INDENT temp = head NEW_LINE slow = head NEW_LINE fast = head NEW_LINE while ( fast and fast . next ) : NEW_LINE INDENT slow = slow . next NEW_LINE fast = fast . next . next NEW_LINE DEDENT if ( fast ) : NEW_LINE INDENT slow = slow . next NEW_LINE DEDENT return slow NEW_LINE DEDENT def modifyTheList ( head , slow ) : NEW_LINE INDENT s = [ ] NEW_LINE temp = head NEW_LINE while ( slow ) : NEW_LINE INDENT s . append ( slow . data ) NEW_LINE slow = slow . next NEW_LINE DEDENT while ( len ( s ) != 0 ) : NEW_LINE INDENT temp . data = temp . data - s [ - 1 ] NEW_LINE temp = temp . next NEW_LINE s . pop ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = append ( head , 10 ) NEW_LINE head = append ( head , 7 ) NEW_LINE head = append ( head , 12 ) NEW_LINE head = append ( head , 8 ) NEW_LINE head = append ( head , 9 ) NEW_LINE head = append ( head , 2 ) NEW_LINE mid = find_mid ( head ) NEW_LINE modifyTheList ( head , mid ) NEW_LINE print ( " Modified ▁ List : " ) NEW_LINE printList ( head ) NEW_LINE DEDENT
Binary Search Tree | Set 1 ( Search and Insertion ) | A utility function to search a given key in BST ; Base Cases : root is null or key is present at root ; Key is greater than root 's key ; Key is smaller than root 's key
def search ( root , key ) : NEW_LINE INDENT if root is None or root . val == key : NEW_LINE INDENT return root NEW_LINE DEDENT if root . val < key : NEW_LINE INDENT return search ( root . right , key ) NEW_LINE DEDENT return search ( root . left , key ) NEW_LINE DEDENT
Modify a binary tree to get preorder traversal using right pointers only | A binary tree node has data , left child and right child ; Function to modify tree ; if the left tree exists ; get the right - most of the original left subtree ; set root right to left subtree ; if the right subtree does not exists we are done ! ; set right pointer of right - most of the original left subtree ; modify the rightsubtree ; printing using right pointer only ; Driver code ; Constructed binary tree is 10 / \ 8 2 / \ 3 5
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 modifytree ( root ) : NEW_LINE INDENT right = root . right NEW_LINE rightMost = root NEW_LINE if ( root . left ) : NEW_LINE INDENT rightMost = modifytree ( root . left ) NEW_LINE root . right = root . left NEW_LINE root . left = None NEW_LINE DEDENT if ( not right ) : NEW_LINE INDENT return rightMost NEW_LINE DEDENT rightMost . right = right NEW_LINE rightMost = modifytree ( right ) NEW_LINE return rightMost NEW_LINE DEDENT def printpre ( root ) : NEW_LINE INDENT while ( root != None ) : NEW_LINE INDENT print ( root . data , end = " ▁ " ) NEW_LINE root = root . right NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 8 ) NEW_LINE root . right = newNode ( 2 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE modifytree ( root ) NEW_LINE printpre ( root ) NEW_LINE DEDENT
Construct BST from its given level order traversal | Python implementation to construct a BST from its level order traversal ; node of a BST ; function to get a new node ; Allocate memory ; put in the data ; function to construct a BST from its level order traversal ; function to print the inorder traversal ; Driver program
import math NEW_LINE 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 getNode ( data ) : NEW_LINE INDENT newNode = Node ( data ) NEW_LINE newNode . data = data NEW_LINE newNode . left = None NEW_LINE newNode . right = None NEW_LINE return newNode NEW_LINE DEDENT def LevelOrder ( root , data ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT root = getNode ( data ) NEW_LINE return root NEW_LINE DEDENT if ( data <= root . data ) : NEW_LINE INDENT root . left = LevelOrder ( root . left , data ) NEW_LINE DEDENT else : NEW_LINE INDENT root . right = LevelOrder ( root . right , data ) NEW_LINE DEDENT return root NEW_LINE DEDENT def constructBst ( arr , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return None NEW_LINE DEDENT root = None NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT root = LevelOrder ( root , arr [ i ] ) NEW_LINE DEDENT return root NEW_LINE DEDENT def inorderTraversal ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT inorderTraversal ( root . left ) NEW_LINE print ( root . data , end = " ▁ " ) NEW_LINE inorderTraversal ( root . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 4 , 12 , 3 , 6 , 8 , 1 , 5 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE root = constructBst ( arr , n ) NEW_LINE print ( " Inorder ▁ Traversal : ▁ " , end = " " ) NEW_LINE root = inorderTraversal ( root ) NEW_LINE DEDENT
Modify a binary tree to get preorder traversal using right pointers only | A binary tree node has data , left child and right child ; An iterative process to set the right pointer of Binary tree ; Base Case ; Create an empty stack and append root to it ; Pop all items one by one . Do following for every popped item a ) prit b ) append its right child c ) append its left child Note that right child is appended first so that left is processed first ; Pop the top item from stack ; append right and left children of the popped node to stack ; check if some previous node exists ; set the right pointer of previous node to currrent ; set previous node as current node ; printing using right pointer only ; Constructed binary tree is 10 / \ 8 2 / \ 3 5
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 modifytree ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT nodeStack = [ ] NEW_LINE nodeStack . append ( root ) NEW_LINE pre = None NEW_LINE while ( len ( nodeStack ) ) : NEW_LINE INDENT node = nodeStack [ - 1 ] NEW_LINE nodeStack . pop ( ) NEW_LINE if ( node . right ) : NEW_LINE INDENT nodeStack . append ( node . right ) NEW_LINE DEDENT if ( node . left ) : NEW_LINE INDENT nodeStack . append ( node . left ) NEW_LINE DEDENT if ( pre != None ) : NEW_LINE INDENT pre . right = node NEW_LINE DEDENT pre = node NEW_LINE DEDENT DEDENT def printpre ( root ) : NEW_LINE INDENT while ( root != None ) : NEW_LINE INDENT print ( root . data , end = " ▁ " ) NEW_LINE root = root . right NEW_LINE DEDENT DEDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 8 ) NEW_LINE root . right = newNode ( 2 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE modifytree ( root ) NEW_LINE printpre ( root ) NEW_LINE
Check given array of size n can represent BST of n levels or not | A binary tree node has data , left child and right child ; To create a Tree with n levels . We always insert new node to left if it is less than previous value . ; Please refer below post for details of this function . www . geeksforgeeks . org / a - program - to - check - if - a - binary - tree - is - bst - or - not / https : ; Allow only distinct values ; Returns tree if given array of size n can represent a BST of n levels . ; Driver code
class newNode ( ) : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . key = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def createNLevelTree ( arr , n ) : NEW_LINE INDENT root = newNode ( arr [ 0 ] ) NEW_LINE temp = root NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( temp . key > arr [ i ] ) : NEW_LINE INDENT temp . left = newNode ( arr [ i ] ) NEW_LINE temp = temp . left NEW_LINE DEDENT else : NEW_LINE INDENT temp . right = newNode ( arr [ i ] ) NEW_LINE temp = temp . right NEW_LINE DEDENT DEDENT return root NEW_LINE DEDENT def isBST ( root , min , max ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( root . key < min or root . key > max ) : NEW_LINE INDENT return False NEW_LINE DEDENT return ( isBST ( root . left , min , ( root . key ) - 1 ) and isBST ( root . right , ( root . key ) + 1 , max ) ) NEW_LINE DEDENT def canRepresentNLevelBST ( arr , n ) : NEW_LINE INDENT root = createNLevelTree ( arr , n ) NEW_LINE return isBST ( root , 0 , 2 ** 32 ) NEW_LINE DEDENT arr = [ 512 , 330 , 78 , 11 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE if ( canRepresentNLevelBST ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check given array of size n can represent BST of n levels or not | Driver Code ; This element can be inserted to the right of the previous element , only if it is greater than the previous element and in the range . ; max remains same , update min ; This element can be inserted to the left of the previous element , only if it is lesser than the previous element and in the range . ; min remains same , update max ; if the loop completed successfully without encountering else condition
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5123 , 3300 , 783 , 1111 , 890 ] NEW_LINE n = len ( arr ) NEW_LINE max = 2147483647 NEW_LINE min = - 2147483648 NEW_LINE flag = True NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] and arr [ i ] > min and arr [ i ] < max ) : NEW_LINE INDENT min = arr [ i - 1 ] NEW_LINE DEDENT elif ( arr [ i ] < arr [ i - 1 ] and arr [ i ] > min and arr [ i ] < max ) : NEW_LINE INDENT max = arr [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Convert a normal BST to Balanced BST | Python3 program to convert a left unbalanced BST to a balanced BST ; A binary tree node has data , pointer to left child and a pointer to right child ; This function traverse the skewed binary tree and stores its nodes pointers in vector nodes [ ] ; Base case ; Store nodes in Inorder ( which is sorted order for BST ) ; Recursive function to construct binary tree ; base case ; Get the middle element and make it root ; Using index in Inorder traversal , construct left and right subtress ; This functions converts an unbalanced BST to a balanced BST ; Store nodes of given BST in sorted order ; Constucts BST from nodes [ ] ; Function to do preorder traversal of tree ; Driver code ; Constructed skewed binary tree is 10 / 8 / 7 / 6 / 5
import sys NEW_LINE import math NEW_LINE 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 storeBSTNodes ( root , nodes ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return NEW_LINE DEDENT storeBSTNodes ( root . left , nodes ) NEW_LINE nodes . append ( root ) NEW_LINE storeBSTNodes ( root . right , nodes ) NEW_LINE DEDENT def buildTreeUtil ( nodes , start , end ) : NEW_LINE INDENT if start > end : NEW_LINE INDENT return None NEW_LINE DEDENT mid = ( start + end ) // 2 NEW_LINE node = nodes [ mid ] NEW_LINE node . left = buildTreeUtil ( nodes , start , mid - 1 ) NEW_LINE node . right = buildTreeUtil ( nodes , mid + 1 , end ) NEW_LINE return node NEW_LINE DEDENT def buildTree ( root ) : NEW_LINE INDENT nodes = [ ] NEW_LINE storeBSTNodes ( root , nodes ) NEW_LINE n = len ( nodes ) NEW_LINE return buildTreeUtil ( nodes , 0 , n - 1 ) NEW_LINE DEDENT def preOrder ( root ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return NEW_LINE DEDENT print ( " { } ▁ " . format ( root . data ) , end = " " ) NEW_LINE preOrder ( root . left ) NEW_LINE preOrder ( root . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 10 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . left . left = Node ( 7 ) NEW_LINE root . left . left . left = Node ( 6 ) NEW_LINE root . left . left . left . left = Node ( 5 ) NEW_LINE root = buildTree ( root ) NEW_LINE print ( " Preorder ▁ traversal ▁ of ▁ balanced ▁ BST ▁ is ▁ : " ) NEW_LINE preOrder ( root ) NEW_LINE DEDENT
Find the node with minimum value in a Binary Search Tree | A binary tree node ; Give a binary search tree and a number , inserts a new node with the given number in the correct place in the tree . Returns the new root pointer which the caller should then use ( the standard trick to avoid using reference parameters ) . ; 1. If the tree is empty , return a new , single node ; 2. Otherwise , recur down the tree ; Return the ( unchanged ) node pointer ; Given a non - empty binary search tree , return the minimum data value found in that tree . Note that the entire tree does not need to be searched . ; loop down to find the lefmost leaf ; Driver program
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def insert ( node , data ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return ( Node ( data ) ) NEW_LINE DEDENT else : NEW_LINE INDENT if data <= node . data : NEW_LINE INDENT node . left = insert ( node . left , data ) NEW_LINE DEDENT else : NEW_LINE INDENT node . right = insert ( node . right , data ) NEW_LINE DEDENT return node NEW_LINE DEDENT DEDENT def minValue ( node ) : NEW_LINE INDENT current = node NEW_LINE while ( current . left is not None ) : NEW_LINE INDENT current = current . left NEW_LINE DEDENT return current . data NEW_LINE DEDENT root = None NEW_LINE root = insert ( root , 4 ) NEW_LINE insert ( root , 2 ) NEW_LINE insert ( root , 1 ) NEW_LINE insert ( root , 3 ) NEW_LINE insert ( root , 6 ) NEW_LINE insert ( root , 5 ) NEW_LINE print   " NEW_LINE Minimum value in BST is % d " % ( minValue ( root ) ) NEW_LINE
Check if the given array can represent Level Order Traversal of Binary Search Tree | To store details of a node like node ' s ▁ ▁ data , ▁ ' min ' ▁ and ▁ ' max ' ▁ to ▁ obtain ▁ the ▁ ▁ range ▁ of ▁ values ▁ where ▁ node ' s left and right child 's should lie  ; function to check if the given array can represent Level Order Traversal of Binary Search Tree ; if tree is empty ; queue to store NodeDetails ; index variable to access array elements ; node details for the root of the BST ; until there are no more elements in arr [ ] or queue is not empty ; extracting NodeDetails of a node from the queue ; check whether there are more elements in the arr [ ] and arr [ i ] can be left child of ' temp . data ' or not ; Create NodeDetails for newNode / and add it to the queue ; check whether there are more elements in the arr [ ] and arr [ i ] can be right child of ' temp . data ' or not ; Create NodeDetails for newNode / and add it to the queue ; given array represents level order traversal of BST ; given array do not represent level order traversal of BST ; Driver code
class NodeDetails : NEW_LINE INDENT def __init__ ( self , data , min , max ) : NEW_LINE INDENT self . data = data NEW_LINE self . min = min NEW_LINE self . max = max NEW_LINE DEDENT DEDENT def levelOrderIsOfBST ( arr , n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT q = [ ] NEW_LINE i = 0 NEW_LINE newNode = NodeDetails ( arr [ i ] , INT_MIN , INT_MAX ) NEW_LINE i += 1 NEW_LINE q . append ( newNode ) NEW_LINE while i != n and len ( q ) != 0 : NEW_LINE INDENT temp = q . pop ( 0 ) NEW_LINE if i < n and ( arr [ i ] < temp . data and arr [ i ] > temp . min ) : NEW_LINE INDENT newNode = NodeDetails ( arr [ i ] , temp . min , temp . data ) NEW_LINE i += 1 NEW_LINE q . append ( newNode ) NEW_LINE DEDENT if i < n and ( arr [ i ] > temp . data and arr [ i ] < temp . max ) : NEW_LINE INDENT newNode = NodeDetails ( arr [ i ] , temp . data , temp . max ) NEW_LINE i += 1 NEW_LINE q . append ( newNode ) NEW_LINE DEDENT DEDENT if i == n : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 4 , 12 , 3 , 6 , 8 , 1 , 5 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE if levelOrderIsOfBST ( arr , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Construct Tree from given Inorder and Preorder traversals | A binary tree node ; Recursive function to construct binary of size len from Inorder traversal in [ ] and Preorder traversal pre [ ] . Initial values of inStrt and inEnd should be 0 and len - 1. The function doesn 't  do any error checking for cases where inorder and preorder  do not form a tree  ; Pich current node from Preorder traversal using preIndex and increment preIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder Traversal , construct left and right subtrees ; Function to find index of value in arr [ start ... end ] The function assumes that value is rpesent in inOrder [ ] ; This funtcion is here just to test buildTree ( ) ; first recur on left child ; then print the data of node ; now recur on right child ; Driver program to test above function ; Let us test the build tree by priting Inorder traversal
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 buildTree ( inOrder , preOrder , inStrt , inEnd ) : NEW_LINE INDENT if ( inStrt > inEnd ) : NEW_LINE INDENT return None NEW_LINE DEDENT tNode = Node ( preOrder [ buildTree . preIndex ] ) NEW_LINE buildTree . preIndex += 1 NEW_LINE if inStrt == inEnd : NEW_LINE INDENT return tNode NEW_LINE DEDENT inIndex = search ( inOrder , inStrt , inEnd , tNode . data ) NEW_LINE tNode . left = buildTree ( inOrder , preOrder , inStrt , inIndex - 1 ) NEW_LINE tNode . right = buildTree ( inOrder , preOrder , inIndex + 1 , inEnd ) NEW_LINE return tNode NEW_LINE DEDENT def search ( arr , start , end , value ) : NEW_LINE INDENT for i in range ( start , end + 1 ) : NEW_LINE INDENT if arr [ i ] == value : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT DEDENT def printInorder ( node ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return NEW_LINE DEDENT printInorder ( node . left ) NEW_LINE print node . data , NEW_LINE printInorder ( node . right ) NEW_LINE DEDENT inOrder = [ ' D ' , ' B ' , ' E ' , ' A ' , ' F ' , ' C ' ] NEW_LINE preOrder = [ ' A ' , ' B ' , ' D ' , ' E ' , ' C ' , ' F ' ] NEW_LINE buildTree . preIndex = 0 NEW_LINE root = buildTree ( inOrder , preOrder , 0 , len ( inOrder ) - 1 ) NEW_LINE print " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree ▁ is " NEW_LINE printInorder ( root ) NEW_LINE