General Tree

I need help on one assignment, in which i need to build a tree, that has one parent as a root "A" and the children of "A" is more than 2(let's just say 30 children.)All the children are ordered.  I just need a simple jave code for building this kind of tree.

                                               / /  \  \.......
                                             1  2  3  4.......
Who is Participating?
jimmackConnect With a Mentor Commented:
I understand your concerns and having followed the links around, I must say that I respect your request to reject the points.  It's nice to see experts with good ethics ;-)

However, I'm not sure you should be hounding this poor guy ;-)  I just checked his profile and he's only been an EE member for two days, so he still needs to get used to the way it works.  Leave it to us in this TA.  We'll take care of him now.  Thanx ;-)

If this link provided by LucF is the source of your original question, I would say that you probably don't really want to be implementing a tree in the way you suggest.  The question you have says:

>> Write a program to maintain this data base. Your program should process requests to add, delete and list the following:

You would probably be better having a class for Flight, a class for FlightDate and (possibly) a class for Passengers.

The structure only needs to be simple and you can employ the suggestions by Bruno for sorting.

public class Flight
    private int flightNum;
    private ArrayList flightDates;

    public Flight(int num)
        flightNum = num;
        flightDates = new ArrayList();

    public int getFlightNum()
       return (flightNum);

    public void addFlightDate(FlightDate fDate)

    public ArrayList getFlightDates()
        return (flightDates);

If passenger information is limited to just a String for their name, there is no need for a Passenger class.  If you want to store more detail (eg. passport number, age etc., the you should create a class to store this data).

Create similar classes for FlightDate.  In the FlightDate class, you'll need to store the date instead of the flightNum and the ArrayList will store passengers.

For each of the classes you create, as bruno said, you'll need to implement java.util.Comparator so that you can call the Collections.sort() with the ArrayLists as parameters.

Finally, you'll need to create your controlling class (with the "main(String[] args)" entry point).

This way, you avoid the "problem areas" of trees (eg. recursive methods etc.), but you do achieve the top-down data structure that you require.

When you've written all the code, post again if it doesn't work.  If you get exceptions, please post the full exception.  If the exception indicates an error in the code (eg., then post the code so we can see what is at line 123.


Good luck.

this is just a class you can use to solve it, if you disagree with it or have any doubts, just post a comment.

public class Tree {
     String node;
     Vector children; // if the height is always, a vector of String will be ok

     public Tree(String node) {
        this.node = node;
        children = new Vector();
     public void addChild(String child) {
        Collections.sort(children); // if you change the type of the children you must implement Comparable

LucFEMEA Server EngineerCommented:
quote from: http:Q_20778018.html

"This is not a home work problem, it's one of the problem from a book. I donot get home works."

So I still think this is a homework question. Maybe not from school, but if you want to learn something try figuring it out piece by piece.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LucFEMEA Server EngineerCommented:
Ok, Jim, good luck ;-) this isn't my TA after all.
What's this!!!!!

shirazballer, is this a mistake?  If so, please could you post on the CS TA to change the accepted answer.
shirazballerAuthor Commented:
Dang!!! what was i thinking. No way, not Lucf, never in my life.

I am sorry jimmack i was thinking i was accepting your answer, but it turned out to be somebody else's.
I am sending them a request to accept your answer instead of LUCF

I really liked the comments u made about LUCF.

Thank You Very Mcuh
Phew!  I wondered what was going on there for a minute ;-)

Is your code working OK now?
LucFEMEA Server EngineerCommented:
Second time this happens shirazballer!
jimmack, I already posted in http:Q_20778053.html

shirazballerAuthor Commented:
Yeah it is working fine, it's doing what i wanted to do.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.