在树结构中,如何命名树、节点、叶子?

发布于 2024-09-14 05:14:46 字数 300 浏览 7 评论 0原文

这个问题是关于最佳实践的。我正在实现一个 3D 区间 Kd-Tree,并且由于树的递归结构,我很想创建一个独特的类 KdTree 来表示树本身、节点和叶子。

然而:元素仅包含在叶子中,一些通用的树参数(例如分割空间之前的最大元素数)对于所有树来说都是相同的,并且最终分割平面在叶子中根本没有意义。

也就是说:我应该组成三个类(KdTreeKdNodeKdLeaf)还是假装每个节点或叶子实际上都是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

面如桃花 2024-09-21 05:14:46

我想说不需要 Tree 类。顶部元素是一个像其他所有元素一样的节点。

为了区分叶子节点和分支节点,我会选择

 namespace KdTree
 { 
       abstract class Node 
       {
             virtual EnumLeafNodes(LeafNodeCallback callback);
             virtual GetLeafCount();

       }

       class Leaf : Node 
       {
             // implement virtuals by returning/counting leaf values
       }

       class Branch : Node
       {
             // implement virtuals by delegating to child nodes

             // direct children:
             Node[] children;  
       }   
 }

“注意”,这是非常伪代码(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

 namespace KdTree
 { 
       abstract class Node 
       {
             virtual EnumLeafNodes(LeafNodeCallback callback);
             virtual GetLeafCount();

       }

       class Leaf : Node 
       {
             // implement virtuals by returning/counting leaf values
       }

       class Branch : Node
       {
             // implement virtuals by delegating to child nodes

             // direct children:
             Node[] children;  
       }   
 }

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.

时光倒影 2024-09-21 05:14:46

在 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

谁许谁一生繁华 2024-09-21 05:14:46

线索和树似乎只是位于“分支”末端的开始处的节点。

在这些情况下,我只是将它们命名为“节点”,在解析它们时,我将它们称为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文