简体   繁体   中英

Location hierarchy data structure

Data Structure or Data Model for location hierarchy

I have the following location types,

Airport
City
State
Country

Hierarchy is Country has a state, State has a City and a City has airport.

City:San Francisco To City:Frankfort    Rate is 100$ is stored in the system in some form.

When a person ask for the rate from Airport:SFO To Airport:FRA, the application should look for any rate available from Airport:SFO To Airport:FRA.

As we don't have one(we only have City to City), the application should check one level higher to Airport which is City. Thus application should be able to find the City of Airport:SFO and City of Airport:Frankfort and the check whether a rate is available. In this case it picks up 100$ as City:San Francisco to City:Frankfort rate is maintained as 100$.

How can I represent this location hierarchy in a data structure (In Java)? Will graph or Tree be useful? If so can please provide me some samples.

IMO, there are two ways bottom-up or top-down (though both are actually based on HAS-A relationship:

bottom-up:

1, have classes Airport, City, State , Country

2, Airport have City, City have State, State have Country Variable

now whenever you want the rates, you goto Airport object, check for City->State->Country etc and charge accordingly

top-down:

1, have classes Country, State, City, Airport

2, Country will have a List containing State, State will have List of City and City will have Airport List

I would prefer the 1st one, since maintaining 1 value of parent is easier/efficient than maintaining List of all children.

You can try some tree structure like below

Advantages

1.uniform data structure across different location types.

2.no need for new class in case of addition of new location type.

3.parent lookups becomes easy.

4.recursive traversal of parent becomes possible.

5.recursive traversal of children becomes possible.

public class Location
{
    private LocationType locationType;
    private Set<Location> children = new HashSet<Location>();
    private Location parent;

    public int rateTo(Location location)
    {
        int rate = -1;

        Location from = this;
        Location to = location;

        do
        {
            rate = getRate(from, to);
            if (rate > -1)
                break;

            from = from.parent;
            to = to.parent;

            if (from == null || to == null)
                break;
        } while (true);

        return rate;
    }

    public void addChildLocation(Location child)
    {
        child.parent = this;
        children.add(child);
    }

    public int getRate(Location from, Location to)
    {
        //1. implement your own database lookup, etc......
        //2. take care of reverse location parameters also...
        return -1;
    }

    public enum LocationType
    {
        Airport,
        City,
        State,
        Country
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM