2025-12-27 16:57:55 +0300 MSK
Throne Inheritance
Links
Code
class Node:
def __init__(self, name: str, parent: Optional["Node"] = None) -> None:
self.name: str = name
self.parent: Optional["Node"] = parent
self.children: list[Node] = []
self.dead: bool = False
def __iter__(self) -> Generator["Node", None, None]:
if not self.dead:
yield self
for child in self.children:
yield from child
class ThroneInheritance:
def __init__(self, kingName: str):
self._root: Node = Node(kingName)
self._nodes: dict[str, Node] = {
kingName: self._root
}
def birth(self, parentName: str, childName: str) -> None:
parent = self._nodes[parentName]
child = Node(childName, parent)
parent.children.append(child)
self._nodes[childName] = child
def death(self, name: str) -> None:
node = self._nodes.pop(name)
node.dead = True
def getInheritanceOrder(self) -> List[str]:
return list(node.name for node in self._root)
# Your ThroneInheritance object will be instantiated and called as such:
# obj = ThroneInheritance(kingName)
# obj.birth(parentName,childName)
# obj.death(name)
# param_3 = obj.getInheritanceOrder()