Program 1
# Project: Family Tree Builder (Based on BT and Linked list)
# Objective:
# To create a basic system that allows users to build, manage, and
# explore a family tree structure, where each person can have parent-child
# relationships.
# Tree Data Structure: Each node represents a person. Nodes can have a parent and
# multiple children.
# Dynamic Memory Allocation: For creating new family members at runtime.
# Recursion: For displaying the tree or searching through it.
# Key Features:
# 1. Add a New Family Member(person)
# 2. Set Parents
# 2. Display the Entire Family Tree
# 3.Search for a Member
# 4.Show Ancestors
# 5. Exit
class Person: # Node
def __init__(self, name):
self.name = name # Name of person
self.father = None # address of Fatehers Node
self.mother = None # address of Mothers Node
self.next = None # for linked list
class FamilyTree:
def __init__(self):
self.head = None
def find_person(self, name):
temp = self.head
while temp!=None:
if temp.name.lower() == name.lower():
return temp
temp = temp.next
return None
def add_person(self, name):
if self.find_person(name):
print("Person already exists.")
return
new_person = Person(name) # Create Node
if self.head==None:
self.head = new_person
else:
temp = self.head
while temp.next!=None:
temp = temp.next
temp.next = new_person
print("Person added successfully.")
#Sonu Deepak Mamta
def set_parents(self, child_name, father_name, mother_name):
child = self.find_person(child_name)
if not child:
print("Child not found. Please add the person first.")
return
father = self.find_person(father_name)
if not father:
print("Father not found. Please add the person first.")
return
mother = self.find_person(mother_name)
if not mother:
print("Mother not found. Please add the person first.")
return
child.father = father
child.mother = mother
print("Parents set successfully for: ",child_name)
def display_tree(self):
if self.head==None:
print("No family members found.")
return
temp = self.head
while temp!=None:
print(f"\n Name: {temp.name}")
if temp.father!=None:
print(f" Father: {temp.father.name}")
if temp.mother!=None:
print(f" Mother: {temp.mother.name}")
temp = temp.next
def search_person(self, name):
found = self.find_person(name)
if found!=None:
print(f" {name} is in the family tree.")
else:
print(f" {name} not found.")
def show_ancestors(self, name): # Sonu
person = self.find_person(name)
if not person:
print(" Person not found.")
return
print(f"\n Ancestors of {person.name}:")
self._print_ancestors(person, 1)
def _print_ancestors(self, person, level):
if person.father!=None:
print(" " * level + f" Father: {person.father.name}")
self._print_ancestors(person.father, level + 1)
if person.mother:
print(" " * level + f" Mother: {person.mother.name}")
self._print_ancestors(person.mother, level + 1)
def main():
tree = FamilyTree()
while True:
print("\n====== Family Tree Builder ======")
print(" Add Person ")
print(" Set Parents ")
print(" Display Family Tree ")
print(" Search Person ")
print(" Show Ancestors ")
print(" Exit ")
print("==================================")
choice = input(" Choose an option: ")
if choice == '1':
name = input(" Enter person's name: ")
tree.add_person(name)
elif choice == '2':
child = input(" Enter child's name: ")
father = input(" Enter father's name: ")
mother = input(" Enter mother's name: ")
tree.set_parents(child, father, mother)
elif choice == '3':
tree.display_tree()
elif choice == '4':
name = input(" Enter name to search: ")
tree.search_person(name)
elif choice == '5':
name = input(" Enter name to show ancestors: ")
tree.show_ancestors(name)
elif choice == '6':
print(" Exiting. Goodbye!")
break
else:
print(" Invalid choice. Try again.")
if __name__ == "__main__":
main()