Solved

General Tree

Posted on 2003-10-25
10
319 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 32

Expert Comment

by:Luc Franken
ID: 9622089
Ok, Jim, good luck ;-) this isn't my TA after all.
0
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

785 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