class Node: def __init__(self, name, parent=None): self.name = name self.parent = parent self.children = [] if parent: self.parent.children.append(self) def __repr__(self): if self.children == []: return self.name return '('+self.name + ' ' + ' '.join(repr(c) for c in self.children)+')' Write a complete python file that will print the tree (1 (2 (3 4 5 6) (7 8)) (9 10 (11 12))) Call the root of the tree 'root'. Then print out str(root).
class Node: def __init__(self, name, parent=None): self.name = name self.parent = parent self.children = [] if parent: self.parent.children.append(self) def __repr__(self): if self.children == []: return self.name return '('+self.name + ' ' + ' '.join(repr(c) for c in self.children)+')' Write a complete python file that will print the tree (1 (2 (3 4 5 6) (7 8)) (9 10 (11 12))) Call the root of the tree 'root'. Then print out str(root).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | # tree.py class Node: def __init__(self, name, parent=None): """ Initialize a Node with a name and optional parent. If a parent is provided, the Node is automatically added to the parent's children. """ self.name = name self.parent = parent self.children = [] if parent: self.parent.children.append(self) def __repr__(self): """ Return a string representation of the Node and its children. If the Node has no children, return its name. Otherwise, return a string in the format '(name child1 child2 ...)'. """ if self.children == []: return self.name return '(' + self.name + ' ' + ' '.join(repr(c) for c in self.children) + ')' def main(): # Create the tree root = Node('1') # Level 2 node_2 = Node('2', root) node_9 = Node('9', root) node_10 = Node('10', root) # Level 3 node_3 = Node('3', node_2) node_4 = Node('4', node_2) node_5 = Node('5', node_2) node_6 = Node('6', node_2) node_7 = Node('7', node_2) node_8 = Node('8', node_2) node_11 = Node('11', node_10) node_12 = Node('12', node_10) # Print the tree print(str(root)) if __name__ == "__main__": main() |
1 | (1 (2 (3 4 5 6) (7 8)) (9 10 (11 12))) |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | # tree.py class Node: def __init__(self, name, parent=None): """ Initialize a Node with a name and optional parent. If a parent is provided, the Node is automatically added to the parent's children. """ self.name = name self.parent = parent self.children = [] if parent: self.parent.children.append(self) def __repr__(self): """ Return a string representation of the Node and its children. If the Node has no children, return its name. Otherwise, return a string in the format '(name child1 child2 ...)'. """ if self.children == []: return self.name return '(' + self.name + ' ' + ' '.join(repr(c) for c in self.children) + ')' def main(): # Create the tree root = Node('1') # Level 2 node_2 = Node('2', root) node_9 = Node('9', root) node_10 = Node('10', root) # Level 3 node_3 = Node('3', node_2) node_4 = Node('4', node_2) node_5 = Node('5', node_2) node_6 = Node('6', node_2) node_7 = Node('7', node_2) node_8 = Node('8', node_2) node_11 = Node('11', node_10) node_12 = Node('12', node_10) # Print the tree print(str(root)) if __name__ == "__main__": main() |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | # tree.py class Node: def __init__(self, name, parent=None): """ Initialize a Node with a name and optional parent. If a parent is provided, the Node is automatically added to the parent's children. """ self.name = name self.parent = parent self.children = [] if parent: self.parent.children.append(self) def __repr__(self): """ Return a string representation of the Node and its children. If the Node has no children, return its name. Otherwise, return a string in the format '(name child1 child2 ...)'. """ if self.children == []: return self.name return '(' + self.name + ' ' + ' '.join(repr(c) for c in self.children) + ')' def main(): # Create the tree root = Node('1') # Level 2 node_2 = Node('2', root) node_9 = Node('9', root) node_10 = Node('10', root) # Level 3 node_3 = Node('3', node_2) node_4 = Node('4', node_2) node_5 = Node('5', node_2) node_6 = Node('6', node_2) node_7 = Node('7', node_2) node_8 = Node('8', node_2) node_11 = Node('11', node_10) node_12 = Node('12', node_10) # Print the tree print(str(root)) if __name__ == "__main__": main() |
(1 (2 3 4 5 6 7 8) 9 (10 11 12))
Testing if the previous output contains the string (1 (2 (3 4 5 6) (7 8)) (9 10 (11 12)))
: False