C++ 红黑树实现
我实现了一个红黑树,然后在其中插入C++ 红黑树实现,c++,algorithm,data-structures,tree,C++,Algorithm,Data Structures,Tree,我实现了一个红黑树,然后在其中插入1,2,3,4,5,6,7,8,9,10。但我的树似乎不平衡,因为预顺序遍历看起来是这样的:4,2,1,3,6,5,8,7,9,10,顺序遍历:1,2,3,4,5,6,7,8,9,10。这意味着根是4,树是不平衡的!这是我的密码 void RedBlackTree::leftRotate(RedBlackTreeNode * x){ RedBlackTreeNode *y = x->right; //set y y = x->righ
1,2,3,4,5,6,7,8,9,10
。但我的树似乎不平衡,因为预顺序遍历看起来是这样的:4,2,1,3,6,5,8,7,9,10
,顺序遍历:1,2,3,4,5,6,7,8,9,10
。这意味着根是4
,树是不平衡的!这是我的密码
void RedBlackTree::leftRotate(RedBlackTreeNode * x){
RedBlackTreeNode *y = x->right; //set y
y = x->right;
x->right = y->left;
if (y->left != nilLeaf)
y->left->p = x;
y->p = x->p;
if (x->p == nilLeaf)
root = y;
else if (x == x->p->left)
x->p->left = y;
else x->p->right = y;
y->left = x;
x->p = y;
}
void RedBlackTree::rightRotate(RedBlackTreeNode * x){
RedBlackTreeNode *y = x->left; //set y
y = x->left;
x->left = y->right;
if (y->right != nilLeaf)
y->right->p = x;
y->p = x->p;
if (x->p == nilLeaf)
root = y;
else if (x == x->p->right)
x->p->right = y;
else x->p->left = y;
y->right = x;
x->p = y;
}
void RedBlackTree::insert(const Point &newItem){
size++;
if (empty){
root->key = newItem;
empty = false;
return;
}
RedBlackTreeNode * z = new RedBlackTreeNode;
z->key = newItem;
z->right = z->left = z->p = nilLeaf;
RedBlackTreeNode *y = nilLeaf;// = new RedBlackTreeNode;
RedBlackTreeNode *x = root;// = new RedBlackTreeNode;
while (x != nilLeaf)
{
y = x;
if (z->key < x->key)
x = x->left;
else
x = x->right;
}
z->p = y;
if (y == nilLeaf)
root = z;
else if (z->key < y->key)
y->left = z;
else
y->right = z;
z->left = nilLeaf;
z->right = nilLeaf;
z->color = RedBlackTreeNode::Red;
insertFixUp(z);
}
void RedBlackTree::insertFixUp(RedBlackTreeNode* z){
while (z->p->color == RedBlackTreeNode::Red)
{
if (z->p == z->p->p->left)
{
RedBlackTreeNode* y = z->p->p->right;
if (y->color == RedBlackTreeNode::Red)
{
z->p->color = RedBlackTreeNode::Black;
y->color = RedBlackTreeNode::Black;
z->p->p->color = RedBlackTreeNode::Red;
z = z->p->p;
}
else if (z == z->p->right)
{
z = z->p;
leftRotate(z);
}
else{
z->p->color = RedBlackTreeNode::Black;
z->p->p->color = RedBlackTreeNode::Red;
rightRotate(z->p->p);
}
}
else if (z->p == z->p->p->right)
{
RedBlackTreeNode* y = z->p->p->left;
if (y->color == RedBlackTreeNode::Red)
{
z->p->color = RedBlackTreeNode::Black;
y->color = RedBlackTreeNode::Black;
z->p->p->color = RedBlackTreeNode::Red;
z = z->p->p;
}
else if (z == z->p->left)
{
z = z->p;
rightRotate(z); //**
}
else{
z->p->color = RedBlackTreeNode::Black;
z->p->p->color = RedBlackTreeNode::Red;
leftRotate(z->p->p); //**
}
}
}
root->color = RedBlackTreeNode::Black;
}
void RedBlackTree::leftRotate(RedBlackTreeNode*x){
RedBlackTreeNode*y=x->right;//设置y
y=x->右;
x->右=y->左;
如果(y->左!=nilLeaf)
y->左->p=x;
y->p=x->p;
如果(x->p==nilliaf)
根=y;
如果(x==x->p->左)
x->p->left=y;
否则x->p->right=y;
y->左=x;
x->p=y;
}
void RedBlackTree::rightRotate(RedBlackTreeNode*x){
RedBlackTreeNode*y=x->left;//设置y
y=x->左;
x->左=y->右;
如果(y->右!=nilLeaf)
y->右->p=x;
y->p=x->p;
如果(x->p==nilliaf)
根=y;
如果(x==x->p->right),则为else
x->p->右=y;
否则x->p->left=y;
y->右=x;
x->p=y;
}
void RedBlackTree::insert(常量点和newItem){
大小++;
if(空){
root->key=newItem;
空=假;
返回;
}
RedBlackTreeNode*z=新的RedBlackTreeNode;
z->key=newItem;
z->right=z->left=z->p=nilliaf;
RedBlackTreeNode*y=nilLeaf;//=新的RedBlackTreeNode;
RedBlackTreeNode*x=根;//=新的RedBlackTreeNode;
while(x!=nilliaf)
{
y=x;
如果(z->keykey)
x=x->左;
其他的
x=x->右;
}
z->p=y;
如果(y==nilliaf)
根=z;
否则如果(z->keykey)
y->左=z;
其他的
y->右=z;
z->左=零叶;
z->右=零叶;
z->color=RedBlackTreeNode::Red;
插入固定(z);
}
void RedBlackTree::insertFixUp(RedBlackTreeNode*z){
而(z->p->color==RedBlackTreeNode::Red)
{
如果(z->p==z->p->p->left)
{
红黑树节点*y=z->p->p->右;
如果(y->color==RedBlackTreeNode::Red)
{
z->p->color=RedBlackTreeNode::Black;
y->color=RedBlackTreeNode::Black;
z->p->p->color=RedBlackTreeNode::Red;
z=z->p->p;
}
否则如果(z==z->p->right)
{
z=z->p;
左旋转(z);
}
否则{
z->p->color=RedBlackTreeNode::Black;
z->p->p->color=RedBlackTreeNode::Red;
右旋转(z->p->p);
}
}
否则如果(z->p==z->p->p->右侧)
{
红黑树节点*y=z->p->p->左;
如果(y->color==RedBlackTreeNode::Red)
{
z->p->color=RedBlackTreeNode::Black;
y->color=RedBlackTreeNode::Black;
z->p->p->color=RedBlackTreeNode::Red;
z=z->p->p;
}
否则如果(z==z->p->左)
{
z=z->p;
右旋转(z)//**
}
否则{
z->p->color=RedBlackTreeNode::Black;
z->p->p->color=RedBlackTreeNode::Red;
左旋转(z->p->p)//**
}
}
}
根->颜色=RedBlackTreeNode::黑色;
}
但是我很确定,insertFixUp
有问题。因为此代码在大多数示例中都可以正常工作,但在某些情况下(如上面的示例),节点高度之间的差异将大于2
编辑:如果我在其中插入一些随机数,这段代码就可以正常工作,当我在其中插入排序的数字时就会出现问题 此代码没有问题。根据: '这些约束强制执行红黑树的一个关键特性:从根到最远叶子的路径长度不超过从根到最近叶子路径的两倍。结果是,这棵树的高度大致平衡'
所以4作为根没有什么错。尝试在树中插入更多数字,您将看到,根目录将发生更改,并且所述属性将始终保持不变。您是否使用调试器检查了代码以了解发生了什么情况?不幸的是,我不知道如何使用MSVS调试器。Iv尝试使用cerr打印每个州,但我找不到问题@NathanOliver@MoNo总有一天你会学会的,我建议你现在就去学。尝试放置断点并按f10/f11几次。尝试悬停一些变量名以查看其值。确保在调试模式下编译!看一看。学习如何逐步完成代码是一项非常重要的编程技能。我认为您的实现很好。看看一棵红黑相间的树。尝试添加示例中的数字,根将为4。如果您仍然认为您的实现有错误,请在问题中添加更多细节(至少是
RedBlackTree
class的完整代码)。