Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

BINARY TREE

from queue import Queue def showtree(self, root, treedepth):

        print(

            'THE BINARY TREE CREATED IS SHOWN BELOW')

class Node:         q = Queue(maxsize=2**0)

    def __init__(self, data=None):         q.put(root)

        self.data = data         d = BinaryTree.temp

        self.left = None         d.append(q.get())

        self.right = None         for k in list(range(1, treedepth+2)):

            q = Queue(maxsize=2**k)

            for j in d:

class BinaryTree:                 q.put(j.left)

    PreOL = []  # This list stores elements of preorder traversal                 q.put(j.right)

    InOL = []   # This list stores elements of inorder traversal             for item in list(map(lambda x: x.data, d)):

    PostOL = []  # This list stores elements of postorder traversal                 BinaryTree.LOL.append(item)

    LOL = []  # This list stores elements of level order traversal             print('  '*(k), *list(map(lambda x: x.data, d)),

    temp = []   # A temporary list.                   sep=' '*((2**(2*treedepth-k))-1))

            d.clear()

    def __init__(self, root):             for i in range(2**k):

        self.root = Node(root)                 d.append(q.get())

    def givebirth(self, parent, leftchild, rightchild):     def print_levelorder_traversal(self):

        parent.left = Node(leftchild)         print('Level order Traversal : ', *bt.LOL)

        parent.right = Node(rightchild)

# -------HIERARCHY COMMANDS SECTION--------------------------

    def preorder(self, root): # It is mandatory to declare all children at any level 
        if root is None: # upto the alloted treedepth.
            return # If the child is missing just declare None.
        BinaryTree.PreOL.append(root.data) # Children of None parent will all be None.
        self.preorder(root.left) # Syntax of givebirth is object.givebirth(parent, leftchild, rightchild).
        self.preorder(root.right) # showtree method displays best results when the children 

# are of single digit/letter 
    def print_preorder_traversal(self): # Upgraded version of showtree is coming soon which 
        self.preorder(self.root) # will address this shortcoming.
        print('Pre order Traversal : ', *bt.PreOL)

bt = BinaryTree('A')  # Here A is the root.

    def inorder(self, root): bt.givebirth(bt.root, 'B', 'C')  # B and C are children of A.

        if root is None: bt.givebirth(bt.root.left, 'D', 'E')    # D and E are children of B.

            return bt.givebirth(bt.root.right, 'F', 'G')   # F and G are children of C.

        self.inorder(root.left) bt.givebirth(bt.root.left.left, 'H', 'I')  # H and I are children of D.

        BinaryTree.InOL.append(root.data) bt.givebirth(bt.root.left.right, 'J', 'K')  # J and K are children of E.

        self.inorder(root.right) bt.givebirth(bt.root.right.left, 'L', 'M')  # L and M are children of F.

bt.givebirth(bt.root.right.right, 'N', 'O')  # N and O are chldren of G.

    def print_inorder_traversal(self):

        self.inorder(self.root) # ---------DISPLAY COMMANDS SECTION-------------------------

        print('In order Traversal : ', *bt.InOL) # showtree method will print the entire tree upto the alloted treedepth.
bt.showtree(root=bt.root, treedepth=3)

    def postorder(self, root): bt.print_preorder_traversal()

        if root is None: bt.print_inorder_traversal()

            return bt.print_postorder_traversal()

        self.postorder(root.left) bt.print_levelorder_traversal()

        self.postorder(root.right)

        BinaryTree.PostOL.append(root.data) OUTPUT:

    def print_postorder_traversal(self):
THE BINARY TREE CREATED IS SHOWN BELOW
        self.postorder(self.root)
A
        print('Post order Traversal : ', *bt.PostOL)
B C
D E F G
    
H I J K L M N O
Pre order Traversal : A B D H I E J K C F L M G N O
In order Traversal : H D I B J E K A L F M C N G O
Post order Traversal : H I D J K E B L M F N O G C A
Level order Traversal : A B C D E F G H I J K L M N O
BINARY TREE

You might also like