Python3 program to find a pair with given sum using hashing
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