C++ 红黑树无效转换
我无法通过红黑树的初始化。无论何时编译,都会出现以下错误C++ 红黑树无效转换,c++,compiler-errors,initialization,red-black-tree,red-black-tree-insertion,C++,Compiler Errors,Initialization,Red Black Tree,Red Black Tree Insertion,我无法通过红黑树的初始化。无论何时编译,都会出现以下错误 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 con
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>;
它在插入功能下。这可能是我声明变量的方式吗?我们应该遵循一个特定的模板,它说让x成为一个新的节点类型,但我不能。删除它会导致程序出现seg故障
#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
\ifndef REDBLACKTREE\u H
#定义红黑树
#包括
#包括
使用名称空间std;
枚举节点颜色{红色,黑色};
模板
结构节点类型
{
myType keyValue=keyValue;
nodeColor颜色=空;
nodeType*left=NULL;
nodeType*right=NULL;
节点类型*parent=NULL;
};
模板
类红黑树
{
公众:
红黑树(){};
~redBlackTree();
void树();
unsigned int countNodes()常量;
无符号整数高度()常量;
void printree()常量;
空白插入(myType);
布尔搜索(myType);
私人:
nodeType*root=NULL;
bool搜索(myType,nodeType*);
树(节点类型*);
无符号整数countNodes(nodeType*)常量;
无符号整数高度(节点类型*)常量;
无效打印树(节点类型*)常量;
void rightRotate(节点类型*);
void leftRotate(节点类型*);
};
//模板
//redBlackTree::redBlackTree():根(NULL)
//{
//};
模板
redBlackTree::~redBlackTree()
{
破坏树();
}
模板
void redBlackTree::destroyTree()
{
树(根);
};
模板
unsigned int redBlackTree::countNodes()常量
{
返回countNodes(root);
};
模板
无符号整型redBlackTree::height()常量
{
返回高度(根);
};
模板
void redBlackTree::printTree()常量
{
打印树(根);
};
模板
void redBlackTree::insert(myType值)
{
如果(搜索(值))
返回;
节点类型*x=新节点类型;
x->right=NULL;
x->left=NULL;
x->parent=NULL;
x->keyValue=value;
x->颜色=红色;
if(root==NULL)
{
根=x;
x->颜色=黑色;
返回;
}
节点类型*curr=root;
if(curr->keyValue>x->keyValue)
{
当前->左=x;
x->parent=curr;
x->颜色=红色;
}
其他的
{
当前->右=x;
x->parent=curr;
x->颜色=红色;
}
而(x!=根和x->父->颜色==红色)
{
如果(x->parent==x->parent->parent->left)
{
如果(x->parent->parent->right!=NULL&&x->parent->parent->color==RED)
{
x->父->颜色=黑色;
x->parent->parent->right->color=黑色;
x->parent->parent->color=红色;
x=x->父->父;
}
其他的
{
如果(x==x->父级->右侧)
{
x=x->父级;
左旋转(x);
}
x->父->颜色=黑色;
x->parent->parent->color=红色;
右旋转(x->parent->parent);
}
}
其他的
{
如果(x->parent->parent->left!=NULL&&x->parent->parent->color==RED)
{
x->父->颜色=黑色;
x->parent->parent->left->color=黑色;
x->parent->parent->color=红色;
x=x->父->父;
}
其他的
{
如果(x==x->父级->右侧)
{
x=x->父级;
右旋转(x);
}
x->父->颜色=黑色;
x->parent->parent->color=红色;
左旋转(x->父对象->父对象);
}
}
if(x==根)
x->颜色=黑色;
}
};
模板
布尔红黑树::搜索(myType x)
{
搜索(x,根);
};
模板
bool redBlackTree::search(myType x,nodeType*nd)
{
如果(nd==NULL)
返回0;
搜索(x,nd->左);
搜索(x,nd->右);
如果(nd->keyValue==x)
返回true;
其他的
返回false;
};
模板
void redBlackTree::destroyTree(节点类型*节点)
{
if(node==NULL)
返回;
破坏树(节点->左侧);
破坏树(节点->右侧);
删除节点;
};
模板
unsigned int redBlackTree::countNodes(nodeType*nd)const
{
如果(nd==NULL)
返回0;
返回(countNodes(nd->left)+countNodes(nd->right)+1);
};
模板
无符号整型redBlackTree::高度(节点类型*nd)常数
{
int leftHeight=0;
int rightHeight=0;
如果(nd==NULL)
返回1;
leftHeight=leftHeight+height(nd->left);
rightHeight=rightHeight+高度(nd->right);
如果(leftHeight==rightHeight)
返回左高;
否则如果(leftHeight>rightHeight)
返回左高;
其他的
返回正确高度;
};
模板
void redBlackTree::printTree(节点类型*nd)常量
{
如果(nd==NULL)
返回;
打印树(nd->左);
cout键值;
打印树(nd->右侧);
cout键值;
};
模板
void redBlackTree::rightRotate(节点类型*y)
{
节点类型*x=y->左侧;
x->左=x->右;
如果(x->右!=NULL)
x->right->parent=y;
x->parent=y->parent;
如果(x->parent==NULL)
根=x;
如果(y==y->父项->左项),则为else
y->父->左=x;
其他的
y->parent->right=x;
x->右=y;
y->parent=x;
};
模板
void redBlackTree::leftRotate(节点类型*x)
{
节点类型*y=y->右侧;
x->右=y->左;
如果(x->右!=NULL)
根=y;
否则如果(x==x->父级->左)
x->父->左=y;
其他的
x->parent->right=y;
x->左=x;
x->parent=y;
};
#恩迪夫
问题是由以下线路引起的:
nodeColor color = NULL;
NULL
不是color
的有效值。使用有效的值,例如:
nodeColor color = RED;
myType keyValue = {};
还有,线路
myType keyValue = keyValue;
这将是一个问题。它使用未初始化的keyValue
值初始化keyValue
。使该行的RHS为s
myType keyValue = {};
template <class myType>
bool redBlackTree<myType>::search(myType x)
{
return search(x, root);
// ^^
}