挑战数据结构--->遍历问题

jessy_winter2000 2002-06-21 10:26:56
有这样一个图:
grandfather----grandmother
|
_______________
| |
father --mother aunt
|
__________
| |
Me brother---brother's wife
|
________
| |
nephew niece

有了这样的结构,如何从任一结点遍历其它结点,请给出Person的类结构和如何实现。谢谢!
...全文
26 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ajoo 2002-06-22
  • 打赏
  • 举报
回复
Person:
Person father
Person mother
Person spouse

Then all the other relationships like nephew, child, brother can be derived from the above.

depending on your application and algorithm, you can physically store some derived relation (kinda like denormalization in rdb)

of course, this is not very flexible. what if a person have two spouses? what if he's a gay? what if a person marries to a cousin?
Then there might exist more than one relationship between certain two persons.



But, you know what?
if you do it relationally. (this is a typical application of prolog)

you can just have a Relation object

Relation:
Person a;
Person b;
RelationType;

Then, it can represent everything.


drinkant 2002-06-21
  • 打赏
  • 举报
回复
两个策略:
1.根据你的图,根节点是唯一的,所以任何一个节点不停的向上找父节点
直到没有父节点也就是根节点,然后再遍历
2.每一个节点无非包括三个节点:a.父节点,b.兄弟节点,c.子节点
各自用递归算法实现遍历

具体如何的算法自己实现
wafer_w 2002-06-21
  • 打赏
  • 举报
回复
有这样一个图:
grandfather----grandmother
|
_______________
| |
father --mother aunt
|
__________
| |
Me brother---brother's wife
|
________
| |
nephew niece

有了这样的结构,如何从任一结点遍历其它结点,请给出Person的类结构和如何实现。谢谢!
gularess 2002-06-21
  • 打赏
  • 举报
回复
????

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧