“权利”的目的是什么?嵌套树中的值?
如果我正在寻找父节点的嵌套节点,我会像这样查询子节点:
parent.left < child.left
这始终是正确的:
child.right < parent.right
那么为什么我发现的所有示例都运行 Between 查询?
谢谢, 马特
If I'm looking for nested nodes of a parent, I'd query for children like this:
parent.left < child.left
This is always true though:
child.right < parent.right
So why do all examples I've found run a between query?
thanks,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在嵌套集中,层次结构是通过指定每个节点的左边界和右边界来描述的,并且所有后代节点都落在这些边界内。假设您的层次结构如下所示
当将每一行添加到嵌套集中时,您最终会得到一个如下所示的表:
或者以另一种方式看待它:
当您想要查询节点的所有后代时(比方说
A
),你可以像这样查询:由于每个节点的左边界都必须小于其右边界,因此你不需要检查节点的右边界,而只需搜索落在的节点在父边界内(因此只需要右边界来确定集合的末尾在哪里)。例如,如果您尝试获取节点
C
的后代,并且仅检查C
的左边界,则查询将返回兄弟节点 (<代码>F和G
),以及与<无关的(H
、I
和J
)代码>C。In a nested set, the hierarchy is described by specifying the left and right boundaries of each node, and all descendant nodes fall within those boundaries. Let's say your hierarchy looks like
When each row is added to your nested set, you'd end up with a table that looks like:
Or to look at it another way:
When you want to query for all descendants of a node (let's say
A
), you'd query like so:Since every node's left boundary has to be less than its right boundary, you don't need to check the node's right boundary, but just search for nodes that fall within the parent's boundaries (therefore the right boundary is only needed to determine where the end of the set is). If, for example, you were trying to get the descendants of node
C
, and only checked againstC
's left boundary, the query would return nodes that are siblings (F
andG
), and unrelated (H
,I
andJ
) toC
.我相信这只适用于二叉搜索树(BST),不一定适用于所有二叉树。
I believe it is only always true for Binary Search Trees (BST), not necessarily all Binary Trees.
如果您正在谈论嵌套集模型,那么您是错误的:
parent。左< child.left
和child.right <父.right
。因此,您可以通过查询左右 ID 之间的节点 ID 来获取该节点的所有子节点。
请参阅 此链接中的 Celko 的第二个查询:
If you are talking about the Nested Set Model, then you are incorrect:
parent.left < child.left
andchild.right < parent.right
.So, you can get all of a node's children by querying for node IDs between its left and right IDs.
See Celko's second query in this link: