class ThroneInheritance:
def __init__(self, kingName: str):
self.king = kingName
self.family = {kingName: []}
self.dead = set()
def birth(self, parentName: str, childName: str) -> None:
if parentName not in self.family:
self.family[parentName] = []
self.family[parentName].append(childName)
self.family[childName] = []
def death(self, name: str) -> None:
self.dead.add(name)
def getInheritanceOrder(self) -> list:
order = []
def dfs(name):
if name not in self.dead:
order.append(name)
for child in self.family.get(name, []):
dfs(child)
dfs(self.king)
return order
class ThroneInheritance {
string king;
unordered_map<string, vector<string>> family;
unordered_set<string> dead;
public:
ThroneInheritance(string kingName) {
king = kingName;
family[kingName] = {};
}
void birth(string parentName, string childName) {
family[parentName].push_back(childName);
family[childName] = {};
}
void death(string name) {
dead.insert(name);
}
vector<string> getInheritanceOrder() {
vector<string> order;
function<void(string)> dfs = [&](string name) {
if (!dead.count(name)) order.push_back(name);
for (auto &child : family[name]) dfs(child);
};
dfs(king);
return order;
}
};
class ThroneInheritance {
private String king;
private Map<String, List<String>> family;
private Set<String> dead;
public ThroneInheritance(String kingName) {
king = kingName;
family = new HashMap<>();
family.put(kingName, new ArrayList<>());
dead = new HashSet<>();
}
public void birth(String parentName, String childName) {
family.get(parentName).add(childName);
family.put(childName, new ArrayList<>());
}
public void death(String name) {
dead.add(name);
}
public List<String> getInheritanceOrder() {
List<String> order = new ArrayList<>();
dfs(king, order);
return order;
}
private void dfs(String name, List<String> order) {
if (!dead.contains(name)) order.add(name);
for (String child : family.get(name)) {
dfs(child, order);
}
}
}
var ThroneInheritance = function(kingName) {
this.king = kingName;
this.family = {};
this.family[kingName] = [];
this.dead = new Set();
};
ThroneInheritance.prototype.birth = function(parentName, childName) {
if (!this.family[parentName]) this.family[parentName] = [];
this.family[parentName].push(childName);
this.family[childName] = [];
};
ThroneInheritance.prototype.death = function(name) {
this.dead.add(name);
};
ThroneInheritance.prototype.getInheritanceOrder = function() {
const order = [];
const dfs = (name) => {
if (!this.dead.has(name)) order.push(name);
for (const child of this.family[name] || []) dfs(child);
};
dfs(this.king);
return order;
};
The "Throne Inheritance" problem simulates the succession order in a royal family. You are required to design a system that models the inheritance of the throne, supporting the following operations:
ThroneInheritance(kingName)
: Initializes the inheritance system with the king as the root of the family tree.
birth(parentName, childName)
: Records the birth of a child to the given parent.
death(name)
: Records the death of the given person. They should be skipped in the inheritance order, but their children are still considered.
getInheritanceOrder()
: Returns the current inheritance order, which is the order in which people would inherit the throne, skipping the dead.
The inheritance order is determined by a pre-order traversal of the family tree, starting from the king, skipping anyone who is dead, but always considering their children.
Key constraints:
birth
, death
, and getInheritanceOrder
operations.To solve this problem, we need to model a dynamic family tree that supports quick updates and queries. The main challenge is to maintain the correct order of inheritance as the family grows and as people die.
A brute-force approach might involve reconstructing the entire inheritance order from scratch on every query, but this would be inefficient as the tree grows. Instead, we want a structure that lets us:
This suggests using a tree structure (with the king as the root), and for each person, maintaining a list of children in birth order. We'll also need a way to quickly check if someone is dead.
Let's break down the solution step by step:
Let's walk through an example step by step:
ThroneInheritance("king") birth("king", "andy") birth("king", "bob") birth("king", "catherine") birth("andy", "matthew") birth("bob", "alex") birth("bob", "asha") death("bob") getInheritanceOrder()
getInheritanceOrder()
does a pre-order traversal:getInheritanceOrder()
call. If done inefficiently, it could be worse.birth
and death
are O(1) operations.getInheritanceOrder
is O(N), since we must visit every living person in the family tree once per call.The Throne Inheritance problem is elegantly solved by modeling the family as a tree using hash maps, recording deaths in a set, and using a pre-order traversal to generate the inheritance order. This approach is both efficient and clear, supporting fast updates and queries while accurately reflecting the rules of succession. The use of standard data structures makes the solution robust and easy to understand, and the pre-order traversal naturally matches the inheritance logic required by the problem.