Program to find out the inheritance order in a family in Python


Suppose, there is a family that consists of members from different generations. Such as the family has a father, his children, and their grandmother. But births and deaths happen in each family.

The eldest member of the family is considered the head. So, when the 'head' member dies, their direct successor or their children becomes the head. We implement three functions, the first one is used when a child is born into the family. The function takes the parent's name and child's name as input and adds them to the record.

The second function is used when there is a death. It takes the name of the deceased family member as input and removes them from the record.

The third function gives the order of inheritance. The current order of inheritance is printed whenever it is called.

So, for a set of inputs; we have to find out the order of inheritance. So, if the order of input is like birth, birth, birth, birth, birth, death, inheritance, death, inheritance, then the output will be ['Zach', 'Jesse', 'Ursula', 'Ryan', 'Thea'] ['Jesse', 'Ursula', 'Ryan', 'Thea']

At first the head of the family is Paul.

Then Paul had children named Zach and Jesse respectively.

Jesse then had three children; Ursula, Ryan, and Thea, Ursula being the eldest and Thea being the youngest.

Then Paul dies. The order of inheritance is ['Zach', 'Jesse', 'Ursula', 'Ryan', 'Thea'].

Then Zach dies, the order of inheritance becomes ['Jesse', 'Ursula', 'Ryan', 'Thea'].

To solve this, we will follow these steps −

  • family := a new map that contains lists as values

  • head := the current head of the family

  • dead := a set

  • Define a function birth() . This will take p_name, c_name

    • insert c_name at the end of family[p_name]

  • Define a function death() . This will take name

    • add(name) to the set dead

  • Define a function inheritance() . This will take

    • ans := a new list

    • depth_search(head)

    • return ans

  • Define a function depth_search() . This will take current

    • if current is not in dead, then

      • insert current at the end of ans

    • for each child in family[current], do

      • depth_search(child)

Example

Let us see the following implementation to get better understanding

from collections import defaultdict
class Solution:

   def __init__(self, head_name):
      self.family = defaultdict(list)
      self.head = head_name
      self.dead = set()

   def birth(self, p_name, c_name):
      self.family[p_name].append(c_name)

   def death(self, name):
      self.dead.add(name)

   def inheritance(self):
      self.ans = []
      self.depth_search(self.head)
      return self.ans

   def depth_search(self, current):
      if current not in self.dead:
         self.ans.append(current)
      for child in self.family[current]:
         self.depth_search(child)

ob = Solution('Paul')
ob.birth('Paul', 'Zach')
ob.birth('Paul', 'Jesse')
ob.birth('Jesse', 'Ursula')
ob.birth('Jesse', 'Ryan')
ob.birth('Jesse', 'Thea')
ob.death('Paul')
print(ob.inheritance())
ob.death('Zach')
print(ob.inheritance())

Input

ob = Solution('Paul')
ob.birth('Paul', 'Zach')
ob.birth('Paul', 'Jesse')
ob.birth('Jesse', 'Ursula')
ob.birth('Jesse', 'Ryan')
ob.birth('Jesse', 'Thea')
ob.death('Paul')
print(ob.inheritance())
ob.death('Zach')
print(ob.inheritance())

Output

['Zach', 'Jesse', 'Ursula', 'Ryan', 'Thea']
['Jesse', 'Ursula', 'Ryan', 'Thea']

Updated on: 06-Oct-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements