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 | import sys # 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)+')' # Create the tree structure for (1 (2 3) 4) # Create the root node '1' root = Node('1') # Create node '2' as a child of '1' node2 = Node('2', parent=root) # Create node '3' as a child of '2' node3 = Node('3', parent=node2) # Node '3' is a leaf # Create node '4' as a child of '1' node4 = Node('4', parent=root) # Node '4' is a leaf # Print the string representation of the root node print(str(root)) |
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 | import sys # 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)+')' # Create the tree structure for (1 (2 3) 4) # Create the root node '1' root = Node('1') # Create node '2' as a child of '1' node2 = Node('2', parent=root) # Create node '3' as a child of '2' node3 = Node('3', parent=node2) # Node '3' is a leaf # Create node '4' as a child of '1' node4 = Node('4', parent=root) # Node '4' is a leaf # Print the string representation of the root node print(str(root)) |
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 | import sys # 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)+')' # Create the tree structure for (1 (2 3) 4) # Create the root node '1' root = Node('1') # Create node '2' as a child of '1' node2 = Node('2', parent=root) # Create node '3' as a child of '2' node3 = Node('3', parent=node2) # Node '3' is a leaf # Create node '4' as a child of '1' node4 = Node('4', parent=root) # Node '4' is a leaf # Print the string representation of the root node print(str(root)) |
(1 (2 3) 4)
Testing if the previous output contains the string (1 (2 3) 4)
: True