有这样一个图:
      grandfather----grandmother
                   |
           _______________
          |               | 
       father --mother   aunt         
              |
         __________
        |          |
       Me         brother---brother's wife
                          |
                       ________
                      |        |
                    nephew    niece  有了这样的结构,如何从任一结点遍历其它结点,请给出Person的类结构和如何实现。谢谢!

解决方案 »

  1.   

    两个策略:
    1.根据你的图,根节点是唯一的,所以任何一个节点不停的向上找父节点
    直到没有父节点也就是根节点,然后再遍历
    2.每一个节点无非包括三个节点:a.父节点,b.兄弟节点,c.子节点
    各自用递归算法实现遍历具体如何的算法自己实现
      

  2.   

    Person:
         Person father
         Person mother
         Person spouseThen 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 objectRelation:
        Person a;
        Person b;
        RelationType;Then, it can represent everything.