<jupyter_start><jupyter_text>Binary Tree<jupyter_code>class Node:
    def __init__(self, data):
        self.data = data
        self.right = None
        self.left = None
class Queue:
    def __init__(self):
        self.items = []
        
    def enqueue(self, item):
        self.items.insert(0, item)
        
    def dequeue(self):
        if not self.isEmpty() :
            return self.items.pop()
        
    def isEmpty(self):
        return len(self.items) == 0
    
    def peek(self):
        if not self.isEmpty():
            return self.items[-1]
    
    def __len__(self):
        return self.size()
    
    def size(self):
        return len(self.items)
class  BinaryTree:
    def __init__(self, root):
        self.root = Node(root)  
        
    def printTree(self, traversal_type):
        if traversal_type == 'preorder':
            return self.preorderRecursive(tree.root, 'Preorder Tree Traversal  : ')
        elif traversal_type == 'inorder':
            return self.inorderRecursive(tree.root, 'Inorder Tree Traversal   : ')
        elif traversal_type == 'postorder':
            return self.postorderRecursive(tree.root, 'Postorder Tree Traversal : ')
        elif traversal_type == 'levelorder':
            return self.levelOrder(tree.root)
        else :
            print(traversal_type + 'is not supported')
            return False
        
    def preorderRecursive(self, start, result):
        # Root --> Left --> Right
        if start :
            result += (str(start.data) + '-')
            result = self.preorderRecursive(start.left, result)
            result = self.preorderRecursive(start.right, result)
        return result
    
    def inorderRecursive(self, start, result):
        # Left --> Root --> Right
        if start :
            result = self.inorderRecursive(start.left, result)
            result += (str(start.data)+'-')
            result = self.inorderRecursive(start.right, result)
        return result
    
    def postorderRecursive(self, start, result):
        # Left --> Right --> Root
        if start :
            result = self.postorderRecursive(start.left, result)
            result = self.postorderRecursive(start.right, result)
            result += (str(start.data)+'-')
        return result
    
    def levelOrder(self, start):
        if start is None:
            return
        
        queue = Queue()
        queue.enqueue(start)
        
        traversal = ''
        while len(queue)>0:
            traversal += str(queue.peek().data)+'-'
            node = queue.dequeue()
            
            if node.left :
                queue.enqueue(node.left)
            if node.right :
                queue.enqueue(node.right)
                
        return traversal
    
    def topView(self, start):
        if start is None:
            return
        
        l = []
        l.append(start)
        while len(l)>0:
            if len(l) == 1:
                print(l[0])
                
            
            
# Set up tree :
tree = BinaryTree(1)
tree.root.left = Node(2)
tree.root.right = Node(3)
tree.root.left.left = Node(4)
tree.root.left.right = Node(5)
tree.root.right.left = Node(6)
tree.root.right.right = Node(7)<jupyter_output><empty_output><jupyter_text>root              1          /     \         2       3       /  \     /  \      4    5   6    7<jupyter_code>print(tree.printTree('preorder'))
print(tree.printTree('inorder'))
print(tree.printTree('postorder'))
print(tree.printTree('levelorder'))
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.enqueue(4)
q.enqueue(5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11696551724137931, 'fraction_numerical': 0.007172413793103448, 'mean_word_length': 1.744890234670704, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '59759', 'license': 'MIT', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1096, 'n_words': 228, 'path': 'Tree/BinaryTree.ipynb', 'repo_name': 'goutamdadhich/DSA_Python', 'size': '3625'}