General Tree

Posted on 2003-10-25
Medium Priority
Last Modified: 2012-06-27
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.......
Question by:shirazballer
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1

Expert Comment

ID: 9621162
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

LVL 32

Expert Comment

ID: 9622044
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.
LVL 15

Accepted Solution

jimmack earned 2000 total points
ID: 9622081
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. FlightDate.java:123), then post the FlightDate.java code so we can see what is at line 123.


Good luck.

Technology Partners: 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!

LVL 32

Expert Comment

ID: 9622089
Ok, Jim, good luck ;-) this isn't my TA after all.
LVL 15

Expert Comment

ID: 9627202
What's this!!!!!

shirazballer, is this a mistake?  If so, please could you post on the CS TA to change the accepted answer.

Author Comment

ID: 9627227
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
LVL 15

Expert Comment

ID: 9627264
Phew!  I wondered what was going on there for a minute ;-)

Is your code working OK now?
LVL 32

Expert Comment

ID: 9627269
Second time this happens shirazballer!
jimmack, I already posted in http:Q_20778053.html


Author Comment

ID: 9627301
Yeah it is working fine, it's doing what i wanted to do.

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question