在树结构中,如何命名树、节点、叶子?
这个问题是关于最佳实践的。我正在实现一个 3D 区间 Kd-Tree,并且由于树的递归结构,我很想创建一个独特的类 KdTree 来表示树本身、节点和叶子。
然而:元素仅包含在叶子中,一些通用的树参数(例如分割空间之前的最大元素数)对于所有树来说都是相同的,并且最终分割平面在叶子中根本没有意义。
也就是说:我应该组成三个类(KdTree
、KdNode
、KdLeaf
)还是假装每个节点或叶子实际上都是一个 Kd -树(事实上,正是这种情况)和重复数据?
托马索
this question is about best practices. I'm implementing a 3D interval Kd-Tree and, because of the recursive structure of the tree I would be tempted to create a unique class, KdTree
to represent the tree itself, the nodes and the leaves.
However: elements are contained only at leaves, some general tree parameters (such as the maximum number of elements before splitting the space) are meant to be the same for all the tree and finally splitting planes have no sense at all in leaves.
That said: should I make up three classes (KdTree
, KdNode
, KdLeaf
) or just pretend that each node or leaf is in fact a Kd-Tree (which, in fact, is precisely the case) and duplicate data?
Tommaso
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想说不需要
Tree
类。顶部元素是一个像其他所有元素一样的节点。为了区分叶子节点和分支节点,我会选择
“注意”,这是非常伪代码(C# 风格)。这种设计背后的想法是,使用虚函数来区分分支和叶节点之间的行为,并且分支可以委托给它们的子节点。这是所谓的访问者模式的一个简单示例。
I would say there is no need for a
Tree
class. The top element is a node like all the rest.To differentiate the leaves and the branch nodes, I'd go for
Note that this is very much pseudocode (C#-ish). The idea behind this design is that you use virtual functions to differentiate the behavior between branches and leaf nodes, and the branches can delegate to their child nodes. This is a trivial example on what is know as the Visitor pattern.
在 KdTree 上下文中私有地创建和使用 KdNode 和 KdLeaf 类。这将使您的生活更轻松,并隐藏程序其他部分的复杂性
Create and use the classes KdNode and KdLeaf privately within the context of KdTree. This will make your life easier, and hide the complexity from other parts of the program
线索和树似乎只是位于“分支”末端的开始处的节点。
在这些情况下,我只是将它们命名为“节点”,在解析它们时,我将它们称为 KdParentNode、KdNode 和 KdChildNode。如果一个节点没有父节点,则它是树(根)节点,如果它没有子节点,则它是叶节点。
It seems the lead and tree are nodes that are simply at the beginning of the end of the "branch".
In these cases, I just name them "nodes" and when parsing through them, I would refer to them as KdParentNode, KdNode and KdChildNode. If a node does not have a parent, It's the tree (root) node, and if it does not have children, it's a leaf node.