codeparrot/xlcost-text-to-code huggingface dataset:
Check if two BSTs contain same set of elements | BST Node ; Utility function to create Node ; function to insert elements of the tree to map m ; function to check if the two BSTs contain same set of elements ; Base cases ; Create two hash sets and store elements both BSTs in them . ; Return True if both hash sets contain same elements . ; First BST ; Second BST ; check if two BSTs have same set of elements
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . val = 0 NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def Node_ ( val1 ) : NEW_LINE INDENT temp = Node ( ) NEW_LINE temp . val = val1 NEW_LINE temp . left = temp . right = None NEW_LINE return temp NEW_LINE DEDENT v = [ ] NEW_LINE def storeInorder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT storeInorder ( root . left ) NEW_LINE v . append ( root . data ) NEW_LINE storeInorder ( root . right ) NEW_LINE DEDENT def checkBSTs ( root1 , root2 ) : NEW_LINE INDENT if ( root1 != None and root2 != None ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( ( root1 == None and root2 != None ) or ( root1 != None and root2 == None ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT v1 = [ ] NEW_LINE v2 = [ ] NEW_LINE v = v1 NEW_LINE storeInorder ( root1 ) NEW_LINE v1 = v NEW_LINE v = v2 NEW_LINE storeInorder ( root2 ) NEW_LINE v2 = v NEW_LINE return ( v1 == v2 ) NEW_LINE DEDENT root1 = Node_ ( 15 ) NEW_LINE root1 . left = Node_ ( 10 ) NEW_LINE root1 . right = Node_ ( 20 ) NEW_LINE root1 . left . left = Node_ ( 5 ) NEW_LINE root1 . left . right = Node_ ( 12 ) NEW_LINE root1 . right . right = Node_ ( 25 ) NEW_LINE root2 = Node_ ( 15 ) NEW_LINE root2 . left = Node_ ( 12 ) NEW_LINE root2 . right = Node_ ( 20 ) NEW_LINE root2 . left . left = Node_ ( 5 ) NEW_LINE root2 . left . left . right = Node_ ( 10 ) NEW_LINE root2 . right . right = Node_ ( 25 ) NEW_LINE if ( checkBSTs ( root1 , root2 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Shortest distance between two nodes in BST | Python3 program to find distance between two nodes in BST ; Standard BST insert function ; This function returns distance of x from root . This function assumes that x exists in BST and BST is not NULL . ; Returns minimum distance beween a and b . This function assumes that a and b exist in BST . ; Both keys lie in left ; Both keys lie in right same path ; Lie in opposite directions ( Root is LCA of two nodes ) ; This function make sure that a is smaller than b before making a call to findDistWrapper ( ) ; 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 insert ( root , key ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT root = newNode ( key ) NEW_LINE DEDENT elif root . key > key : NEW_LINE INDENT root . left = insert ( root . left , key ) NEW_LINE DEDENT elif root . key < key : NEW_LINE INDENT root . right = insert ( root . right , key ) NEW_LINE DEDENT return root NEW_LINE DEDENT def distanceFromRoot ( root , x ) : NEW_LINE INDENT if root . key == x : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif root . key > x : NEW_LINE INDENT return 1 + distanceFromRoot ( root . left , x ) NEW_LINE DEDENT return 1 + distanceFromRoot ( root . right , x ) NEW_LINE DEDENT def distanceBetween2 ( root , a , b ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return 0 NEW_LINE DEDENT if root . key > a and root . key > b : NEW_LINE INDENT return distanceBetween2 ( root . left , a , b ) NEW_LINE DEDENT if root . key < a and root . key < b : NEW_LINE INDENT return distanceBetween2 ( root . right , a , b ) NEW_LINE DEDENT if root . key >= a and root . key <= b : NEW_LINE INDENT return ( distanceFromRoot ( root , a ) + distanceFromRoot ( root , b ) ) NEW_LINE DEDENT DEDENT def findDistWrapper ( root , a , b ) : NEW_LINE INDENT if a > b : NEW_LINE INDENT a , b = b , a NEW_LINE DEDENT return distanceBetween2 ( root , a , b ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 20 ) NEW_LINE insert ( root , 10 ) NEW_LINE insert ( root , 5 ) NEW_LINE insert ( root , 15 ) NEW_LINE insert ( root , 30 ) NEW_LINE insert ( root , 25 ) NEW_LINE insert ( root , 35 ) NEW_LINE a , b = 5 , 55 NEW_LINE print ( findDistWrapper ( root , 5 , 35 ) ) NEW_LINE DEDENT
Print BST keys in given Range | O ( 1 ) Space | Python3 code to print BST keys in given Range in constant space using Morris traversal . Helper function to create a new node ; Function to print the keys in range ; check if current node lies between n1 and n2 ; finding the inorder predecessor - inorder predecessor is the right most in left subtree or the left child , i . e in BST it is the maximum ( right most ) in left subtree . ; check if current node lies between n1 and n2 ; Driver Code ; Constructed binary tree is 4 / \ 2 7 / \ / \ 1 3 6 10
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 RangeTraversal ( root , n1 , n2 ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return NEW_LINE DEDENT curr = root NEW_LINE while curr : NEW_LINE INDENT if curr . left == None : NEW_LINE INDENT if curr . data <= n2 and curr . data >= n1 : NEW_LINE INDENT print ( curr . data , end = " ▁ " ) NEW_LINE DEDENT curr = curr . right NEW_LINE DEDENT else : NEW_LINE INDENT pre = curr . left NEW_LINE while ( pre . right != None and pre . right != curr ) : NEW_LINE INDENT pre = pre . right NEW_LINE DEDENT if pre . right == None : NEW_LINE INDENT pre . right = curr ; NEW_LINE curr = curr . left NEW_LINE DEDENT else : NEW_LINE INDENT pre . right = None NEW_LINE if curr . data <= n2 and curr . data >= n1 : NEW_LINE INDENT print ( curr . data , end = " ▁ " ) NEW_LINE DEDENT curr = curr . right NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 4 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 7 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 3 ) NEW_LINE root . right . left = newNode ( 6 ) NEW_LINE root . right . right = newNode ( 10 ) NEW_LINE RangeTraversal ( root , 4 , 12 ) NEW_LINE DEDENT
Count BST subtrees that lie in given range | A utility function to check if data of root is in range from low to high ; A recursive function to get count of nodes whose subtree is in range from low to high . This function returns true if nodes in subtree rooted under ' root ' are in range . ; Base case ; Recur for left and right subtrees ; If both left and right subtrees are in range and current node is also in range , then increment count and return true ; A wrapper over getCountUtil ( ) . This function initializes count as 0 and calls getCountUtil ( ) ; Utility function to create new node ; Driver Code ; Let us construct the BST shown in the above figure ; Let us constructed BST shown in above example 10 / \ 5 50 / / \ 1 40 100
def inRange ( root , low , high ) : NEW_LINE INDENT return root . data >= low and root . data <= high NEW_LINE DEDENT def getCountUtil ( root , low , high , count ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return True NEW_LINE DEDENT l = getCountUtil ( root . left , low , high , count ) NEW_LINE r = getCountUtil ( root . right , low , high , count ) NEW_LINE if l and r and inRange ( root , low , high ) : NEW_LINE INDENT count [ 0 ] += 1 NEW_LINE return True NEW_LINE DEDENT return False NEW_LINE DEDENT def getCount ( root , low , high ) : NEW_LINE INDENT count = [ 0 ] NEW_LINE getCountUtil ( root , low , high , count ) NEW_LINE return count NEW_LINE DEDENT 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 if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 5 ) NEW_LINE root . right = newNode ( 50 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . right . left = newNode ( 40 ) NEW_LINE root . right . right = newNode ( 100 ) NEW_LINE l = 5 NEW_LINE h = 45 NEW_LINE print ( " Count ▁ of ▁ subtrees ▁ in ▁ [ " , l , " , ▁ " , h , " ] ▁ is ▁ " , getCount ( root , l , h ) ) NEW_LINE DEDENT
Remove all leaf nodes from the binary search tree | Python 3 program to delete leaf Node from binary search tree . Create a newNode in binary search tree . ; Constructor to create a new node ; Insert a Node in binary search tree . ; Function for inorder traversal in a BST . ; Delete leaf nodes from binary search tree . ; Else recursively delete in left and right subtrees . ; Driver code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def insert ( root , data ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return newNode ( data ) NEW_LINE DEDENT if data < root . data : NEW_LINE INDENT root . left = insert ( root . left , data ) NEW_LINE DEDENT elif data > root . data : NEW_LINE INDENT root . right = insert ( root . right , data ) 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 def leafDelete ( 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 None NEW_LINE DEDENT root . left = leafDelete ( root . left ) NEW_LINE root . right = leafDelete ( root . right ) NEW_LINE return root NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 20 ) NEW_LINE insert ( root , 10 ) NEW_LINE insert ( root , 5 ) NEW_LINE insert ( root , 15 ) NEW_LINE insert ( root , 30 ) NEW_LINE insert ( root , 25 ) NEW_LINE insert ( root , 35 ) NEW_LINE print ( " Inorder ▁ before ▁ Deleting ▁ the ▁ leaf ▁ Node . " ) NEW_LINE inorder ( root ) NEW_LINE leafDelete ( root ) NEW_LINE print ( ) NEW_LINE print ( " INorder ▁ after ▁ Deleting ▁ the ▁ leaf ▁ Node . " ) NEW_LINE inorder ( root ) NEW_LINE DEDENT
Sum of k smallest elements in BST | Python3 program to find Sum Of All Elements smaller than or equal to Kth Smallest Element In BST ; utility that allocates a newNode with the given key ; A utility function to insert a new Node with given key in BST and also maintain lcount , Sum ; If the tree is empty , return a new Node ; Otherwise , recur down the tree ; return the ( unchanged ) Node pointer ; function return sum of all element smaller than and equal to Kth smallest element ; Base cases ; Compute sum of elements in left subtree ; Add root 's data ; Add current Node ; If count is less than k , return right subtree Nodes ; Wrapper over ksmallestElementSumRec ( ) ; Driver Code ; 20 / \ 8 22 / \ 4 12 / \ 10 14
INT_MAX = 2147483647 NEW_LINE class createNode : 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 ( root , key ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return createNode ( key ) NEW_LINE DEDENT if ( root . data > key ) : NEW_LINE INDENT root . left = insert ( root . left , key ) NEW_LINE DEDENT elif ( root . data < key ) : NEW_LINE INDENT root . right = insert ( root . right , key ) NEW_LINE DEDENT return root NEW_LINE DEDENT def ksmallestElementSumRec ( root , k , count ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( count [ 0 ] > k [ 0 ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = ksmallestElementSumRec ( root . left , k , count ) NEW_LINE if ( count [ 0 ] >= k [ 0 ] ) : NEW_LINE INDENT return res NEW_LINE DEDENT res += root . data NEW_LINE count [ 0 ] += 1 NEW_LINE if ( count [ 0 ] >= k [ 0 ] ) : NEW_LINE INDENT return res NEW_LINE DEDENT return res + ksmallestElementSumRec ( root . right , k , count ) NEW_LINE DEDENT def ksmallestElementSum ( root , k ) : NEW_LINE INDENT count = [ 0 ] NEW_LINE return ksmallestElementSumRec ( root , k , count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 20 ) NEW_LINE root = insert ( root , 8 ) NEW_LINE root = insert ( root , 4 ) NEW_LINE root = insert ( root , 12 ) NEW_LINE root = insert ( root , 10 ) NEW_LINE root = insert ( root , 14 ) NEW_LINE root = insert ( root , 22 ) NEW_LINE k = [ 3 ] NEW_LINE print ( ksmallestElementSum ( root , k ) ) NEW_LINE DEDENT
Sum of k smallest elements in BST | utility function new Node of BST ; A utility function to insert a new Node with given key in BST and also maintain lcount , Sum ; If the tree is empty , return a new Node ; Otherwise , recur down the tree ; increment lCount of current Node ; increment current Node sum by adding key into it ; return the ( unchanged ) Node pointer ; function return sum of all element smaller than and equal to Kth smallest element ; if we fount k smallest element then break the function ; store sum of all element smaller than current root ; ; decremented k and call right sub - tree ; call left sub - tree ; Wrapper over ksmallestElementSumRec ( ) ; Driver Code ; 20 / \ 8 22 / \ 4 12 / \ 10 14
class createNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . lCount = 0 NEW_LINE self . Sum = 0 NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def insert ( root , key ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return createNode ( key ) NEW_LINE DEDENT if root . data > key : NEW_LINE INDENT root . lCount += 1 NEW_LINE root . Sum += key NEW_LINE root . left = insert ( root . left , key ) NEW_LINE DEDENT elif root . data < key : NEW_LINE INDENT root . right = insert ( root . right , key ) NEW_LINE DEDENT return root NEW_LINE DEDENT def ksmallestElementSumRec ( root , k , temp_sum ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . lCount + 1 ) == k : NEW_LINE INDENT temp_sum [ 0 ] += root . data + root . Sum NEW_LINE return NEW_LINE DEDENT elif k > root . lCount : NEW_LINE INDENT temp_sum [ 0 ] += root . data + root . Sum NEW_LINE k = k - ( root . lCount + 1 ) NEW_LINE ksmallestElementSumRec ( root . right , k , temp_sum ) NEW_LINE DEDENT else : NEW_LINE INDENT ksmallestElementSumRec ( root . left , k , temp_sum ) NEW_LINE DEDENT DEDENT def ksmallestElementSum ( root , k ) : NEW_LINE INDENT Sum = [ 0 ] NEW_LINE ksmallestElementSumRec ( root , k , Sum ) NEW_LINE return Sum [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 20 ) NEW_LINE root = insert ( root , 8 ) NEW_LINE root = insert ( root , 4 ) NEW_LINE root = insert ( root , 12 ) NEW_LINE root = insert ( root , 10 ) NEW_LINE root = insert ( root , 14 ) NEW_LINE root = insert ( root , 22 ) NEW_LINE k = 3 NEW_LINE print ( ksmallestElementSum ( root , k ) ) NEW_LINE DEDENT
Inorder Successor in Binary Search Tree | A binary tree node ; Given 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 avoidusing reference parameters ) ; 1 ) If tree is empty then return a new singly node ; 2 ) Otherwise , recur down the tree ; return the unchanged node pointer ; Step 1 of the above algorithm ; Step 2 of the above algorithm ; Given a non - empty binary search tree , return the minimum data value found in that tree . Note that the entire tree doesn 't need to be searched ; loop down to find the leftmost leaf ; Driver progam to test above function
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 temp = insert ( node . left , data ) NEW_LINE node . left = temp NEW_LINE temp . parent = node NEW_LINE DEDENT else : NEW_LINE INDENT temp = insert ( node . right , data ) NEW_LINE node . right = temp NEW_LINE temp . parent = node NEW_LINE DEDENT return node NEW_LINE DEDENT DEDENT def inOrderSuccessor ( n ) : NEW_LINE INDENT if n . right is not None : NEW_LINE INDENT return minValue ( n . right ) NEW_LINE DEDENT p = n . parent NEW_LINE while ( p is not None ) : NEW_LINE INDENT if n != p . right : NEW_LINE INDENT break NEW_LINE DEDENT n = p NEW_LINE p = p . parent NEW_LINE DEDENT return p NEW_LINE DEDENT def minValue ( node ) : NEW_LINE INDENT current = node NEW_LINE while ( current is not None ) : NEW_LINE INDENT if current . left is None : NEW_LINE INDENT break NEW_LINE DEDENT current = current . left NEW_LINE DEDENT return current NEW_LINE DEDENT root = None NEW_LINE root = insert ( root , 20 ) NEW_LINE root = insert ( root , 8 ) ; NEW_LINE root = insert ( root , 22 ) ; NEW_LINE root = insert ( root , 4 ) ; NEW_LINE root = insert ( root , 12 ) ; NEW_LINE root = insert ( root , 10 ) ; NEW_LINE root = insert ( root , 14 ) ; NEW_LINE temp = root . left . right . right NEW_LINE succ = inOrderSuccessor ( root , temp ) NEW_LINE if succ is not None : NEW_LINE INDENT print " \n Inorder ▁ Successor ▁ of ▁ % ▁ d ▁ is ▁ % ▁ d ▁ " % ( temp . data , succ . data ) NEW_LINE DEDENT else : NEW_LINE INDENT print " \n Inorder ▁ Successor ▁ doesn ' t ▁ exist " NEW_LINE DEDENT
Inorder predecessor and successor for a given key in BST | A Binary Tree Node Utility function to create a new tree node ; since inorder traversal results in ascendingorder visit to node , we can store the values of the largest o which is smaller than a ( predecessor ) and smallest no which is large than a ( successor ) using inorder traversal ; If root is None return ; traverse the left subtree ; root data is greater than a ; q stores the node whose data is greater than a and is smaller than the previously stored data in * q which is successor ; if the root data is smaller than store it in p which is predecessor ; traverse the right subtree ; Driver Code
class getnode : 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 find_p_s ( root , a , p , q ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT find_p_s ( root . left , a , p , q ) NEW_LINE if ( root and root . data > a ) : NEW_LINE INDENT if ( ( not q [ 0 ] ) or q [ 0 ] and q [ 0 ] . data > root . data ) : NEW_LINE INDENT q [ 0 ] = root NEW_LINE DEDENT DEDENT elif ( root and root . data < a ) : NEW_LINE INDENT p [ 0 ] = root NEW_LINE DEDENT find_p_s ( root . right , a , p , q ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = getnode ( 50 ) NEW_LINE root1 . left = getnode ( 20 ) NEW_LINE root1 . right = getnode ( 60 ) NEW_LINE root1 . left . left = getnode ( 10 ) NEW_LINE root1 . left . right = getnode ( 30 ) NEW_LINE root1 . right . left = getnode ( 55 ) NEW_LINE root1 . right . right = getnode ( 70 ) NEW_LINE p = [ None ] NEW_LINE q = [ None ] NEW_LINE find_p_s ( root1 , 55 , p , q ) NEW_LINE if ( p [ 0 ] ) : NEW_LINE INDENT print ( p [ 0 ] . data , end = " " ) NEW_LINE DEDENT if ( q [ 0 ] ) : NEW_LINE INDENT print ( " " , q [ 0 ] . data ) NEW_LINE DEDENT DEDENT
Inorder predecessor and successor for a given key in BST | Iterative Approach | Function that finds predecessor and successor of key in BST . ; Search for given key in BST . ; If root is given key . ; the minimum value in right subtree is predecessor . ; the maximum value in left subtree is successor . ; If key is greater than root , then key lies in right subtree . Root could be predecessor if left subtree of key is null . ; If key is smaller than root , then key lies in left subtree . Root could be successor if right subtree of key is null . ; A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; Driver Code ; Key to be searched in BST ; Let us create following BST 50 / \ / \ 30 70 / \ / \ / \ / \ 20 40 60 80
def findPreSuc ( root , pre , suc , key ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return NEW_LINE DEDENT while root != None : NEW_LINE INDENT if root . key == key : NEW_LINE INDENT if root . right : NEW_LINE INDENT suc [ 0 ] = root . right NEW_LINE while suc [ 0 ] . left : NEW_LINE INDENT suc [ 0 ] = suc [ 0 ] . left NEW_LINE DEDENT DEDENT if root . left : NEW_LINE INDENT pre [ 0 ] = root . left NEW_LINE while pre [ 0 ] . right : NEW_LINE INDENT pre [ 0 ] = pre [ 0 ] . right NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT elif root . key < key : NEW_LINE INDENT pre [ 0 ] = root NEW_LINE root = root . right NEW_LINE DEDENT else : NEW_LINE INDENT suc [ 0 ] = root NEW_LINE root = root . left NEW_LINE DEDENT DEDENT DEDENT 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 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 DEDENT else : NEW_LINE INDENT node . right = insert ( node . right , key ) NEW_LINE DEDENT return node NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT key = 65 NEW_LINE 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 pre , suc = [ None ] , [ None ] NEW_LINE findPreSuc ( root , pre , suc , key ) NEW_LINE if pre [ 0 ] != None : NEW_LINE INDENT print ( " Predecessor ▁ is " , pre [ 0 ] . key ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT if suc [ 0 ] != None : NEW_LINE INDENT print ( " Successor ▁ is " , suc [ 0 ] . key ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT
Find a pair with given sum in BST | Python3 program to find a pair with given sum using hashing ; Driver code
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 insert ( root , data ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return Node ( data ) NEW_LINE DEDENT if ( data < root . data ) : NEW_LINE INDENT root . left = insert ( root . left , data ) NEW_LINE DEDENT if ( data > root . data ) : NEW_LINE INDENT root . right = insert ( root . right , data ) NEW_LINE DEDENT return root NEW_LINE DEDENT def findPairUtil ( root , summ , unsorted_set ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return False NEW_LINE DEDENT if findPairUtil ( root . left , summ , unsorted_set ) : NEW_LINE INDENT return True NEW_LINE DEDENT if unsorted_set and ( summ - root . data ) in unsorted_set : NEW_LINE INDENT print ( " Pair ▁ is ▁ Found ▁ ( { } , { } ) " . format ( summ - root . data , root . data ) ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT unsorted_set . add ( root . data ) NEW_LINE DEDENT return findPairUtil ( root . right , summ , unsorted_set ) NEW_LINE DEDENT def findPair ( root , summ ) : NEW_LINE INDENT unsorted_set = set ( ) NEW_LINE if ( not findPairUtil ( root , summ , unsorted_set ) ) : NEW_LINE INDENT print ( " Pair ▁ do ▁ not ▁ exist ! " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 15 ) NEW_LINE root = insert ( root , 10 ) NEW_LINE root = insert ( root , 20 ) NEW_LINE root = insert ( root , 8 ) NEW_LINE root = insert ( root , 12 ) NEW_LINE root = insert ( root , 16 ) NEW_LINE root = insert ( root , 25 ) NEW_LINE root = insert ( root , 10 ) NEW_LINE summ = 33 NEW_LINE findPair ( root , summ ) NEW_LINE DEDENT