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
*/
#include <iostream>
#include <cstring>
#include <windows.h>
using namespace std;
class Person // Node
{
public:
char name[50];
Person* father;
Person* mother;
Person* next;
Person(const char* pname)
{
strcpy(name, pname);
father = nullptr;
mother = nullptr;
next = nullptr;
}
};
class FamilyTree
{
private:
Person* head;
Person* findPerson(const char* name) // mamta
{
Person* temp = head;
while (temp != nullptr)
{
if (strcmp(temp->name, name) == 0)
return temp;
temp = temp->next;
}
return nullptr;
}
void printAncestors(Person* person, int level = 1)
{
if (person == nullptr)
return;
if (person->father != nullptr)
{
for (int i = 0; i < level; i++) cout << " ";
cout << "Father: " << person->father->name << endl;
printAncestors(person->father, level + 1);
}
if (person->mother != nullptr) {
for (int i = 0; i < level; i++) cout << " ";
cout << "Mother: " << person->mother->name << endl;
printAncestors(person->mother, level + 1);
}
}
public:
FamilyTree()
{
head = nullptr;
}
void addPerson()
{
char name[50];
cout << "Enter person's name: ";
cin.getline(name, 50);
if (findPerson(name) != nullptr)
{
cout << "Person already exists.\n";
return;
}
Person* newPerson = new Person(name);
if (head == nullptr)
head = newPerson;
else
{
Person* temp = head;
while (temp->next != nullptr)
temp = temp->next;
temp->next = newPerson;
}
cout << "Person added.\n";
}
void setParents()
{
char childName[50], fatherName[50], motherName[50];
cout << "Enter child's name: ";
cin.getline(childName, 50);
Person* child = findPerson(childName);
if (child == nullptr)
{
cout << "Child not found. Add person first.\n";
return;
}
cout << "Enter father's name: ";
cin.getline(fatherName, 50);
Person* father = findPerson(fatherName);
if (father == nullptr) {
cout << "Father not found. Add person first.\n";
return;
}
cout << "Enter mother's name: ";
cin.getline(motherName, 50);
Person* mother = findPerson(motherName);
if (mother == nullptr) {
cout << "Mother not found. Add person first.\n";
return;
}
child->father = father;
child->mother = mother;
cout << "Parents set successfully.\n";
}
void displayTree()
{
if (head == nullptr)
{
cout << "No family members found.\n";
return;
}
Person* temp = head;
while (temp != nullptr)
{
cout << "\nName: " << temp->name << endl;
if (temp->father != nullptr)
cout << " Father: " << temp->father->name << endl;
if (temp->mother != nullptr)
cout << " Mother: " << temp->mother->name << endl;
temp = temp->next;
}
}
void searchPerson()
{
char name[50];
cout << "Enter name to search: ";
cin.getline(name, 50); //mamta
Person* found = findPerson(name);
if (found != nullptr)
cout << " " << name << " is in the family tree.\n";
else
cout << " " << name << " not found.\n";
}
void showAncestors()
{
char name[50];
cout << "Enter name to show ancestors: "; //shubham
cin.getline(name, 50);
Person* person = findPerson(name);
if (person == nullptr)
{
cout << " Person not found.\n";
return;
}
cout << "🧬 Ancestors of " << person->name << ":\n";
printAncestors(person);
}
};
// Menu
void menu() {
FamilyTree tree;
int choice;
do {
cout << "\n===== Family Tree Builder =====\n";
cout << "1. Add Person\n";
cout << "2. Set Parents\n";
cout << "3. Display Family Tree\n";
cout << "4. Search Person\n";
cout << "5. Show Ancestors\n";
cout << "6. Exit\n";
cout<<"=============================\n";
cout << "Choose an option: ";
cin >> choice;
cin.ignore(); // flush newline
switch (choice) {
case 1:
tree.addPerson();
break;
case 2:
tree.setParents();
break;
case 3:
tree.displayTree();
break;
case 4:
tree.searchPerson();
break;
case 5:
tree.showAncestors();
break;
case 6:
cout << " Exiting. Goodbye!\n";
break;
default:
cout << " Invalid choice. Try again.\n";
}
} while (choice != 6);
}
int main()
{
SetConsoleOutputCP(CP_UTF8);
menu();
return 0;
}