DSA Project – Family Tree Builder

Learn DSA & Get Ready for MAANG Companies Start Now!!

Program 1

//Project: Family Tree Builder (Based on BT)
/*
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
2. Set Parents 
2. Display the Entire Family Tree
3.Search for a Member
4.Show Ancestors

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define NAME_LENGTH 50

typedef struct Person
 {
    char name[NAME_LENGTH];
    struct Person* father;
    struct Person* mother;
    struct Person* next; // for linked list of all persons
} Person;

Person* head = NULL; // start of linked list

// Create a new person
Person* createPerson(char name[])      //amit
{
    Person* newPerson = (Person*)malloc(sizeof(Person));
    strcpy(newPerson->name, name);
    newPerson->father = NULL;
    newPerson->mother = NULL;
    newPerson->next = NULL;

    // Add to linked list
    if (head == NULL) 
    {
        head = newPerson;
    } 
    else 
    {
        Person* temp = head;
        while (temp->next != NULL)
            temp = temp->next;
        temp->next = newPerson;
    }
    return newPerson;
}

// Search person by name
Person* findPerson(char name[]) 
{
    Person* temp = head;
    while (temp != NULL) {
        if (strcmp(temp->name, name) == 0)
            return temp;
        temp = temp->next;
    }
    return NULL;
}

// Set parents
void setParents(char childName[], char fatherName[], char motherName[]) 
{
    Person* child = findPerson(childName);
    Person* father = findPerson(fatherName);
    Person* mother = findPerson(motherName);

    if (child==NULL || father==NULL || !mother==NULL) 
    {
        printf(" One or more names not found. Make sure all persons are added.\n");
        return;
    }

    child->father = father;
    child->mother = mother;

    printf(" Parents set successfully for %s.\n", childName);
}

// Display family tree (basic)
void displayPerson(Person* person, int level) 
{
    if (person==NULL) 
    return;
    for (int i = 0; i < level; i++) 
     printf("  ");
    
    printf(" %s\n", person->name);
    if (person->father!=NULL || person->mother!=NULL) 
    {
        for (int i = 0; i < level + 1; i++) 
         printf("  ");
        if (person->father) printf(" Father: %s\n", person->father->name);
        if (person->mother) 
        {
            for (int i = 0; i < level + 1; i++) 
            printf("  ");
            printf(" Mother: %s\n", person->mother->name);
        }
    }
}

// Display full tree
void displayFamilyTree() 
{
    Person* temp = head;
    printf("\n Family Tree:\n");
    while (temp != NULL) 
    {
        displayPerson(temp, 0);
        temp = temp->next;
    }
}

// Show ancestors
void showAncestors(Person* person) 
{
    if (person==NULL) 
    return;
    printf(" Ancestors of %s:\n", person->name);
    if (person->father!=NULL) 
    {
        printf(" Father: %s\n", person->father->name);
        showAncestors(person->father);
    }
    if (person->mother) 
    {
        printf(" Mother: %s\n", person->mother->name);
        showAncestors(person->mother);
    }
}

// Menu
void menu() 
{
    int choice;
    char name[NAME_LENGTH], father[NAME_LENGTH], mother[NAME_LENGTH];

    do {
        printf("\n=====  Family Tree Builder =====\n");
        printf("1. Add Person\n");
        printf("2. Set Parents\n");
        printf("3. Display Family Tree\n");
        printf("4. Search Person\n");
        printf("5. Show Ancestors\n");
        printf("6. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        getchar(); // clear newline

        switch (choice) 
        {
            case 1:
                printf("Enter person's name: ");
                fgets(name, NAME_LENGTH, stdin);
                name[strcspn(name, "\n")] = 0;
                if (findPerson(name)) 
                {
                    printf(" Person already exists!\n");
                }
                 else 
                {
                    createPerson(name);         
                    printf(" Person added.\n");
                }
                break;

            case 2:
                printf("Enter child's name: ");
                fgets(name, NAME_LENGTH, stdin);
                name[strcspn(name, "\n")] = 0;

                printf("Enter father's name: ");
                fgets(father, NAME_LENGTH, stdin);
                father[strcspn(father, "\n")] = 0;

                printf("Enter mother's name: ");
                fgets(mother, NAME_LENGTH, stdin);
                mother[strcspn(mother, "\n")] = 0;

                setParents(name, father, mother);
                break;

            case 3:
                displayFamilyTree();
                break;

            case 4:
                printf("Enter name to search: ");
                fgets(name, NAME_LENGTH, stdin);
                name[strcspn(name, "\n")] = 0;
                if (findPerson(name)) {
                    printf(" %s found in the tree.\n", name);
                } else {
                    printf(" Person not found.\n");
                }
                break;

            case 5:
                printf("Enter name to show ancestors: ");
                fgets(name, NAME_LENGTH, stdin);
                name[strcspn(name, "\n")] = 0;
                showAncestors(findPerson(name));
                break;

            case 6:
                printf(" Exiting...\n");
                break;

            default:
                printf(" Invalid choice.\n");
        }

    } while (choice != 6);
}

int main() 
{
    SetConsoleOutputCP(CP_UTF8);
    menu();
    return 0;
}

 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *