一棵树,其中每个节点可以有多个父母 [英] A tree, where each node could have multiple parents

查看:547
本文介绍了一棵树,其中每个节点可以有多个父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个理论/细节问题:这里有一些很好的问题。不幸的是,我遇到的大多数答案似乎都强调了第三方的解决方案。



想像一下每个财产可以由多方拥有的财产。此外,从所有权的一次迭代到下一个两个邻国领土的所有者可以决定将所有权部分地结合在一起,以便.....



领土1,t = 0: a,b,c,d
区域2,t-0:e,f,g,h



区域1,t = 1:a,b ,g,h
territory 2,t = 1:g,h



也就是说,c和d不再拥有属性,而g和h成为胖猫,可以这么说。



目前,我表示这个数据结构是一棵树,每个孩子都可以有多个父母。我的目标是在复合数据结构中填补这一点,但是我有一个问题,在客户端可能会如何回退,更新以前的所有权,而不影响整个结构。



我的问题是两个方面:Easy:这个数据结构的方便名称是什么,我可以自己google吗?



硬:我是什么做错了?当我的代码我试着保持咒语,保持简单,愚蠢在我的头,我觉得我打破这个信条。



谢谢你们! / p>

解决方案


我的问题是双重的:Easy:这个数据的方便名称是
结构,以便我可以自己google?


你在这里不是一棵树,它是一个图形。一个multimap将帮助你在这里。
但是任何邻接列表或邻接矩阵将给你一个很好的开始。



这是一个邻接矩阵和列表视频:在邻接矩阵和列表上的Youtube


硬:我做错了什么?


这真的很难说。也许你没有以正确的方式建立关系
。这不是很难,因为开始有一个好的数据结构。



而且,当你要求设计模式(但你可能发现自己)时,
复合图案将让您轻松建模此类设置。


Here's a theoretical/pedanticle question: There are some good questions here btw. Unfortunately, most the answers I've come across seem to stress some third party solution.

Imagine property where each property could be owned by multiple parties. Furthermore, from one iteration of ownership to the next two neighboring territories' owners could decide to partly combine ownership such that.....

territory 1, t=0: a,b,c,d territory 2, t-0: e,f,g,h

territory 1, t=1: a,b,g,h territory 2, t=1: g,h

That is to say, c and d no longer own property, and g and h became fat cats, so to speak.

I'm, currently, representing this data structure as a tree where each child could have multiple parents. My goal is to cram this in the composite data structure, but I'm having issues getting a conceptual footing on how the client might go back and update previous ownership without mucking up the whole structure.

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

Hard: What am I doing wrong? When I code I try to keep the mantra, "Keep it simple, Stupid," in my head, and I feel I am breaking this credo.

Thanks guys!

解决方案

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.

这篇关于一棵树,其中每个节点可以有多个父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆