codeparrot/xlcost-text-to-code huggingface dataset:
Find the closest element in Binary Search Tree | Utility that allocates a new node with the given key and NULL left and right pointers . ; Function to find node with minimum absolute difference with given K min_diff -- > minimum difference till now min_diff_key -- > node having minimum absolute difference with K ; If k itself is present ; update min_diff and min_diff_key by checking current node value ; if k is less than ptr -> key then move in left subtree else in right subtree ; Wrapper over maxDiffUtil ( ) ; Initialize minimum difference ; Find value of min_diff_key ( Closest key in tree with k ) ; 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 maxDiffUtil ( ptr , k , min_diff , min_diff_key ) : NEW_LINE INDENT if ptr == None : NEW_LINE INDENT return NEW_LINE DEDENT if ptr . key == k : NEW_LINE INDENT min_diff_key [ 0 ] = k NEW_LINE return NEW_LINE DEDENT if min_diff > abs ( ptr . key - k ) : NEW_LINE INDENT min_diff = abs ( ptr . key - k ) NEW_LINE min_diff_key [ 0 ] = ptr . key NEW_LINE DEDENT if k < ptr . key : NEW_LINE INDENT maxDiffUtil ( ptr . left , k , min_diff , min_diff_key ) NEW_LINE DEDENT else : NEW_LINE INDENT maxDiffUtil ( ptr . right , k , min_diff , min_diff_key ) NEW_LINE DEDENT DEDENT def maxDiff ( root , k ) : NEW_LINE INDENT min_diff , min_diff_key = 999999999999 , [ - 1 ] NEW_LINE maxDiffUtil ( root , k , min_diff , min_diff_key ) NEW_LINE return min_diff_key [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newnode ( 9 ) NEW_LINE root . left = newnode ( 4 ) NEW_LINE root . right = newnode ( 17 ) NEW_LINE root . left . left = newnode ( 3 ) NEW_LINE root . left . right = newnode ( 6 ) NEW_LINE root . left . right . left = newnode ( 5 ) NEW_LINE root . left . right . right = newnode ( 7 ) NEW_LINE root . right . right = newnode ( 22 ) NEW_LINE root . right . right . left = newnode ( 20 ) NEW_LINE k = 18 NEW_LINE print ( maxDiff ( root , k ) ) NEW_LINE DEDENT
Construct a complete binary tree from given array in level order fashion | Helper function that allocates a new node ; Function to insert nodes in level order ; Base case for recursion ; insert left child ; insert right child ; Function to print tree nodes in InOrder fashion ; 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 insertLevelOrder ( arr , root , i , n ) : NEW_LINE INDENT if i < n : NEW_LINE INDENT temp = newNode ( arr [ i ] ) NEW_LINE root = temp NEW_LINE root . left = insertLevelOrder ( arr , root . left , 2 * i + 1 , n ) NEW_LINE root . right = insertLevelOrder ( arr , root . right , 2 * i + 2 , n ) NEW_LINE DEDENT return root NEW_LINE DEDENT def inOrder ( root ) : NEW_LINE INDENT if root != None : NEW_LINE INDENT inOrder ( root . left ) NEW_LINE print ( root . data , end = " ▁ " ) NEW_LINE inOrder ( root . right ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 6 , 6 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE root = None NEW_LINE root = insertLevelOrder ( arr , root , 0 , n ) NEW_LINE inOrder ( root ) NEW_LINE DEDENT
Construct Full Binary Tree from given preorder and postorder traversals | A binary tree node has data , pointer to left child and a pointer to right child ; A recursive function to construct Full from pre [ ] and post [ ] . preIndex is used to keep track of index in pre [ ] . l is low index and h is high index for the current subarray in post [ ] ; Base case ; The first node in preorder traversal is root . So take the node at preIndex from preorder and make it root , and increment preIndex ; If the current subarry has only one element , no need to recur ; Search the next element of pre [ ] in post [ ] ; Use the index of element found in postorder to divide postorder array in two parts . Left subtree and right subtree ; The main function to construct Full Binary Tree from given preorder and postorder traversals . This function mainly uses constructTreeUtil ( ) ; A utility function to print inorder traversal of a Binary Tree ; Driver code
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 constructTreeUtil ( pre : list , post : list , l : int , h : int , size : int ) -> Node : NEW_LINE INDENT global preIndex NEW_LINE if ( preIndex >= size or l > h ) : NEW_LINE INDENT return None NEW_LINE DEDENT root = Node ( pre [ preIndex ] ) NEW_LINE preIndex += 1 NEW_LINE if ( l == h or preIndex >= size ) : NEW_LINE INDENT return root NEW_LINE DEDENT i = l NEW_LINE while i <= h : NEW_LINE INDENT if ( pre [ preIndex ] == post [ i ] ) : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( i <= h ) : NEW_LINE INDENT root . left = constructTreeUtil ( pre , post , l , i , size ) NEW_LINE root . right = constructTreeUtil ( pre , post , i + 1 , h , size ) NEW_LINE DEDENT return root NEW_LINE DEDENT def constructTree ( pre : list , post : list , size : int ) -> Node : NEW_LINE INDENT global preIndex NEW_LINE return constructTreeUtil ( pre , post , 0 , size - 1 , size ) NEW_LINE DEDENT def printInorder ( node : Node ) -> None : NEW_LINE INDENT if ( node is None ) : NEW_LINE INDENT return NEW_LINE DEDENT printInorder ( node . left ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE printInorder ( node . right ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT pre = [ 1 , 2 , 4 , 8 , 9 , 5 , 3 , 6 , 7 ] NEW_LINE post = [ 8 , 9 , 4 , 5 , 2 , 6 , 7 , 3 , 1 ] NEW_LINE size = len ( pre ) NEW_LINE preIndex = 0 NEW_LINE root = constructTree ( pre , post , size ) NEW_LINE print ( " Inorder ▁ traversal ▁ of ▁ " " the ▁ constructed ▁ tree : ▁ " ) NEW_LINE printInorder ( root ) NEW_LINE DEDENT
Convert a Binary Tree to Threaded binary tree | Set 2 ( Efficient ) | Converts tree with given root to threaded binary tree . This function returns rightmost child of root . ; Base cases : Tree is empty or has single node ; Find predecessor if it exists ; Find predecessor of root ( Rightmost child in left subtree ) ; Link a thread from predecessor to root . ; If current node is rightmost child ; Recur for right subtree . ; A utility function to find leftmost node in a binary tree rooted with ' root ' . This function is used in inOrder ( ) ; Function to do inorder traversal of a threaded binary tree ; Find the leftmost node in Binary Tree ; If this Node is a thread Node , then go to inorder successor ; Else go to the leftmost child in right subtree ; A utility function to create a new node ; Driver Code ; 1 / \ 2 3 / \ / \ 4 5 6 7
def createThreaded ( root ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return None NEW_LINE DEDENT if root . left == None and root . right == None : NEW_LINE INDENT return root NEW_LINE DEDENT if root . left != None : NEW_LINE INDENT l = createThreaded ( root . left ) NEW_LINE l . right = root NEW_LINE l . isThreaded = True NEW_LINE DEDENT if root . right == None : NEW_LINE INDENT return root NEW_LINE DEDENT return createThreaded ( root . right ) NEW_LINE DEDENT def leftMost ( root ) : NEW_LINE INDENT while root != None and root . left != None : NEW_LINE INDENT root = root . left NEW_LINE DEDENT return root NEW_LINE DEDENT def inOrder ( root ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return NEW_LINE DEDENT cur = leftMost ( root ) NEW_LINE while cur != None : NEW_LINE INDENT print ( cur . key , end = " ▁ " ) NEW_LINE if cur . isThreaded : NEW_LINE INDENT cur = cur . right NEW_LINE DEDENT else : NEW_LINE INDENT cur = leftMost ( cur . right ) NEW_LINE DEDENT DEDENT DEDENT class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . left = self . right = None NEW_LINE self . key = key NEW_LINE self . isThreaded = None NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . left = newNode ( 6 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE createThreaded ( root ) NEW_LINE print ( " Inorder ▁ traversal ▁ of ▁ created " , " threaded ▁ tree ▁ is " ) NEW_LINE inOrder ( root ) NEW_LINE DEDENT
Inorder Non | A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Function to print inorder traversal using parent pointer ; Start traversal from root ; If left child is not traversed , find the leftmost child ; Print root 's data ; Mark left as done ; If right child exists ; If right child doesn 't exist, move to parent ; If this node is right child of its parent , visit parent 's parent first ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , item ) : NEW_LINE INDENT self . key = item NEW_LINE self . parent = self . left = self . right = None NEW_LINE DEDENT DEDENT def insert ( node , key ) : NEW_LINE INDENT if node == None : NEW_LINE INDENT return newNode ( key ) NEW_LINE DEDENT if key < node . key : NEW_LINE INDENT node . left = insert ( node . left , key ) NEW_LINE node . left . parent = node NEW_LINE DEDENT elif key > node . key : NEW_LINE INDENT node . right = insert ( node . right , key ) NEW_LINE node . right . parent = node NEW_LINE DEDENT return node NEW_LINE DEDENT def inorder ( root ) : NEW_LINE INDENT leftdone = False NEW_LINE while root : NEW_LINE INDENT if leftdone == False : NEW_LINE INDENT while root . left : NEW_LINE INDENT root = root . left NEW_LINE DEDENT DEDENT print ( root . key , end = " ▁ " ) NEW_LINE leftdone = True NEW_LINE if root . right : NEW_LINE INDENT leftdone = False NEW_LINE root = root . right NEW_LINE DEDENT elif root . parent : NEW_LINE INDENT while root . parent and root == root . parent . right : NEW_LINE INDENT root = root . parent NEW_LINE DEDENT if root . parent == None : NEW_LINE INDENT break NEW_LINE DEDENT root = root . parent NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 24 ) NEW_LINE root = insert ( root , 27 ) NEW_LINE root = insert ( root , 29 ) NEW_LINE root = insert ( root , 34 ) NEW_LINE root = insert ( root , 14 ) NEW_LINE root = insert ( root , 4 ) NEW_LINE root = insert ( root , 10 ) NEW_LINE root = insert ( root , 22 ) NEW_LINE root = insert ( root , 13 ) NEW_LINE root = insert ( root , 3 ) NEW_LINE root = insert ( root , 2 ) NEW_LINE root = insert ( root , 6 ) NEW_LINE print ( " Inorder ▁ traversal ▁ is ▁ " ) NEW_LINE inorder ( root ) NEW_LINE DEDENT
Sorted order printing of a given array that represents a BST | Python3 Code for Sorted order printing of a given array that represents a BST ; print left subtree ; print root ; print right subtree ; Driver Code
def printSorted ( arr , start , end ) : NEW_LINE INDENT if start > end : NEW_LINE INDENT return NEW_LINE DEDENT printSorted ( arr , start * 2 + 1 , end ) NEW_LINE print ( arr [ start ] , end = " ▁ " ) NEW_LINE printSorted ( arr , start * 2 + 2 , end ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 2 , 5 , 1 , 3 ] NEW_LINE arr_size = len ( arr ) NEW_LINE printSorted ( arr , 0 , arr_size - 1 ) NEW_LINE DEDENT
Floor and Ceil from a BST | A Binary tree node ; Function to find ceil of a given input in BST . If input is more than the max key in BST , return - 1 ; Base Case ; We found equal key ; If root 's key is smaller, ceil must be in right subtree ; Else , either left subtre or root has the ceil value ; Driver program to test above function
class Node : 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 ceil ( root , inp ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if root . key == inp : NEW_LINE INDENT return root . key NEW_LINE DEDENT if root . key < inp : NEW_LINE INDENT return ceil ( root . right , inp ) NEW_LINE DEDENT val = ceil ( root . left , inp ) NEW_LINE return val if val >= inp else root . key NEW_LINE DEDENT root = Node ( 8 ) NEW_LINE root . left = Node ( 4 ) NEW_LINE root . right = Node ( 12 ) NEW_LINE root . left . left = Node ( 2 ) NEW_LINE root . left . right = Node ( 6 ) NEW_LINE root . right . left = Node ( 10 ) NEW_LINE root . right . right = Node ( 14 ) NEW_LINE for i in range ( 16 ) : NEW_LINE INDENT print " % ▁ d ▁ % ▁ d " % ( i , ceil ( root , i ) ) NEW_LINE DEDENT
Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree | Utility function to create a new tree node ; A utility function to print inorder traversal of a Binary Tree ; A recursive function to construct Full binary tree from pre [ ] and preM [ ] . preIndex is used to keep track of index in pre [ ] . l is low index and h is high index for the current subarray in preM [ ] ; Base case ; The first node in preorder traversal is root . So take the node at preIndex from preorder and make it root , and increment preIndex ; If the current subarry has only one element , no need to recur ; Search the next element of pre [ ] in preM [ ] ; construct left and right subtrees recursively ; return root ; function to construct full binary tree using its preorder traversal and preorder traversal of its mirror tree ; 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 prInorder ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT prInorder ( node . left ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE prInorder ( node . right ) NEW_LINE DEDENT def constructBinaryTreeUtil ( pre , preM , preIndex , l , h , size ) : NEW_LINE INDENT if ( preIndex >= size or l > h ) : NEW_LINE INDENT return None , preIndex NEW_LINE DEDENT root = newNode ( pre [ preIndex ] ) NEW_LINE preIndex += 1 NEW_LINE if ( l == h ) : NEW_LINE INDENT return root , preIndex NEW_LINE DEDENT i = 0 NEW_LINE for i in range ( l , h + 1 ) : NEW_LINE INDENT if ( pre [ preIndex ] == preM [ i ] ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( i <= h ) : NEW_LINE INDENT root . left , preIndex = constructBinaryTreeUtil ( pre , preM , preIndex , i , h , size ) NEW_LINE root . right , preIndex = constructBinaryTreeUtil ( pre , preM , preIndex , l + 1 , i - 1 , size ) NEW_LINE DEDENT return root , preIndex NEW_LINE DEDENT def constructBinaryTree ( root , pre , preMirror , size ) : NEW_LINE INDENT preIndex = 0 NEW_LINE preMIndex = 0 NEW_LINE root , x = constructBinaryTreeUtil ( pre , preMirror , preIndex , 0 , size - 1 , size ) NEW_LINE prInorder ( root ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT preOrder = [ 1 , 2 , 4 , 5 , 3 , 6 , 7 ] NEW_LINE preOrderMirror = [ 1 , 3 , 7 , 6 , 2 , 5 , 4 ] NEW_LINE size = 7 NEW_LINE root = newNode ( 0 ) NEW_LINE constructBinaryTree ( root , preOrder , preOrderMirror , size ) NEW_LINE DEDENT
Floor and Ceil from a BST | A binary tree node has key , . left child and right child ; Helper function to find floor and ceil of a given key in BST ; Display the floor and ceil of a given key in BST . If key is less than the min key in BST , floor will be - 1 ; If key is more than the max key in BST , ceil will be - 1 ; ; Variables ' floor ' and ' ceil ' are passed by reference ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def floorCeilBSTHelper ( root , key ) : NEW_LINE INDENT global floor , ceil NEW_LINE while ( root ) : NEW_LINE INDENT if ( root . data == key ) : NEW_LINE INDENT ceil = root . data NEW_LINE floor = root . data NEW_LINE return NEW_LINE DEDENT if ( key > root . data ) : NEW_LINE INDENT floor = root . data NEW_LINE root = root . right NEW_LINE DEDENT else : NEW_LINE INDENT ceil = root . data NEW_LINE root = root . left NEW_LINE DEDENT DEDENT DEDENT def floorCeilBST ( root , key ) : NEW_LINE INDENT global floor , ceil NEW_LINE floor = - 1 NEW_LINE ceil = - 1 NEW_LINE floorCeilBSTHelper ( root , key ) NEW_LINE print ( key , floor , ceil ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT floor , ceil = - 1 , - 1 NEW_LINE root = Node ( 8 ) NEW_LINE root . left = Node ( 4 ) NEW_LINE root . right = Node ( 12 ) NEW_LINE root . left . left = Node ( 2 ) NEW_LINE root . left . right = Node ( 6 ) NEW_LINE root . right . left = Node ( 10 ) NEW_LINE root . right . right = Node ( 14 ) NEW_LINE for i in range ( 16 ) : NEW_LINE INDENT floorCeilBST ( root , i ) NEW_LINE DEDENT DEDENT
Leaf nodes from Preorder of a Binary Search Tree | Binary Search ; Poto the index in preorder . ; Function to prLeaf Nodes by doing preorder traversal of tree using preorder and inorder arrays . ; If l == r , therefore no right or left subtree . So , it must be leaf Node , print it . ; If array is out of bound , return . ; Finding the index of preorder element in inorder array using binary search . ; Incrementing the index . ; Finding on the left subtree . ; Finding on the right subtree . ; Finds leaf nodes from given preorder traversal . ; To store inorder traversal ; Copy the preorder into another array . ; Finding the inorder by sorting the array . ; Print the Leaf Nodes . ; Driver Code
def binarySearch ( inorder , l , r , d ) : NEW_LINE INDENT mid = ( l + r ) >> 1 NEW_LINE if ( inorder [ mid ] == d ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( inorder [ mid ] > d ) : NEW_LINE INDENT return binarySearch ( inorder , l , mid - 1 , d ) NEW_LINE DEDENT else : NEW_LINE INDENT return binarySearch ( inorder , mid + 1 , r , d ) NEW_LINE DEDENT ind = [ 0 ] NEW_LINE DEDENT def leafNodesRec ( preorder , inorder , l , r , ind , n ) : NEW_LINE INDENT if ( l == r ) : NEW_LINE INDENT print ( inorder [ l ] , end = " ▁ " ) NEW_LINE ind [ 0 ] = ind [ 0 ] + 1 NEW_LINE return NEW_LINE DEDENT if ( l < 0 or l > r or r >= n ) : NEW_LINE INDENT return NEW_LINE DEDENT loc = binarySearch ( inorder , l , r , preorder [ ind [ 0 ] ] ) NEW_LINE ind [ 0 ] = ind [ 0 ] + 1 NEW_LINE leafNodesRec ( preorder , inorder , l , loc - 1 , ind , n ) NEW_LINE leafNodesRec ( preorder , inorder , loc + 1 , r , ind , n ) NEW_LINE DEDENT def leafNodes ( preorder , n ) : NEW_LINE INDENT inorder = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT inorder [ i ] = preorder [ i ] NEW_LINE DEDENT inorder . sort ( ) NEW_LINE ind = [ 0 ] NEW_LINE leafNodesRec ( preorder , inorder , 0 , n - 1 , ind , n ) NEW_LINE DEDENT preorder = [ 890 , 325 , 290 , 530 , 965 ] NEW_LINE n = len ( preorder ) NEW_LINE leafNodes ( preorder , n ) NEW_LINE
Leaf nodes from Preorder of a Binary Search Tree | Print the leaf node from the given preorder of BST . ; Since rightmost element is always leaf node . ; Driver code
def leafNode ( preorder , n ) : NEW_LINE INDENT s = [ ] NEW_LINE i = 0 NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT found = False NEW_LINE if preorder [ i ] > preorder [ j ] : NEW_LINE INDENT s . append ( preorder [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT while len ( s ) != 0 : NEW_LINE INDENT if preorder [ j ] > s [ - 1 ] : NEW_LINE INDENT s . pop ( - 1 ) NEW_LINE found = True NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if found : NEW_LINE INDENT print ( preorder [ i ] , end = " ▁ " ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT print ( preorder [ n - 1 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT preorder = [ 890 , 325 , 290 , 530 , 965 ] NEW_LINE n = len ( preorder ) NEW_LINE leafNode ( preorder , n ) NEW_LINE DEDENT
Leaf nodes from Preorder of a Binary Search Tree ( Using Recursion ) | Print the leaf node from the given preorder of BST . ; Driver code
def isLeaf ( pre , i , n , Min , Max ) : NEW_LINE INDENT if i [ 0 ] >= n : NEW_LINE INDENT return False NEW_LINE DEDENT if pre [ i [ 0 ] ] > Min and pre [ i [ 0 ] ] < Max : NEW_LINE INDENT i [ 0 ] += 1 NEW_LINE left = isLeaf ( pre , i , n , Min , pre [ i [ 0 ] - 1 ] ) NEW_LINE right = isLeaf ( pre , i , n , pre [ i [ 0 ] - 1 ] , Max ) NEW_LINE if left == False and right == False : NEW_LINE INDENT print ( pre [ i [ 0 ] - 1 ] , end = " ▁ " ) NEW_LINE DEDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def printLeaves ( preorder , n ) : NEW_LINE INDENT i = [ 0 ] NEW_LINE INT_MIN , INT_MAX = - 999999999999 , 999999999999 NEW_LINE isLeaf ( preorder , i , n , INT_MIN , INT_MAX ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT preorder = [ 890 , 325 , 290 , 530 , 965 ] NEW_LINE n = len ( preorder ) NEW_LINE printLeaves ( preorder , n ) NEW_LINE DEDENT
Binary Search Tree insert with Parent Pointer | A utility function to create a new BST Node ; A utility function to do inorder traversal of BST ; A utility function to insert a new Node with given key in BST ; If the tree is empty , return a new Node ; Otherwise , recur down the tree ; Set parent of root of left subtree ; Set parent of root of right subtree ; return the ( unchanged ) Node pointer ; Driver Code ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 ; print iNoder traversal of the BST
class newNode : NEW_LINE INDENT def __init__ ( self , item ) : NEW_LINE INDENT self . key = item NEW_LINE self . left = self . right = None NEW_LINE self . parent = None NEW_LINE DEDENT DEDENT def inorder ( root ) : NEW_LINE INDENT if root != None : NEW_LINE INDENT inorder ( root . left ) NEW_LINE print ( " Node ▁ : " , root . key , " , ▁ " , end = " " ) NEW_LINE if root . parent == None : NEW_LINE INDENT print ( " Parent ▁ : ▁ NULL " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Parent ▁ : ▁ " , root . parent . key ) NEW_LINE DEDENT inorder ( root . right ) NEW_LINE DEDENT DEDENT def insert ( node , key ) : NEW_LINE INDENT if node == None : NEW_LINE INDENT return newNode ( key ) NEW_LINE DEDENT if key < node . key : NEW_LINE INDENT lchild = insert ( node . left , key ) NEW_LINE node . left = lchild NEW_LINE lchild . parent = node NEW_LINE DEDENT elif key > node . key : NEW_LINE INDENT rchild = insert ( node . right , key ) NEW_LINE node . right = rchild NEW_LINE rchild . parent = node NEW_LINE DEDENT return node NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 50 ) NEW_LINE insert ( root , 30 ) NEW_LINE insert ( root , 20 ) NEW_LINE insert ( root , 40 ) NEW_LINE insert ( root , 70 ) NEW_LINE insert ( root , 60 ) NEW_LINE insert ( root , 80 ) NEW_LINE inorder ( root ) NEW_LINE DEDENT