gpt4 book ai didi

c++ - 红黑树无效转换

转载 作者:行者123 更新时间:2023-11-28 01:36:48 24 4
gpt4 key购买 nike

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

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

最佳答案

问题出在线路上:

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);
// ^^
}

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

关于c++ - 红黑树无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48980926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com