sakshi
sakshi

Reputation: 105

Family Tree in java

I need to create a java program that can create a family tree in java. The program reads an input file in below format:

A is father of B
B is father of C
A is father of D
:
:

What I need to do is to create a family tree or whatever that seems logical. Then JUnit tests will run on my program, asking to return true or false like: B is child of A, A is an ancestor of E......

Plus, I need to traverse the tree from left to right, and show hierarchy like: A B C.....

I have thought of some logic, though I am new to Java. I have created a Person class, this has elements: Name, Left of Person type and Right of person type:

class Person {

    String name;
    Person left;
    Person right;
}

Plus I have added getters, setters and a constructor.

Then I read the file and create one person as:

BufferedReader br = new BufferedReader(new FileReader(new File(path)));
String names[] = br.readLine().split(" ");
Person p = new Person(names[0]);
p.setLeft(new Person(names[4]));

I am totally confused at this stage and would benefit some guidance. Thanks in advace

Upvotes: 0

Views: 2967

Answers (1)

Paul Janssens
Paul Janssens

Reputation: 722

Instead of left and right, better use List< Person > children, there's no point in using a binary tree for this problem.

Upvotes: 1

Related Questions