用unique_ptr实现的二进制搜索树中的迭代findMin()

时间:2020-02-08 12:30:08

标签: c++ binary-search-tree unique-ptr

我已经使用通常的unique_ptr实现了BST:

class BinarySearchTree
{
public:
struct Node {
    Node(int k)
    {
        key = k;
        left = nullptr;
        right = nullptr;
    }
    int key;
    // This will auto-destroy all child nodes when this Node is destroyed
    std::unique_ptr<Node> left;
    std::unique_ptr<Node> right;
};

BinarySearchTree():
      m_root(nullptr)
    , m_depth(0)
{}

void deleteKey(int key)
{
    deleteKeyRec(key, m_root);
}

void deleteKeyRec(int key, std::unique_ptr<Node>& node)
{
    if (key == node->key) {
        // This node needs to be deleted and replaced with one of its children
        if (node->left == nullptr && node->right == nullptr) {
            // Node to be deleted has no children
            node = nullptr;
        }
        else if (node->left != nullptr && node->right == nullptr) {
            // Only left child present
            node = std::move(node->left);
        }
        else if (node->left == nullptr && node->right != nullptr) {
            // Only right child present
            node = std::move(node->right);
        }
        else {
            // Both children present, find minimum node in left subtree and replace
            auto minNode = findMin(node->left);
            node->key = minNode->key;
            deleteKeyRec(minNode->key, minNode);                
        }
    }
    else if (key < node->key) {
        deleteKeyRec(key, node->left);
    }
    else if (key > node->key) {
        deleteKeyRec(key, node->right);
    }
}

std::unique_ptr<Node> findMin(std::unique_ptr<Node> & node)
{
    Node *current = node.get();
    while (current->left) {
        current = current->left.get();
    }
    return std::make_unique<Node>(current);
}

private:
    std::unique_ptr<Node> m_root;   // Clean-up all children when this object is destroyed
    int m_depth;

};

我正在尝试迭代实现findMin(),并使其返回unique_ptr&,因此我可以在deleteKeyRec()中使用它来删除最小节点。但是似乎我无法从findMin()返回unique_ptr&。还有另一种方法来迭代实现findMin()吗?

MSVC的特定错误在findMin()的以下行中: 返回std :: make_unique(current);

错误C2664'BinarySearchTree :: Node :: Node(BinarySearchTree :: Node &&)':无法将参数1从'BinarySearchTree :: Node *'转换为'int'

似乎我不应该使用当前的Node *创建新的unique_ptr。但是那我该如何遍历树呢?


修改后的代码,内含最后一个案例的逻辑:

void deleteKeyRec(int key, std::unique_ptr<Node>& node)
{
    if (key == node->key) {
        // This node needs to be deleted and replaced with one of its children
        if (node->left == nullptr && node->right == nullptr) {
            // Node to be deleted has no children
            node = nullptr;
        }
        else if (node->left != nullptr && node->right == nullptr) {
            // Only left child present
            node = std::move(node->left);
        }
        else if (node->left == nullptr && node->right != nullptr) {
            // Only right child present
            node = std::move(node->right);
        }
        else {
            // Both children present, find minimum node in right subtree and replace 'node'
            Node *owner = nullptr;         // stays one step behind current after first iteration
            Node *current = node->right.get();
            while (current->left.get()) {
                owner = current;
                current = current->left.get();
            }

            //auto minNode = findMin(node->left.get());
            node->key = current->key;      // transfer the key here, we do not free the memory of this node.
            if (owner == nullptr) {
                // The right node of 'node' is the correct replacement as it has no children
                node->right = nullptr;  // delete the node from which key was copied
            }
            else {
                // A left node was found when traversing the right subtree of 'node', so that node's owner now will have no left child!
                // This left child thats being deleted has already had its key copied to 'node' via 'current'
                owner->left = nullptr;   // delete the node from which key was copied
            }
        }
    }
    else if (key < node->key) {
        deleteKeyRec(key, node->left);
    }
    else if (key > node->key) {
        deleteKeyRec(key, node->right);
    }
}

1 个答案:

答案 0 :(得分:1)

find函数应将引用/指针/迭代器返回到找到的元素。它根本不应该返回拥有的指针。同样,它不应该将拥有的指针作为参数。它应该将引用/指针/迭代器带到一个节点上。

每个对象/节点必须完全由一个 std::unique_ptr拥有。因此,如果您不打算从树中删除找到的节点,则不应将其返回为std::unique_ptr

例如:

Node& findMin(const Node& node)
{
    Node *current = node.get();
    while (current->left) {
        current = current->left.get();
    }
    return *current;
}

您对deleteKeyRec有相同的问题。该参数不应为std::unique_ptr

相关问题