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).
#!/usr/bin/env python3 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) + ')' if __name__ == '__main__': # Build the tree corresponding to: # (1 (2 (3 4 5 6) (7 8)) (9 10 (11 12))) # # Explanation of the tree structure: # - The root node "1" has two children: "2" and "9". # - Node "2" has two children: # • "3" with three children "4", "5", and "6" # • "7" with one child "8" # - Node "9" has two children: # • "10" with no children # • "11" with one child "12" root = Node("1") # The root # First subtree under root node2 = Node("2", root) node3 = Node("3", node2) Node("4", node3) Node("5", node3) Node("6", node3) node7 = Node("7", node2) Node("8", node7) # Second subtree under root node9 = Node("9", root) Node("10", node9) node11 = Node("11", node9) Node("12", node11) # Print the tree using __repr__ of the root 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #!/usr/bin/env python3 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) + ')' if __name__ == '__main__': # Build the tree corresponding to: # (1 (2 (3 4 5 6) (7 8)) (9 10 (11 12))) # # Explanation of the tree structure: # - The root node "1" has two children: "2" and "9". # - Node "2" has two children: # • "3" with three children "4", "5", and "6" # • "7" with one child "8" # - Node "9" has two children: # • "10" with no children # • "11" with one child "12" root = Node("1") # The root # First subtree under root node2 = Node("2", root) node3 = Node("3", node2) Node("4", node3) Node("5", node3) Node("6", node3) node7 = Node("7", node2) Node("8", node7) # Second subtree under root node9 = Node("9", root) Node("10", node9) node11 = Node("11", node9) Node("12", node11) # Print the tree using __repr__ of the root 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #!/usr/bin/env python3 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) + ')' if __name__ == '__main__': # Build the tree corresponding to: # (1 (2 (3 4 5 6) (7 8)) (9 10 (11 12))) # # Explanation of the tree structure: # - The root node "1" has two children: "2" and "9". # - Node "2" has two children: # • "3" with three children "4", "5", and "6" # • "7" with one child "8" # - Node "9" has two children: # • "10" with no children # • "11" with one child "12" root = Node("1") # The root # First subtree under root node2 = Node("2", root) node3 = Node("3", node2) Node("4", node3) Node("5", node3) Node("6", node3) node7 = Node("7", node2) Node("8", node7) # Second subtree under root node9 = Node("9", root) Node("10", node9) node11 = Node("11", node9) Node("12", node11) # Print the tree using __repr__ of the root print(str(root)) |
(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)))
: True
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).
#!/usr/bin/env python3 class Node: def __init__(self, name, parent=None): self.name = name self.parent = parent self.children = [] if parent: parent.children.append(self) def __repr__(self): if not self.children: return self.name return '(' + self.name + ' ' + ' '.join(repr(child) for child in self.children) + ')' if __name__ == '__main__': # Build the tree: # Root is "1" root = Node("1") # Children of root "1" node2 = Node("2", root) node9 = Node("9", root) # Children of node2, for subtree (2 (3 4 5 6) (7 8)) node3 = Node("3", node2) node7 = Node("7", node2) # Children of node3 --> (3 4 5 6) Node("4", node3) Node("5", node3) Node("6", node3) # Child of node7 --> (7 8) Node("8", node7) # Children of node9 --> (9 10 (11 12)) Node("10", node9) node11 = Node("11", node9) # Child of node11 --> (11 12) Node("12", node11) # Print the tree string representation. 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #!/usr/bin/env python3 class Node: def __init__(self, name, parent=None): self.name = name self.parent = parent self.children = [] if parent: parent.children.append(self) def __repr__(self): if not self.children: return self.name return '(' + self.name + ' ' + ' '.join(repr(child) for child in self.children) + ')' if __name__ == '__main__': # Build the tree: # Root is "1" root = Node("1") # Children of root "1" node2 = Node("2", root) node9 = Node("9", root) # Children of node2, for subtree (2 (3 4 5 6) (7 8)) node3 = Node("3", node2) node7 = Node("7", node2) # Children of node3 --> (3 4 5 6) Node("4", node3) Node("5", node3) Node("6", node3) # Child of node7 --> (7 8) Node("8", node7) # Children of node9 --> (9 10 (11 12)) Node("10", node9) node11 = Node("11", node9) # Child of node11 --> (11 12) Node("12", node11) # Print the tree string representation. 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #!/usr/bin/env python3 class Node: def __init__(self, name, parent=None): self.name = name self.parent = parent self.children = [] if parent: parent.children.append(self) def __repr__(self): if not self.children: return self.name return '(' + self.name + ' ' + ' '.join(repr(child) for child in self.children) + ')' if __name__ == '__main__': # Build the tree: # Root is "1" root = Node("1") # Children of root "1" node2 = Node("2", root) node9 = Node("9", root) # Children of node2, for subtree (2 (3 4 5 6) (7 8)) node3 = Node("3", node2) node7 = Node("7", node2) # Children of node3 --> (3 4 5 6) Node("4", node3) Node("5", node3) Node("6", node3) # Child of node7 --> (7 8) Node("8", node7) # Children of node9 --> (9 10 (11 12)) Node("10", node9) node11 = Node("11", node9) # Child of node11 --> (11 12) Node("12", node11) # Print the tree string representation. print(str(root)) |
(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)))
: True