Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this project you will create a recursive data structure to hold your family tree ( use ANY tree data structure you want ) .

For this project you will create a recursive data structure to hold your family tree (use ANY tree data structure you want). It doesnt have to be your family, any family of your choosing (you could use The Simpsons or another family). We will represent a family by creating a Person object. The Person object should have at least three fields: name, mom, and dad. Each of the mom and dad fields will either be null or will point to another Person object (which is what makes it recursive). For example, you might create objects like the following example:
Person me = new Person(Sarah Jessica Parker);
me.dad = new Person(Father Parker);
me.mom = new Person(Mother Parker);
me.mom.dad = new Person(Grandfather on Mom's side);
Make a family tree with at least four generations, even if you have to make up people. Connect all of the people in your tree in the appropriate manner. To do this, you can add additional methods to your class. For example, you might want to have a method that works like this:
me.addParents(John Smith,Jane Doe);
That could be implemented like so:
class Person {
Person mom;
Person dad;
...
void addParents(String father_name, String mother_name){
this.dad = new Person(father_name);
...
}
}
You could write a recursive function to count how many people are in your tree like so:
class Person {
...
int count(){
int sum =1; // that is you!
if (mom != null){
sum += mom.count();
}
if (dad != null){
sum += dad.count();
}
return sum;
}
}
System.out.println(The tree has + me.count()+ people in it.);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions