• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

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.

                                                   0
                                               / /  \  \.......
                                             1  2  3  4.......
0
shirazballer
Asked:
shirazballer
  • 3
  • 3
  • 2
  • +1
1 Solution
 
brunomsilvaCommented:
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) {
        children.addElement(child);
        Collections.sort(children); // if you change the type of the children you must implement Comparable
     }
}

cheers,
  bruno
0
 
LucFCommented:
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.
0
 
jimmackCommented:
LucF:
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 ;-)


shirazballer:
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)
    {
        flightDates.add(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. FlightDate.java:123), then post the FlightDate.java code so we can see what is at line 123.

;-)

Good luck.

Jim.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LucFCommented:
Ok, Jim, good luck ;-) this isn't my TA after all.
0
 
jimmackCommented:
What's this!!!!!

shirazballer, is this a mistake?  If so, please could you post on the CS TA to change the accepted answer.
0
 
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
0
 
jimmackCommented:
Phew!  I wondered what was going on there for a minute ;-)

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

LucF
0
 
shirazballerAuthor Commented:
Yeah it is working fine, it's doing what i wanted to do.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now