有关红黑树删除的问题(Z有2个孩子)(固定前)

发布于 2025-01-22 12:00:53 字数 1785 浏览 0 评论 0 原文

代码源 -

    y = z;
    int y_original_color = y->color;
    if (z->left == TNULL) {
        x = z->right;
        rbTransplant(z, z->right);
    } else if (z->right == TNULL) {
        x = z->left;
        rbTransplant(z, z->left);
    } else {
        y = minimum(z->right);
        y_original_color = y->color;
        x = y->right;                            
        if (y->parent == z) {
            x->parent = y;                       \\ [1] Local Class TNull
        } else {
            rbTransplant(y, y->right);           \\ [2] Losing Y
            y->right = z->right;
            y->right->parent = y;
        }

        rbTransplant(z, y);
        y->left = z->left;
        y->left->parent = y;
        y->color = z->color;                     \\ [3] Need of Recoloring
    }

Questions

    1. 本地类tnull-(如果 y-> right tnull ),则TNULL是本地指针,简单地传递给 x x ; x 的更改 parent 也不更改本地 tnull
    2. 的父。
    1. 失去y-如果 z 的最低符号不是直接的孩子,则本节应执行。稍后将其放置在 z 的位置。此段不仅只有Pivot y-&gt; right/x 直到达到 z 的位置,而不是 y/minumim ?<?? /li>
    1. 需要重新上色-IIRC,重新上色也会在以后的 fixdelete()函数调用中发生,为什么需要此?

请忍受我,我对这种事情很慢,我真的处于智慧。这是我第一次在这里问。谢谢。

Code Source - https://github.com/Bibeknam/algorithmtutorprograms/blob/master/data-structures/red-black-trees/RedBlackTree.cpp

    y = z;
    int y_original_color = y->color;
    if (z->left == TNULL) {
        x = z->right;
        rbTransplant(z, z->right);
    } else if (z->right == TNULL) {
        x = z->left;
        rbTransplant(z, z->left);
    } else {
        y = minimum(z->right);
        y_original_color = y->color;
        x = y->right;                            
        if (y->parent == z) {
            x->parent = y;                       \\ [1] Local Class TNull
        } else {
            rbTransplant(y, y->right);           \\ [2] Losing Y
            y->right = z->right;
            y->right->parent = y;
        }

        rbTransplant(z, y);
        y->left = z->left;
        y->left->parent = y;
        y->color = z->color;                     \\ [3] Need of Recoloring
    }

Questions

    1. Local Class TNull - (In case y->right is a TNull) Within this class function, TNull is a local pointer simply passed to x; isn't changing the parent of x also change the parent of the local TNull?
    1. Losing Y - This section is meant to be executed in case the minimum in right subtree of z is not a direct children. Later it will be placed at z's location. Won't this segment only pivot y->right / x until it reaches z's location, instead of y / minimum?
    1. Need of Recoloring - Iirc, recoloring will also happen in the later fixDelete() function call, why is this needed?

Please bear with me, I'm slow in this kind of stuff and I'm really at my wits' end. This is the first time I'm asking here. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

ㄟ。诗瑗 2025-01-29 12:00:53

在您的问题上

  • 可能会发生,Tnull的父母是设定的,作者指出,这是他们利用的故意设计选择。
  • y正在移动到Z所在的位置,这只是修复Y,因此它的指针就是Z所拥有的。 S。
  • 从本质上讲,当要删除的节点有2个孩子时,您会发现继任者或前身节点(必须具有0或1个孩子)并交换节点。然后,您可以在连续或前身节点位置上删除节点。交换了前身或后继节点,保留树排序。但是重要的是在交换节点,颜色不换成,必须保留,因此红黑树的特性仍然正确。
    这就是y_original_color的目的。

On your questions

  • Yes that can happen, TNull's parent is set and the authors remark that this is a deliberate design choice which they exploit.
  • y is moving to where z is and this just fixes y so its pointers are what z had. s
  • No. Essentially when the node to be deleted has 2 children, you find the successor or predecessor node (which has to have 0 or 1 child) and swap the nodes. You then delete the node at the successor or predecessor node position. The predecessor or successor node swapped, preserves the tree ordering. But the important thing is in swapping the nodes, the colour is not swapped, it must be preserved so the red-black tree properties are still correct.
    This is what y_original_color is for.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文