Guest User

Untitled

a guest
Dec 10th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. from Node import Node
  2.  
  3.  
  4. class Tree:
  5.  
  6.     def __init__(self):
  7.         self.nodes = []
  8.  
  9.     def add_node(self, line):
  10.         parent_name, child_name = line.split(')')
  11.         child_node = Node(child_name)
  12.         parent_node = self.get_or_create_node_by_name(parent_name)
  13.         child_node.set_parent(parent_node)
  14.         self.nodes.append(child_node)
  15.  
  16.     def get_or_create_node_by_name(self, name):
  17.         for n in self.nodes:
  18.             if n.name == name:
  19.                 return n
  20.         new_node = Node(name)
  21.         self.nodes.append(new_node)
  22.         return new_node
  23.  
  24.     def compute_orbits_per_node(self):
  25.         for n in self.nodes:
  26.             if not n.parent:
  27.                 n.orbits = 0
  28.  
  29.         # all parents are set
  30.         for n in self.nodes:
  31.             n.orbits = n.get_orbits_to_root()
Advertisement
Add Comment
Please, Sign In to add comment