Insertion in a Binary Tree in level order | A binary tree node has key , pointer to left child and a pointer to right child ; Inorder traversal of a binary tree ; function to insert element in binary tree ; Do level order traversal until we find an empty place . ; 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 inorder ( temp ) : NEW_LINE INDENT if ( not temp ) : NEW_LINE INDENT return NEW_LINE DEDENT inorder ( temp . left ) NEW_LINE print ( temp . key , end = " ▁ " ) NEW_LINE inorder ( temp . right ) NEW_LINE DEDENT def insert ( temp , key ) : NEW_LINE INDENT if not temp : NEW_LINE INDENT root = newNode ( key ) NEW_LINE return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( temp ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( not temp . left ) : NEW_LINE INDENT temp . left = newNode ( key ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( not temp . right ) : NEW_LINE INDENT temp . right = newNode ( key ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 11 ) NEW_LINE root . left . left = newNode ( 7 ) NEW_LINE root . right = newNode ( 9 ) NEW_LINE root . right . left = newNode ( 15 ) NEW_LINE root . right . right = newNode ( 8 ) NEW_LINE print ( " Inorder ▁ traversal ▁ before ▁ insertion : " , end = " ▁ " ) NEW_LINE inorder ( root ) NEW_LINE key = 12 NEW_LINE insert ( root , key ) NEW_LINE print ( ) NEW_LINE print ( " Inorder ▁ traversal ▁ after ▁ insertion : " , end = " ▁ " ) NEW_LINE inorder ( root ) NEW_LINE DEDENT