在c++中更新每个节点红黑树的大小

问题描述 投票:0回答:0

所以我概述了 RB-Tree 插入过程以及一些用于插入的函数。现在节点已正确插入,但旋转和修复后每个节点的大小(左右节点总数)未正确更新。在哪里更新节点大小以反映旋转和修复后的当前大小?感谢您对这个问题的帮助。我添加了一些行来更新尺寸,但它们显然是错误的。

`Node* insertBST(Node *root, Node *ptr) { 如果(根== NULL) 返回指针;

        if (ptr->key < root->key) {
            root->left = insertBST(root->left, ptr);
            root->left->parent = root;
        } else if (ptr->key > root->key) {
            root->right = insertBST(root->right, ptr);
            root->right->parent = root;
        }
        return root;
    }

    void fixInsertRBTree(Node *ptr) {
        Node *parent = NULL;
        Node *grandparent = NULL;
        while (ptr != root && getColor(ptr) == RED && getColor(ptr->parent) == RED) {
            parent = ptr->parent;
            grandparent = parent->parent;
            if (parent == grandparent->left) {
                Node *uncle = grandparent->right;
                if (getColor(uncle) == RED) {
                    setColor(uncle, BLACK);
                    setColor(parent, BLACK);
                    setColor(grandparent, RED);
                    ptr = grandparent;
                } else {
                    if (ptr == parent->right) {
                        rotateLeft(parent);
                        ptr = parent;
                        parent = ptr->parent;
                    }
                    rotateRight(grandparent);
                    swap(parent->color, grandparent->color);
                    ptr = parent;
                }
            } else {
                Node *uncle = grandparent->left;
                if (getColor(uncle) == RED) {
                    setColor(uncle, BLACK);
                    setColor(parent, BLACK);
                    setColor(grandparent, RED);
                    ptr = grandparent;
                } else {
                    if (ptr == parent->left) {
                        rotateRight(parent);
                        ptr = parent;
                        parent = ptr->parent;
                    }
                    rotateLeft(grandparent);
                    swap(parent->color, grandparent->color);
                    ptr = parent;
                }
            }
            
        parent->size = size(parent->left) + size(parent->right) + 1;
        grandparent->size = size(grandparent->left) + size(grandparent->right) + 1;
        ptr->size = size(ptr->left) + size(ptr->right) + 1;
        }
        setColor(root, BLACK);

        root->size = size(root->left) + size(root->right) + 1;


    }


    void Insertion(keytype k, valuetype v) {
        Node *node = newNode(k, v);
        root = insertBST(root, node);
        fixInsertRBTree(node);
    }

    void rotateLeft(Node *ptr) {
        Node *right_child = ptr->right;
        ptr->right = right_child->left;

        if (ptr->right != NULL)
            ptr->right->parent = ptr;

        right_child->parent = ptr->parent;

        if (ptr->parent == NULL)
            root = right_child;
        else if (ptr == ptr->parent->left)
            ptr->parent->left = right_child;
        else
            ptr->parent->right = right_child;

        right_child->left = ptr;
        ptr->parent = right_child;
        right_child->size = size(right_child->left) + size(right_child->right) + 1;
        ptr->size = size(ptr->left) + size(ptr->right) + 1;
    }

    void rotateRight(Node *ptr) {
        Node *left_child = ptr->left;
        ptr->left = left_child->right;

        if (ptr->left != NULL)
            ptr->left->parent = ptr;

        left_child->parent = ptr->parent;

        if (ptr->parent == NULL)
            root = left_child;
        else if (ptr == ptr->parent->left)
            ptr->parent->left = left_child;
        else
            ptr->parent->right = left_child;

        left_child->right = ptr;
        ptr->parent = left_child;
        left_child->size = size(left_child->left) + size(left_child->right) + 1;
        ptr->size = size(ptr->left) + size(ptr->right) + 1;
    }

`

红黑树中每个节点的插入都有效,但更新大小无效。因此,考虑到每个节点都有一个大小属性,我应该添加什么来准确更新每个节点的大小?

c++ c++17 c++14 red-black-tree red-black-tree-insertion
© www.soinside.com 2019 - 2024. All rights reserved.