“权利”的目的是什么?嵌套树中的值?

发布于 2024-09-18 23:14:43 字数 179 浏览 1 评论 0原文

如果我正在寻找父节点的嵌套节点,我会像这样查询子节点:

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 技术交流群。

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

发布评论

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

评论(3

滴情不沾 2024-09-25 23:14:43

在嵌套集中,层次结构是通过指定每个节点的左边界和右边界来描述的,并且所有后代节点都落在这些边界内。假设您的层次结构如下所示

A
 - B
 - C
    - D
    - E
 - F
 - G
H
I
 - J

当将每一行添加到嵌套集中时,您最终会得到一个如下所示的表:

+----+--------+-----+-----+-----------+
| id | value  | lft | rgt | parent_id |
+----+--------+-----+-----+-----------+
|  1 | A      |   1 |  14 |      NULL |
|  2 | B      |   2 |   3 |         1 |
|  3 | C      |   4 |   9 |         1 |
|  4 | D      |   5 |   6 |         3 |
|  5 | E      |   7 |   8 |         3 |
|  6 | F      |  10 |  11 |         1 |
|  7 | G      |  12 |  13 |         1 |
|  8 | H      |  15 |  16 |      NULL |
|  9 | I      |  17 |  20 |      NULL |
| 10 | J      |  18 |  19 |         9 |
+----+--------+-----+-----+-----------+

或者以另一种方式看待它:

        -D- -E-
  -B- -----C----- --F-- --G--             --J--
---------------A---------------- --H-- -----I-----
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

当您想要查询节点的所有后代时(比方说A),你可以像这样查询:

SELECT *
FROM table AS node
JOIN table AS parent
  ON parent.id = 1
WHERE node.lft BETWEEN parent.lft AND parent.rgt -- BETWEEN 1 AND 14
ORDER BY lft

由于每个节点的左边界都必须小于其右边界,因此你不需要检查节点的右边界,而只需搜索落在的节点在父边界内(因此只需要右边界来确定集合的末尾在哪里)。例如,如果您尝试获取节点 C 的后代,并且仅检查 C 的左边界,则查询将返回兄弟节点 (<代码>F和G),以及与<无关的(HIJ)代码>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

A
 - B
 - C
    - D
    - E
 - F
 - G
H
I
 - J

When each row is added to your nested set, you'd end up with a table that looks like:

+----+--------+-----+-----+-----------+
| id | value  | lft | rgt | parent_id |
+----+--------+-----+-----+-----------+
|  1 | A      |   1 |  14 |      NULL |
|  2 | B      |   2 |   3 |         1 |
|  3 | C      |   4 |   9 |         1 |
|  4 | D      |   5 |   6 |         3 |
|  5 | E      |   7 |   8 |         3 |
|  6 | F      |  10 |  11 |         1 |
|  7 | G      |  12 |  13 |         1 |
|  8 | H      |  15 |  16 |      NULL |
|  9 | I      |  17 |  20 |      NULL |
| 10 | J      |  18 |  19 |         9 |
+----+--------+-----+-----+-----------+

Or to look at it another way:

        -D- -E-
  -B- -----C----- --F-- --G--             --J--
---------------A---------------- --H-- -----I-----
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

When you want to query for all descendants of a node (let's say A), you'd query like so:

SELECT *
FROM table AS node
JOIN table AS parent
  ON parent.id = 1
WHERE node.lft BETWEEN parent.lft AND parent.rgt -- BETWEEN 1 AND 14
ORDER BY lft

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 against C's left boundary, the query would return nodes that are siblings (F and G), and unrelated (H, I and J) to C.

清风不识月 2024-09-25 23:14:43

我相信这只适用于二叉搜索树(BST),不一定适用于所有二叉树。

I believe it is only always true for Binary Search Trees (BST), not necessarily all Binary Trees.

怪异←思 2024-09-25 23:14:43

如果您正在谈论嵌套集模型,那么您是错误的:parent。左< child.leftchild.right <父.right

因此,您可以通过查询左右 ID 之间的节点 ID 来获取该节点的所有子节点。

请参阅 此链接中的 Celko 的第二个查询:

SELECT P2.*
FROM Personnel AS P1, Personnel AS P2
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
AND P2.emp = :myemployee;

If you are talking about the Nested Set Model, then you are incorrect: parent.left < child.left and child.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:

SELECT P2.*
FROM Personnel AS P1, Personnel AS P2
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
AND P2.emp = :myemployee;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文