Solved

General Tree

Posted on 2003-10-25
10
313 Views
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.

                                                   0
                                               / /  \  \.......
                                             1  2  3  4.......
0
Comment
Question by:shirazballer
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 4

Expert Comment

by:brunomsilva
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) {
        children.addElement(child);
        Collections.sort(children); // if you change the type of the children you must implement Comparable
     }
}

cheers,
  bruno
0
 
LVL 32

Expert Comment

by:Luc Franken
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.
0
 
LVL 15

Accepted Solution

by:
jimmack earned 500 total points
ID: 9622081
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
 
LVL 32

Expert Comment

by:Luc Franken
ID: 9622089
Ok, Jim, good luck ;-) this isn't my TA after all.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Expert Comment

by:jimmack
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.
0
 

Author Comment

by:shirazballer
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
0
 
LVL 15

Expert Comment

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

Is your code working OK now?
0
 
LVL 32

Expert Comment

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

LucF
0
 

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now