【发布时间】:2012-11-16 01:49:30
【问题描述】:
当我尝试使用下面第 15 行中的指针变量 *temp 创建一个新的 Node 对象时,我遇到了分段错误。我对 c++ 以及双指针的工作方式仍然很陌生,尤其是与 & 结合使用时。感谢您的帮助。
void bst::insert(int n) {
Node **temp;
Node *r, *parent;
// Tree is empty
if (root == NULL) {
root = new Node;
root->parent = NULL;
root->value = n;
root->left = NULL;
root->right = NULL;
root->isBlack = true;
} else {
r = root;
// Create a node with the given value, n
(*temp) = new Node;
(*temp)->value = n;
(*temp)->left = NULL;
(*temp)->right = NULL;
(*temp)->isBlack = false;
【问题讨论】:
-
为什么需要双指针?
-
我使用双指针作为我的旋转和平衡方法的参数;这是一棵红黑二叉搜索树
标签: c++ pointers segmentation-fault