A tree, where each node could have multiple parents

↘锁芯ラ 提交于 2019-12-01 03:44:17

My question is two fold: Easy: What is a convenient name for this data structure such that I can google it myself?

What you have here is not a tree, it is a graph. A multimap will help you here. But any adjacency list or adjacency matrix will give you a good start.

Here is a video on adjacency matrix and list: Youtube on adjacency matrix and list

Hard: What am I doing wrong?

This is really hard to tell. Perhaps you did not model the relationship in a proper way. It is not that hard, given a good datastructure to start with.

And, as you asked for design patterns (but you probably found out yourself), the Composite pattern will let you model such an setting with ease.

You have a many-to-many relationship between your owners and your territories (properties). I'm not sure what language you're working in, but this sort of thing can be easily represented and tracked in a relational database. (You'd probably want a table for each entity, and the relationship would probably require a third "junction" table. If it's necessary to be able to query "back in time", this could have some sort of "time index" column as well.)

If you are working in an object-oriented language, you might create two classes, Territory and Owner, where the Territory class has a property/member/field which is a collection of references/pointers to Owners and the Owner class has a similar collection of Territories. (One of these two collections may need to contain "weak" references depending on the language.)

In this case, some difficulty may arise if you want to be able to go back and look at the network state at some particular point earlier in time. (If this is what you need, say so and I (or someone else) can post a solution that works for that.)

I'm not sure what level of simplicity you are striving for, but in neither of these cases is updating the ownership relationships really that "hard". Maybe if you posted some code it might be easier to give you more concrete advice.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!