【问题标题】:red-black tree invalid conversion红黑树无效转换
【发布时间】:2018-08-05 11:05:30
【问题描述】:

我无法通过红黑树的初始化。每当我编译时,都会出现以下错误。

redBlackTree.h:81:28: error: invalid conversion from ‘long int’ to ‘nodeColor’ [-fpermissive]
  nodeType<myType> *x = new nodeType<myType>;
                            ^
redBlackTree.h:10:8: error: invalid conversion from ‘long int’ to ‘nodeColor’ [-fpermissive]
 struct nodeType
        ^
redBlackTree.h:81:28: note: synthesized method ‘nodeType<int>::nodeType()’ first required here 
  nodeType<myType> *x = new nodeType<myType>;

我也无法搜索树以确保树中没有值。我可以想到的当前方法,但我仍然在我的树中得到重复的结果。

if(nd != NULL)
{
if(nd->keyValue == x)
    return true;

return ((search(x, nd->right)) || (search(x, nd->left)));
}

return false;

在插入函数下。这可能是我声明变量的方式吗?我们应该遵循某个模板,它说要使 x 成为新的 nodeType 但我不能。删除它会导致程序出现段错误。

#ifndef REDBLACKTREE_H
#define REDBLACKTREE_H
#include <iostream>
#include <sstream>
using namespace std;

enum nodeColor { RED, BLACK };

template<class myType>
struct nodeType
{
    myType  keyValue = keyValue;
    nodeColor color = NULL;
    nodeType *left = NULL;
    nodeType *right = NULL;
    nodeType *parent = NULL;
};

template <class myType> 
class redBlackTree
{

public:

    redBlackTree(){};
    ~redBlackTree();
    void destroyTree();
    unsigned int countNodes() const;
    unsigned int height() const;
    void printTree() const;
    void insert(myType);
    bool search(myType);

private:
    nodeType<myType> *root = NULL;
    bool search(myType, nodeType<myType> *);
    void destroyTree(nodeType<myType> *);
    unsigned int countNodes(nodeType<myType> *) const;
    unsigned int height(nodeType<myType> *) const;
    void printTree(nodeType<myType> *) const;
    void rightRotate(nodeType<myType> *);
    void leftRotate(nodeType<myType> *);

};

//template <class myType>
//redBlackTree<myType>::redBlackTree():root(NULL)
//{
//};
template <class myType> 
redBlackTree<myType>::~redBlackTree()
{
    destroyTree();
}
template <class myType>
void redBlackTree<myType>::destroyTree()
{
    destroyTree(root);
};
template <class myType>
unsigned int redBlackTree<myType>::countNodes() const
{
    return countNodes(root);
};
template <class myType>
unsigned int redBlackTree<myType>::height() const
{
    return height(root);
};
template <class myType>
void redBlackTree<myType>::printTree() const
{
    printTree(root);
};
template <class myType>
void redBlackTree<myType>::insert(myType value)
{
    if(search(value))
        return;

    nodeType<myType> *x = new nodeType<myType>;
    x->right = NULL;
    x->left = NULL;
    x->parent = NULL;
    x->keyValue = value;
    x->color = RED;

    if(root == NULL)
    {
        root = x;
        x->color = BLACK;
        return;
    }

    nodeType<myType> *curr = root;
    if(curr->keyValue > x->keyValue)
    {   
        curr->left = x;
        x->parent = curr;
        x->color = RED;
    }
    else
    {
        curr->right = x;
        x->parent = curr;
        x->color = RED;
    }

    while(x != root && x->parent->color == RED)
    {
        if(x->parent == x->parent->parent->left)
        {
            if(x->parent->parent->right != NULL && x->parent->parent->color == RED)
            {
                x->parent->color = BLACK;
                x->parent->parent->right->color = BLACK;
                x->parent->parent->color = RED;
                x = x->parent->parent;
            }
            else
            {
                if(x == x->parent->right)
                {
                    x = x->parent;
                    leftRotate(x);
                }

                x->parent->color = BLACK;
                x->parent->parent->color = RED;
                rightRotate(x->parent->parent);
            }
        }
        else
        {
            if(x->parent->parent->left != NULL && x->parent->parent->color == RED)
            {
                x->parent->color = BLACK;
                x->parent->parent->left->color = BLACK;
                x->parent->parent->color = RED;
                x = x->parent->parent;
            }
            else
            {
                if(x == x->parent->right)
                {
                    x = x->parent;
                    rightRotate(x);
                }

                x->parent->color = BLACK;
                x->parent->parent->color = RED;
                leftRotate(x->parent->parent);
            }
        }

        if(x == root)
            x->color = BLACK;
    }

};

template <class myType>
bool redBlackTree<myType>::search(myType x)
{
    search(x, root);
};

template <class myType>
bool redBlackTree<myType>::search(myType x, nodeType<myType> *nd)
{
    if(nd == NULL)
        return 0;

    search(x, nd->left);
    search(x, nd->right);

    if(nd->keyValue == x)
    return true;

    else
    return false;
};
template <class myType>
void redBlackTree<myType>::destroyTree(nodeType<myType> *node)
{
    if(node==NULL)
        return;

    destroyTree(node->left);
    destroyTree(node->right);
    delete node;    

};
template <class myType>
unsigned int redBlackTree<myType>::countNodes(nodeType<myType> *nd) const
{
    if(nd==NULL)
        return 0;

    return (countNodes(nd->left) + countNodes(nd->right) + 1);
};
template <class myType>
unsigned int redBlackTree<myType>::height(nodeType<myType> *nd) const
{
    int leftHeight = 0;
    int rightHeight = 0;
    if(nd==NULL)
        return 1;

    leftHeight = leftHeight + height(nd->left);
    rightHeight = rightHeight + height(nd->right);

    if(leftHeight == rightHeight)
        return leftHeight;
    else if(leftHeight > rightHeight)
        return leftHeight;
    else
        return rightHeight;
};
template <class myType>
void redBlackTree<myType>::printTree(nodeType<myType> *nd) const
{
    if(nd == NULL)
        return;

    printTree(nd->left);
    cout << nd->keyValue;
    printTree(nd->right);
    cout << nd->keyValue;
};
template <class myType>
void redBlackTree<myType>::rightRotate(nodeType<myType> *y)
{

    nodeType<myType> *x = y->left;
    x->left = x->right;

    if(x->right != NULL)
        x->right->parent = y;

    x->parent = y->parent;

    if(x->parent == NULL)
        root = x;
    else if(y == y->parent->left)
        y->parent->left = x;
    else
        y->parent->right = x;

    x->right = y;
    y->parent = x;

};
template <class myType>
void redBlackTree<myType>::leftRotate(nodeType<myType> *x)
{
    nodeType<myType> *y = y->right;
    x->right = y->left;

    if(x->right != NULL)
        root = y;
    else if(x == x->parent->left)
        x->parent->left = y;
    else
        x->parent->right = y;

    x->left = x;
    x->parent = y;

};

#endif

【问题讨论】:

    标签: c++ compiler-errors initialization red-black-tree red-black-tree-insertion


    【解决方案1】:

    问题是由以下行引起的:

    nodeColor color = NULL;
    

    NULL 不是 color 的有效值。使用有效的值,例如:

    nodeColor color = RED;
    

    还有一行

    myType  keyValue = keyValue;
    

    会是个问题。它用keyValue的未初始化值初始化keyValue。使该行的RHS更合适,例如:

    myType  keyValue = {};
    

    您在第一个 search 函数中缺少 return

    template <class myType>
    bool redBlackTree<myType>::search(myType x)
    {
       return search(x, root);
       // ^^
    }
    

    在成员函数定义的末尾不需要;。您在某些函数定义中拥有它们。您应该删除它们。

    【讨论】:

    • 谢谢。除非它是 c11 或更高版本,否则我仍然会收到有关它的警告。应该不是问题吧?我使用 c11 只是为了澄清。
    • @gamer1996,您在search 函数中缺少return
    • 再次感谢您。这似乎是我代码中其他地方的问题,因为我仍然得到重复。一旦我失业了,我将不得不深入了解一下。
    • 您还应该删除using namespace std;,并在调用cout 的两个地方简单地使用std::cout。没有理由将整个 namespace std 包含在标题中(以及随后包含它的每个文件中)
    猜你喜欢
    • 2016-03-18
    • 2019-07-09
    • 2012-10-11
    • 2016-06-27
    • 2023-03-03
    • 2010-09-06
    • 2013-10-13
    • 2018-06-30
    • 2018-11-07
    相关资源
    最近更新 更多