Java 位置层次数据结构
位置层次结构的数据结构或数据模型Java 位置层次数据结构,java,data-structures,graph,tree,Java,Data Structures,Graph,Tree,位置层次结构的数据结构或数据模型 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.
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.
当有人询问从机场:SFO到机场:FRA的费率时,应用程序应查找从机场:SFO到机场:FRA的任何可用费率
由于我们没有一个(我们只有城市到城市),应用程序应该检查一个更高级别的机场,即城市。因此,应用程序应该能够找到机场城市:SFO和机场城市:法兰克福,并检查是否有可用的费率。在本例中,它拾取100美元,因为城市:旧金山到城市:法兰克福的费率保持为100美元
如何在数据结构(Java)中表示此位置层次结构?图形或树有用吗?如果有,请给我一些样品。IMO,有两种方法自下而上或自上而下(尽管两者实际上都是基于HAS-A关系: 自下而上: 1、有机场类、城市类、州类、国家类 机场有城市,城市有国家,国家有国家变量 现在,无论何时您需要费率,您都可以转到Airport对象,检查City->State->Country等并相应地收费 自上而下: 1、有国家、州、市、机场等类别 国家将有一份包含州的清单,州将有城市清单,城市将有机场清单
我更喜欢第一个,因为维护父项的1值比维护所有子项的列表更容易/有效。您可以尝试下面的树结构 优势 1.跨不同位置类型的统一数据结构 2.在添加新位置类型的情况下,无需新类别 3.家长查找变得容易 4.父对象的递归遍历成为可能
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
}
}
5.子对象的递归遍历成为可能
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
}
}
公共类位置
{
私人场所类型场所类型;
private Set children=new HashSet();
私人位置家长;
公共Intrato(位置)
{
整数率=-1;
位置从=此;
位置到=位置;
做
{
速率=获取速率(从,到);
如果(费率>-1)
打破
from=from.parent;
to=to.parent;
if(from==null | | to==null)
打破
}虽然(正确);
回报率;
}
public void addChildLocation(位置子项)
{
child.parent=这个;
添加(child);
}
public int getRate(位置从,位置到)
{
//1.实现自己的数据库查找等。。。。。。
//2.还要注意反向定位参数。。。
返回-1;
}
公共枚举位置类型
{
机场,
城市
国家,,
国家
}
}
当您说应该查找任何可用的费率,以便在一个城市中有不同的机场,或者每个机场提供不同的费率时?无论如何,我的意思是,如果机场到机场的费率不可用,应用程序应该查找城市到城市的费率。如果每个机场都有一个对象,它如何有效。如果我收到一个请求机场作为旧金山,我如何得到这个对象?有什么数据结构可以帮助?