最佳实践:子节点对其父节点的了解
我想了解树中的子节点应该了解其父节点多少信息的最佳实践。
我当前的问题相当简单直接。我有一棵信息树,想要获取叶节点的“全名”(在本例中,它将是树中每个节点到叶节点的名称,并用点分隔)。我可以通过向叶节点添加一个“getFullName”方法来完成此操作,该方法遍历树到根并在每个父级的名称前面添加并返回最终结果,但这需要叶节点知道其父级的类类型(叶和非叶子不是同一类)。或者我可以在其他地方添加一个实用函数,该函数基本上执行相同的操作,但了解不同的类类型。
我试图四处搜索,但问题有点太宽泛,无法在谷歌上获得任何有用的结果。
提前致谢。
I wanted to get an idea of the best practice for how much information a child node in a tree should know about its parent.
My current problem is fairly simple and straight-forward. I have a tree of information and wanted a to get the "full name" of a leaf node (in this case it will be the name of each node in the tree to the leaf node separated by a dot). I can do this either by adding a "getFullName" method to the leaf nodes which traverses up the tree to the root and prepends each parent's name and returns the final result but this would require the leaf nodes knowing the class type of its parent (leaf and non-leaf are not the same class). Or I can add a utility function somewhere else that does basically the same thing but knows about the different class types.
I attempted to search around but the question is a little too broad to get any useful hits on Google.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您确实在这里有很多选择,是的,您的问题解决了一种常见情况,即一方面权衡处理效率,另一方面增加存储需求和可能的冗余。
不存在单一的最佳实践。空间/时间权衡取决于您的情况。如果您使用冗余存储,将子链接和父链接保留在节点内,则需要确保数据结构正确封装,并且您的方法保持所有内容一致。
由于您有一个从顶部遍历到节点的用例,因此父链接工作正常,您可以通过递归或从后到前构建字符串来组成全名。因为用例适合您的情况,所以这不是一个坏主意。
另一种选择是将全名存储在节点中,但这会在移动节点时增加冗余。
简而言之,您不必担心违反最佳实践,但您应该权衡所有选择,做出适合您的选择。
现在,如果您正在制作一个通用的树数据结构,例如 Java 的 TreeNode,您可能会创建一个接口并允许人们按照他们认为合适的方式实现事物,提供一个合适的通用实现(DefaultMutableTreeNode),该实现具有所有链接 - 父级、孩子和兄弟姐妹。
You do have a lot of options here, and yes your question addresses the common situation of trading off processing efficiency on the one hand with increased storage requirements and possible redundancy on the other.
There is no single best practice. The space/time tradeoff depends on your situation. If you go with redundant storage, keeping both child and parent links within nodes, you will need to make sure your data structure is properly encapsulated and your methods keep everything consistent.
Since you have a use case that does traverse to nodes from the top, parent links work fine and you can compose the full name via recursion or building a string from back to front. Because the use case is for your situation, it is not a bad idea.
Another option is to store the full name in the node, but that adds redundancy in case you move nodes.
In short, you don't have to worry about violating best practices, but you should weigh all choices in making the choice that is right for you.
Now if you were making a general-purpose tree data structure, such as Java's TreeNode, you would probably create an interface and allow people to implement things as they see fit, providing a suitable general implementation (DefaultMutableTreeNode) that has all links -- parent, child, and sibling.
您描述的情况是使用递归的典型情况。换句话说,树中的每个节点都可以有一个名为
getFullName
的方法,以这种方式定义:(这是伪代码;我假设如果
myParent
为 nil,那么myParent.getFullName()
是空字符串,您可以根据您的特定语言进行更改)当没有父级时,递归结束。节点必须知道的唯一事情是它自己的名称以及它的父节点是谁。
The case you depict is a typical case for use of recursion. In other words, each node in your tree could have a method called
getFullName
defined in this way:(this is pseudo-code; I assume that if
myParent
is nil, thenmyParent.getFullName()
is the empty string, you can change things as appropriate to your specific language)Recursion ends when there is no parent. The only things that a node must know is its own name and who its parent is.
我不认为让子节点类“了解”其父节点有任何问题。如果您担心可扩展性,父母可以实现一个接口。
另一种选择是让父类更新其子树中所有子节点的“名称”字段......如果您创建一棵大树然后只查询它(恒定时间查找),这可能是有意义的。同样,如果您担心耦合过多,接口可以提供帮助。
I don't see any problem with having the child-node class "know" about its parents. The parents can implement an interface if you're worried about extensibility.
Another alternative is to have parent classes update a "name" field of all child-nodes in their subtree... might make sense if you make a big tree and then just query it (constant-time lookup). Again, interfaces can help if you're worried about too much coupling.
至少每个节点应该知道它的父节点。叶节点和非叶节点具有不同的类,但这意味着每个父节点都属于同一类。使用它来遍历树并构建名称(反向)。
At the very least each node should know it's parent. Leaf and non-Leaf nodes have different classes, but that means every parent is of the same class. Use this to traverse up the tree and build the name (in reverse) as you go.