class Data_Structures:
    class Node:
        def __init__(self, data, lvl, pos, left=None, right=None):
            self.data, self.level, self.pos, self.left, self.right = data, lvl, pos, left, right

    class Binary_Tree:
        def __init__(self):
            self.root = None

        def write_tree_to_file(self):
            root = self.root
            def write_rec(node, path):
                if (node != None):
                    if node == root:
                        res = node.data + ":T:1:510:100\n"

                    res, l, r = '', write_rec(node.left, path + 'L'), write_rec(node.right, path + 'R')
                    if (node.data != None): res += (node.data + ":" + path + ":" + str(node.level) + ":" + str(node.pos[0]) + ":" + str(node.pos[1]) + "\n")
                    if (l != None): res += l
                    if (r != None): res += r
                    return res

            result = write_rec(self.root, '')
            return result

        def add_node(self, string):
            if (self.root == None): self.root = Data_Structures.Node(None, None, None)

            data = string.split(":")
            # print(data)
            if (data[1] == 'T'):
                self.root.data = data[0]
                self.root.level = int(data[2])
                self.root.pos = (int(data[3]), int(data[4]))
                return self.root

            destination = self.root

            for command in data[1]:
                if (command in 'lL'):
                    if destination.left is None:
                        destination.left = Data_Structures.Node(data[0], int(data[2]), (int(data[3]), int(data[4])))

                    destination = destination.left
                elif (command in 'rR'):
                    if destination.right is None:
                        destination.right = Data_Structures.Node(data[0], int(data[2]), (int(data[3]), int(data[4])))

                    destination = destination.right

            return self.root

        def create_tree(self, nodes):
            tree = None
            for line in nodes:
                tree = self.add_node(tree, line)

            return tree