Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Node import Node
- class Tree:
- def __init__(self):
- self.nodes = []
- def add_node(self, line):
- parent_name, child_name = line.split(')')
- child_node = Node(child_name)
- parent_node = self.get_or_create_node_by_name(parent_name)
- child_node.set_parent(parent_node)
- self.nodes.append(child_node)
- def get_or_create_node_by_name(self, name):
- for n in self.nodes:
- if n.name == name:
- return n
- new_node = Node(name)
- self.nodes.append(new_node)
- return new_node
- def compute_orbits_per_node(self):
- for n in self.nodes:
- if not n.parent:
- n.orbits = 0
- # all parents are set
- for n in self.nodes:
- n.orbits = n.get_orbits_to_root()
Advertisement
Add Comment
Please, Sign In to add comment