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) Call the root of the tree 'root'. Then print out str(root). DO NOT MODIFY THE Node CLASS.
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) Call the root of the tree 'root'. Then print out str(root). DO NOT MODIFY THE Node CLASS.
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 | # Define the Node class 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)+')' # Create the tree and print it def main(): # Create the tree: (1 (2 3) 4) # Create the root node root = Node('1') # Create the children of the root node child1 = Node('2', root) child2 = Node('3', root) child3 = Node('4', root) # Print the tree print(str(root)) if __name__ == "__main__": main() |
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 | # Define the Node class 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)+')' # Create the tree and print it def main(): # Create the tree: (1 (2 3) 4) # Create the root node root = Node('1') # Create the intermediate node intermediate = Node('intermediate', root) # Create the children of the intermediate node child1 = Node('2', intermediate) child2 = Node('3', intermediate) # Create the child of the root node child3 = Node('4', root) # Rename the intermediate node to '' intermediate.name = '' # Print the tree print(str(root)) if __name__ == "__main__": main() |
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 | # Define the Node class 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)+')' # Create the tree and print it def main(): # Create the tree: (1 (2 3) 4) # Create the root node root = Node('1') # Create the intermediate node intermediate = Node('intermediate', root) # Create the children of the intermediate node child1 = Node('2', intermediate) child2 = Node('3', intermediate) # Create the child of the root node child3 = Node('4', root) # Rename the intermediate node to '' intermediate.name = '' # 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 | # Define the Node class 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)+')' # Create the tree and print it def main(): # Create the tree: (1 (2 3) 4) # Create the root node root = Node('1') # Create the intermediate node intermediate = Node('intermediate', root) # Create the children of the intermediate node child1 = Node('2', intermediate) child2 = Node('3', intermediate) # Create the child of the root node child3 = Node('4', root) # Rename the intermediate node to '' intermediate.name = '' # Print the tree print(str(root)) if __name__ == "__main__": main() |
(1 ( 2 3) 4)
Testing if the previous output contains the string (1 (2 3) 4)
: False