C++ C++;-尝试创建一个指向一个节点内(由另一个节点指向)的指针,对吗? #包括 使用名称空间std; 枚举颜色{黑色,红色}; 结构节点{ 公众: int键; 颜色; 节点*左、*右、*父节点; }; 类RBT{ 公众: 节点零; node*root=新节点; RBT(){ 无颜色=黑色; 根=&nil; } 左旋转无效(节点*x){ 节点*y=x->右侧; 如果(y->left==&nil){} } }; int main() { RBT; 库特
您可能需要查找C++ C++;-尝试创建一个指向一个节点内(由另一个节点指向)的指针,对吗? #包括 使用名称空间std; 枚举颜色{黑色,红色}; 结构节点{ 公众: int键; 颜色; 节点*左、*右、*父节点; }; 类RBT{ 公众: 节点零; node*root=新节点; RBT(){ 无颜色=黑色; 根=&nil; } 左旋转无效(节点*x){ 节点*y=x->右侧; 如果(y->left==&nil){} } }; int main() { RBT; 库特,c++,C++,您可能需要查找std::unique\u ptr和std::shared\u ptr 而且 如果我想让x.right指向y.left,我该怎么做呢?但是当我已经有的代码不起作用的时候,继续写更多的代码是没有意义的。没错!你可能会惊讶于有那么多人奋力向前,最终调试了他们本来不应该在丢弃代码之前写几个小时的代码。@user4581301-我几乎每10-20行编译一次,这已经成为我的一种习惯。我仍然会做恶梦,在学校的晚上熬夜到凌晨3-4点,试图修复我一次编写的代码,中间没有测试。甚至没有什么奇怪的地方
std::unique\u ptr
和std::shared\u ptr
而且
如果我想让x.right指向y.left,我该怎么做呢?但是当我已经有的代码不起作用的时候,继续写更多的代码是没有意义的。没错!你可能会惊讶于有那么多人奋力向前,最终调试了他们本来不应该在丢弃代码之前写几个小时的代码。@user4581301-我几乎每10-20行编译一次,这已经成为我的一种习惯。我仍然会做恶梦,在学校的晚上熬夜到凌晨3-4点,试图修复我一次编写的代码,中间没有测试。甚至没有什么奇怪的地方。这是推荐的行为。@Swardfish-谢谢!这有点奇怪添加
node*parent=&nil
对我帮助最大。处理简单的指针对我来说很容易,但当你开始将它与具有指向更多指针成员的指针成员的类和结构结合起来时……它会让我感到困惑,我将把std::unique_ptr
留到下一次,因为我有enoug理解这里的内容是一个问题,但我一定会确保稍后查看
#include<iostream>
using namespace std;
enum Color { black, red };
struct node {
public:
int key;
Color color;
node *left, *right, *parent;
};
class RBT {
public:
node nil;
node *root = new node;
RBT() {
nil.color = black;
root = &nil;
}
void left_rotate(node *x) {
node *y = x->right;
if (y->left == &nil) {}
}
};
int main()
{
RBT t;
cout << "t color is: " << t.root->color;
}
enum class Color { black, red };
struct node {
static node nil; // so nil can be used in the constructor of node
int key;
Color color;
node *parent;
node *left;
node *right;
// Use this constructor for new nodes. Parameters you don't provide
// have defaults.
node(int key = 0, Color color = Color::black,
node *parent = &nil, node *left = &nil, node *right = &nil)
: key{ key }, color{ color }, parent{ parent }, left{ left }, right{ right }
{}
};
node node::nil;
struct RBT {
node root; // please, no new without purpose
void left_rotate(node *x) {
node *y = x->right;
if (y->left == &node::nil) {
// whatever
}
}
};
cout << "t color is: " << t.root->color;
std::ostream& operator<<(std::ostream &os, Color const &color)
{
if(color == Color::black)
return os << "black";
return os << "red";
}