我是 c# 的新手,我想了解一些非常基础的东西。
我想实现一个 RBTree 和一个链表,所以我创建:
public class RBTreeNode
{
// PROPERTIES
public RBTreeNode left;
public RBTreeNode right;
public RBTreeNode parent;
public String Color;
public Int Key;
}
List<RBTreeNode> deleteList = new List<RBTreeNode>();
在我的程序运行期间,我遍历树,获取一些节点(取决于值),并将它们添加到 deleteList。
我无法下定决心的事情是,例如,如果我有 RBTreeNode X,并且我有 X.left = 空。我想要发生的事情是使 X 的左 child 无效(不影响 X.left RBTreeNode ,但我认为这里会发生的是 X.left 确实会变为空,但 X.left RBTreeNode 对象也将变为空。这是不是我想要的。(如果它被复制到 deleteList ,它将变为 null,这是错误的)
我希望能够使用指针完成与 C++ 相同的操作。 在 C++ 中,我可能会这样做:
public class RBTreeNode
{
// PROPERTIES
public RBTreeNode* left;
public RBTreeNode* right;
public RBTreeNode* parent;
public std::string Color;
public int Key;
}
例如:
RBTreeNode* tenp = X->left;
X->left = null; //(i think in c# it will turn temp here to null to - of course without pointers)
temp->right = &X;
任何提示/解释?
最佳答案
不,你很好。 X.left
只是一个变量。将它设置为 null 只是将该变量的值设置为 null,它对它过去引用的对象没有任何作用。
在 C# 中确实没有将对象设置为 null 这样的概念。
我有一篇关于 value and reference types in C# 的文章您可能会觉得有用。
关于c# - c# 中的链表和红/黑树 - 引用问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2324325/