Site icon DataFlair

DSA Java Project – Family Tree Builder

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
*/
import java.util.Scanner;

class Person          // Node 
{
    String name;
    Person father;
    Person mother;
    Person next;  // To create linked list

    Person(String name)
     {
        this.name = name;
        this.father = null;
        this.mother = null;
        this.next = null;
    }
}

public class FamilyTree 
{
    private Person head;

    public FamilyTree() 
    {
        head = null;
    }

    private Person findPerson(String name) //deepak
    {
        Person temp = head;
        while (temp != null) 
        {
            if (temp.name.equalsIgnoreCase(name)) 
            {
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }

    public void addPerson(String name)   //mamta
    {
        if (findPerson(name) != null) 
        {
            System.out.println("[X] Person already exists.");
            return;
        }

        Person newPerson = new Person(name);   // Node Creation
        if (head == null) 
        {
            head = newPerson;
        } else
         {
            Person temp = head;
            while (temp.next != null)
                temp = temp.next;
            temp.next = newPerson;
        }
        System.out.println("[✓] Person added.");
    }

    public void setParents(String childName, String fatherName, String motherName) {
        Person child = findPerson(childName);
        if (child == null)  //child=1011  shubham
        {
            System.out.println(" [X] Child not found. Please add the person first.");
            return;
        }

        Person father = findPerson(fatherName); //father=1166  deepak
        if (father == null)
         {
            System.out.println(" [X] Father not found. Please add the person first.");
            return;
        }

        Person mother = findPerson(motherName);  //2166  mamta
        if (mother == null) 
        {
            System.out.println(" [X] Mother not found. Please add the person first.");
            return;
        }

        child.father = father;
        child.mother = mother;
        System.out.println(" [✓]Parents set successfully.");
    }

    public void displayTree() 
    {
        if (head == null) 
        {
            System.out.println(" [X]No family members found.");
            return;
        }

        Person temp = head;
        while (temp != null) 
        {
            System.out.println("\n Name: " + temp.name);
            if (temp.father != null)
                System.out.println("    Father: " + temp.father.name);
            if (temp.mother != null)
                System.out.println("    Mother: " + temp.mother.name);
            temp = temp.next;
        }
    }

    public void searchPerson(String name)
     {
        Person found = findPerson(name);
        if (found != null)
            System.out.println("[✓]" + name + " is in the family tree.");
        else
            System.out.println("[X]" + name + " not found.");
    }

    public void showAncestors(String name) ////shubham
    {
        Person person = findPerson(name);
        if (person == null) 
        {
            System.out.println(" [X] Person not found.");
            return;
        }

        System.out.println("  Ancestors of " + person.name + ":");
        printAncestors(person, 1);
    }

    private void printAncestors(Person person, int level) 
    {
        if (person.father != null) 
        {
            printIndent(level);
            System.out.println("Father: " + person.father.name);
            printAncestors(person.father, level + 1);
        }
        if (person.mother != null) 
        {
            printIndent(level);
            System.out.println("Mother: " + person.mother.name);
            printAncestors(person.mother, level + 1);
        }
    }

    private void printIndent(int level) 
    {
        for (int i = 0; i < level; i++)
            System.out.print("  ");
    }

    public static void main(String[] args)
     {
        Scanner scanner = new Scanner(System.in);
        FamilyTree tree = new FamilyTree();

        int choice;
        String name, father, mother;

        do {
            System.out.println("\n=====  Family Tree Builder =====");
            System.out.println("1. Add Person");
            System.out.println("2. Set Parents");
            System.out.println("3. Display Family Tree");
            System.out.println("4. Search Person");
            System.out.println("5. Show Ancestors");
            System.out.println("6. Exit");
            System.out.println("===========================");
            System.out.print("Choose an option: ");
            choice = Integer.parseInt(scanner.nextLine());

            switch (choice) 
            {
                case 1:
                    System.out.print("Enter person's name: ");
                    name = scanner.nextLine(); 
                    tree.addPerson(name);
                    break;
                case 2:
                    System.out.print("Enter child's name: ");
                    name = scanner.nextLine();
                    System.out.print("Enter father's name: ");
                    father = scanner.nextLine();
                    System.out.print("Enter mother's name: ");
                    mother = scanner.nextLine();
                    tree.setParents(name, father, mother);
                    break;
                case 3:
                    tree.displayTree();
                    break;
                case 4:
                    System.out.print("Enter name to search: ");
                    name = scanner.nextLine();
                    tree.searchPerson(name);
                    break;
                case 5:
                    System.out.print("Enter name to show ancestors: ");
                    name = scanner.nextLine();
                    tree.showAncestors(name); 
                    break;
                case 6:
                    System.out.println(" Exiting. Goodbye!");
                    break;
                default:
                    System.out.println(" Invalid choice. Try again.");
            }
        } while (choice != 6);

        scanner.close();
    }
}

 

Exit mobile version