Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find out the inheritance order in a family in Python
In family inheritance systems, we need to track the order of succession when the head of the family dies. This program implements a family inheritance tracker that manages births, deaths, and determines the inheritance order using a depth-first search approach.
Problem Overview
The inheritance follows these rules:
The eldest living member is the head of the family
When the head dies, their direct descendants inherit in order
Children inherit before siblings
The inheritance order follows a depth-first traversal of the family tree
Algorithm Steps
We implement three main functions:
birth(): Adds a child to a parent's record
death(): Marks a family member as deceased
inheritance(): Returns the current inheritance order
Implementation
from collections import defaultdict
class FamilyInheritance:
def __init__(self, head_name):
self.family = defaultdict(list) # Store parent-children relationships
self.head = head_name # Current head of family
self.dead = set() # Track deceased members
def birth(self, parent_name, child_name):
"""Add a child to the family tree"""
self.family[parent_name].append(child_name)
def death(self, name):
"""Mark a family member as deceased"""
self.dead.add(name)
def inheritance(self):
"""Get current inheritance order using DFS"""
self.inheritance_order = []
self._depth_search(self.head)
return self.inheritance_order
def _depth_search(self, current):
"""Depth-first search to build inheritance order"""
# Only living members can inherit
if current not in self.dead:
self.inheritance_order.append(current)
# Process all children recursively
for child in self.family[current]:
self._depth_search(child)
# Example usage
family = FamilyInheritance('Paul')
# Build family tree
family.birth('Paul', 'Zach')
family.birth('Paul', 'Jesse')
family.birth('Jesse', 'Ursula')
family.birth('Jesse', 'Ryan')
family.birth('Jesse', 'Thea')
# Paul dies - show inheritance order
family.death('Paul')
print("After Paul's death:", family.inheritance())
# Zach dies - show updated inheritance order
family.death('Zach')
print("After Zach's death:", family.inheritance())
After Paul's death: ['Zach', 'Jesse', 'Ursula', 'Ryan', 'Thea'] After Zach's death: ['Jesse', 'Ursula', 'Ryan', 'Thea']
How It Works
The algorithm uses a depth-first search to traverse the family tree:
Start from head: Begin traversal from the family head
Check if alive: Only living members are added to inheritance order
Visit children: Recursively process all children before moving to siblings
Build order: Create inheritance list based on tree traversal
Family Tree Visualization
Key Features
Dynamic updates: Inheritance order changes as members die
Depth-first traversal: Children inherit before their parent's siblings
Living members only: Deceased members are excluded from inheritance
Efficient storage: Uses defaultdict to store family relationships
Conclusion
This family inheritance tracker uses depth-first search to maintain proper succession order. The algorithm efficiently handles births, deaths, and inheritance queries by traversing the family tree and excluding deceased members from the inheritance line.
