Linked complete binary tree & its creation | For Queue Size ; A tree node ; A queue node ; A utility function to create a new tree node ; A utility function to create a new Queue ; Standard Queue Functions ; A utility function to check if a tree node has both left and right children ; Function to insert a new node in complete binary tree ; Create a new node for given data ; If the tree is empty , initialize the root with new node . ; get the front node of the queue . ; If the left child of this front node doesn t exist , set the left child as the new node ; If the right child of this front node doesn t exist , set the right child as the new node ; If the front node has both the left child and right child , Dequeue ( ) it . ; Enqueue ( ) the new node for later insertions ; Standard level order traversal to test above function ; Driver code
SIZE = 50 NEW_LINE class node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . right = None NEW_LINE self . left = None NEW_LINE DEDENT DEDENT class Queue : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . front = None NEW_LINE self . rear = None NEW_LINE self . size = 0 NEW_LINE self . array = [ ] NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT temp = node ( data ) NEW_LINE return temp NEW_LINE DEDENT def createQueue ( size ) : NEW_LINE INDENT global queue NEW_LINE queue = Queue ( ) ; NEW_LINE queue . front = queue . rear = - 1 ; NEW_LINE queue . size = size ; NEW_LINE queue . array = [ None for i in range ( size ) ] NEW_LINE return queue ; NEW_LINE DEDENT def isEmpty ( queue ) : NEW_LINE INDENT return queue . front == - 1 NEW_LINE DEDENT def isFull ( queue ) : NEW_LINE INDENT return queue . rear == queue . size - 1 ; NEW_LINE DEDENT def hasOnlyOneItem ( queue ) : NEW_LINE INDENT return queue . front == queue . rear ; NEW_LINE DEDENT def Enqueue ( root ) : NEW_LINE INDENT if ( isFull ( queue ) ) : NEW_LINE INDENT return ; NEW_LINE DEDENT queue . rear += 1 NEW_LINE queue . array [ queue . rear ] = root ; NEW_LINE if ( isEmpty ( queue ) ) : NEW_LINE INDENT queue . front += 1 ; NEW_LINE DEDENT DEDENT def Dequeue ( ) : NEW_LINE INDENT if ( isEmpty ( queue ) ) : NEW_LINE INDENT return None ; NEW_LINE DEDENT temp = queue . array [ queue . front ] ; NEW_LINE if ( hasOnlyOneItem ( queue ) ) : NEW_LINE INDENT queue . front = queue . rear = - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT queue . front += 1 NEW_LINE DEDENT return temp ; NEW_LINE DEDENT def getFront ( queue ) : NEW_LINE INDENT return queue . array [ queue . front ] ; NEW_LINE DEDENT def hasBothChild ( temp ) : NEW_LINE INDENT return ( temp and temp . left and temp . right ) ; NEW_LINE DEDENT def insert ( root , data , queue ) : NEW_LINE INDENT temp = newNode ( data ) ; NEW_LINE if not root : NEW_LINE INDENT root = temp ; NEW_LINE DEDENT else : NEW_LINE INDENT front = getFront ( queue ) ; NEW_LINE if ( not front . left ) : NEW_LINE INDENT front . left = temp ; NEW_LINE DEDENT elif ( not front . right ) : NEW_LINE INDENT front . right = temp ; NEW_LINE DEDENT if ( hasBothChild ( front ) ) : NEW_LINE INDENT Dequeue ( ) ; NEW_LINE DEDENT DEDENT Enqueue ( temp ) ; NEW_LINE return root NEW_LINE DEDENT def levelOrder ( root ) : NEW_LINE INDENT queue = createQueue ( SIZE ) ; NEW_LINE Enqueue ( root ) ; NEW_LINE while ( not isEmpty ( queue ) ) : NEW_LINE INDENT temp = Dequeue ( ) ; NEW_LINE print ( temp . data , end = ' ▁ ' ) NEW_LINE if ( temp . left ) : NEW_LINE INDENT Enqueue ( temp . left ) ; NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT Enqueue ( temp . right ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = None NEW_LINE queue = createQueue ( SIZE ) ; NEW_LINE for i in range ( 1 , 13 ) : NEW_LINE INDENT root = insert ( root , i , queue ) ; NEW_LINE DEDENT levelOrder ( root ) ; NEW_LINE DEDENT