• + 1 comment

    Python template

    import sys
    
    lines = []
    
    for line in sys.stdin:
        if 'Exit' == line.rstrip():
            break
        lines.append(line.split('\n')[0])
    
    nol = lines[0]
    treeInput = [int(x) for x in lines[1].split(' ')]
    toIns = lines[2]
            
    # print(nol)
    # print(treeInput)
    # print(toIns)
    
    class Node:
        def __init__(self, data):
            self.data = data
            self.left = None
            self.right = None
            self.height = 1  # Initially, new nodes have height 1
    
            
    class AVLTree:
        def __init__(self):
            self.root = None
    
        # Insertion operation - Add your code here
        def insert(self, data):    
        
        # Traversals
        def inorder(self, node):
            if node is not None:
                self.inorder(node.left)
                print(f"{node.data}(BF={self.getBalance(node)})", end=" ")
                self.inorder(node.right)
        
        def postorder(self, node):
            if node is not None:
                print(f"{node.data}(BF={self.getBalance(node)})", end=" ")
                self.postorder(node.left)
                self.postorder(node.right)
                
        # Function to print BF at each node
        def print_balance(self):
            self.inorder(self.root)
            print("")
            self.postorder(self.root)
    
    tree = AVLTree()
    
    for x in treeInput:
        tree.insert(x)
    tree.insert(int(toIns))
    tree.print_balance()