def preorder_indent(tree: Tree, position: Tree.Position, depth: int): """ Print preorder representation of subtree of T rooted at p at depth depth :param tree: tree :param position: root :param depth: depth """ print(2 * depth * " ", str(position.element())) for c in tree.children(position): preorder_indent(tree, c, depth + 1)
def disk_space(tree: Tree, position: Tree.Position): """ :param tree: tree :param position: position :return: the total disk space for subtree of tree rooted at position """ total = 0 if tree.is_leaf(position) and isinstance(position.element(), File): total = position.element().size for c in tree.children(position): total += disk_space(tree, c) return total
def preorder_label(tree: Tree, position: Tree.Position, depth: int, path: [int]): """ Print preorder representation of subtree of T rooted at p at depth depth :param tree: tree :param position: root :param depth: depth :param path: a list of ints to represent the path of the position """ label = ".".join([str(j+1) for j in path]) print(2 * depth * " ", label, str(position.element())) path.append(0) for c in tree.children(position): preorder_label(tree, c, depth + 1, path) path[-1] += 1 path.pop()
def parenthesis(tree: Tree, position: Tree.Position): """ Print parenthesized representation of tree rooted at the position :param tree: tree :param position: position :return: """ print(position.element(), end="") if not tree.is_leaf(position): first_time = Tree for c in tree.children(position): if first_time: print("(", end="") else: print(",", end=" ") first_time = False parenthesis(tree, c) print(")", end="")