Print Binary Tree in 2 | Python3 Program to print binary tree in 2D ; Binary Tree Node ; Constructor that allocates a new node with the given data and null left and right pointers . ; Function to print binary tree in 2D It does reverse inorder traversal ; Base case ; Increase distance between levels ; Process right child first ; Print current node after space count ; Process left child ; Wrapper over print2DUtil ( ) ; space = [ 0 ] Pass initial space count as 0 ; Driver Code
COUNT = [ 10 ] NEW_LINE class newNode : 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 print2DUtil ( root , space ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT space += COUNT [ 0 ] NEW_LINE print2DUtil ( root . right , space ) NEW_LINE print ( ) NEW_LINE for i in range ( COUNT [ 0 ] , space ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT print ( root . data ) NEW_LINE print2DUtil ( root . left , space ) NEW_LINE DEDENT def print2D ( root ) : NEW_LINE INDENT print2DUtil ( root , 0 ) NEW_LINE 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 root . left . left . left = newNode ( 8 ) NEW_LINE root . left . left . right = newNode ( 9 ) NEW_LINE root . left . right . left = newNode ( 10 ) NEW_LINE root . left . right . right = newNode ( 11 ) NEW_LINE root . right . left . left = newNode ( 12 ) NEW_LINE root . right . left . right = newNode ( 13 ) NEW_LINE root . right . right . left = newNode ( 14 ) NEW_LINE root . right . right . right = newNode ( 15 ) NEW_LINE print2D ( root ) NEW_LINE DEDENT