我有一个网格:
网格由单元格组成,递归地分成更小的单元格。网格中的每个子单元格都受其父单元格的约束。
网格中的单元格存储在类似图形的结构中。每个单元有四个连接,每个角落一个。每个角连接到另一个单元,使得单元的边缘平行于连接并且最接近它是齐平的。该网格可以由以下 JSON 表示:
{
"grid1":
{
"topLeft": null,
"topRight": "grid2",
"bottomLeft": "grid3",
"bottomRight": "grid2",
"topLeftDirection": null,
"topRightDirection": "horizontal",
"bottomLeftDirection": "vertical",
"bottomRightDirection": "horizontal"
},
"grid2":
{
"topLeft": "grid1",
"topRight": "grid4",
"bottomLeft": "grid1",
"bottomRight": "grid5",
"topLeftDirection": "horizontal",
"topRightDirection": "horizontal",
"bottomLeftDirection": "horizontal",
"bottomRightDirection": "vertical"
},
"grid3":
{
"topLeft": "grid1",
"topRight": "grid2",
"bottomLeft": null,
"bottomRight": "grid10",
"topLeftDirection": "vertical",
"topRightDirection": "vertical",
"bottomLeftDirection": null,
"bottomRightDirection": "horizontal"
},
"grid4":
{
"topLeft": "grid2",
"topRight": "grid7",
"bottomLeft": "grid5",
"bottomRight": "grid5",
"topLeftDirection": "horizontal",
"topRightDirection": "horizontal",
"bottomLeftDirection": "vertical",
"bottomRightDirection": "vertical"
},
"grid5":
{
"topLeft": "grid4",
"topRight": "grid4",
"bottomLeft": "grid6",
"bottomRight": "grid6",
"topLeftDirection": "vertical",
"topRightDirection": "vertical",
"bottomLeftDirection": "vertical",
"bottomRightDirection": "vertical"
},
"grid6":
{
"topLeft": "grid5",
"topRight": "grid5",
"bottomLeft": "grid9",
"bottomRight": "grid8",
"topLeftDirection": "vertical",
"topRightDirection": "vertical",
"bottomLeftDirection": "vertical",
"bottomRightDirection": "horizontal"
},
"grid7":
{
"topLeft": "grid4",
"topRight": "grid11",
"bottomLeft": "grid8",
"bottomRight": "grid8",
"topLeftDirection": "horizontal",
"topRightDirection": "horizontal",
"bottomLeftDirection": "vertical",
"bottomRightDirection": "vertical"
},
"grid8":
{
"topLeft": "grid7",
"topRight": "grid7",
"bottomLeft": "grid6",
"bottomRight": "grid9",
"topLeftDirection": "vertical",
"topRightDirection": "vertical",
"bottomLeftDirection": "horizontal",
"bottomRightDirection": "vertical"
},
"grid9":
{
"topLeft": "grid6",
"topRight": "grid8",
"bottomLeft": "grid10",
"bottomRight": "grid10",
"topLeftDirection": "vertical",
"topRightDirection": "vertical",
"bottomLeftDirection": "vertical",
"bottomRightDirection": "vertical"
},
"grid10":
{
"topLeft": "grid9",
"topRight": "grid9",
"bottomLeft": "grid3",
"bottomRight": "grid12",
"topLeftDirection": "vertical",
"topRightDirection": "vertical",
"bottomLeftDirection": "horizontal",
"bottomRightDirection": "horizontal"
},
"grid11":
{
"topLeft": "grid7",
"topRight": null,
"bottomLeft": "grid12",
"bottomRight": "grid12",
"topLeftDirection": "horizontal",
"topRightDirection": null,
"bottomLeftDirection": "vertical",
"bottomRightDirection": "vertical"
},
"grid12":
{
"topLeft": "grid11",
"topRight": "grid11",
"bottomLeft": "grid10",
"bottomRight": null,
"topLeftDirection": "vertical",
"topRightDirection": "vertical",
"bottomLeftDirection": "horizontal",
"bottomRightDirection": null
}
}
这是结构的描述:
通过查看图表,人们可以看到较大的细胞组,其中包含较小的细胞组。 我正在尝试开发一种可以采用网格数据结构并将其转换为树的算法。树中的每个元素要么是叶子(表示网格中的一个单元格),要么是一个容器,其中包含较小的容器或网格中的单元格。这是网格看起来像一棵树的样子:
到目前为止,我还没有多少运气。这是我到目前为止所尝试的:
- 我尝试在室外工作,确定网格的较大部分,然后将它们分开以找到较小的部分。问题是很难确定什么构成容器,以及如何在网格中挑选除网格本身之外最大的容器。
- 我尝试获取单个单元格并跟随一条链到它们最大的父级,然后将这些链组合成一个网格。这种方法的问题是父容器并不总是很明显。
- 我尝试取一个网格,将其拆分为较小的网格,然后沿其最大边缘将其拆分。然而,当遇到边缘的末端时,很难判断该末端是否实际上是网格的边缘,或者它是否是局部边缘。
- 我试图确定网格中的哪些单元格具有直接兄弟姐妹(通过注意哪些连接位于连接到同一单元格的单元格的同一侧。然后我将它们放在一个容器中,用容器替换结构中的单元格并重复过程。我相信这种方法可能确实有效,但它看起来非常繁琐且效率低下。
- 最后,我查看了几种不同的数据结构,包括树形图。我相信树形图是在这种情况下使用的非常好的数据结构,但我的网格已经内置了一个结构。我能找到的所有构建 kd 树的算法都没有假设网格中存在任何预先存在的结构。
我真的坚持这一点,如果有任何意见、建议或想法,我将不胜感激。
更新
看了 Yochai Timmer 的回答后,我想强调一下网格结构的重要性。下面是两个盒子的示例,它们看起来相同,但具有不同的结构,因此具有非常不同的树表示: