【发布时间】:2015-12-21 14:46:47
【问题描述】:
有两个整数 x 和 7 是随机生成的整数。该程序使用红黑树成员函数插入将新值插入树中。
我不明白插入函数的参数,更具体地说是使用
(void*)x and (void*y)
这是main中的函数调用
rbt.rbtree_insert(t, (void*)x, (void*)y, compare_int);
这是定义的插入函数
void RBTree::rbtree_insert(rbtree t, void* key, void* value, compare_func compare)
{
node inserted_node = new_node(key, value, RED, NULL, NULL);
if (t->root == NULL)
{
t->root = inserted_node;
}
else
{
node n = t->root;
while (1)
{
int comp_result = compare(key, n->key);
if (comp_result == 0)
{
n->value = value;
return;
}
else if (comp_result < 0)
{
if (n->left == NULL)
{
n->left = inserted_node;
break;
}
else
{
n = n->left;
}
}
else
{
assert(comp_result > 0);
if (n->right == NULL)
{
n->right = inserted_node;
break;
}
else
{
n = n->right;
}
}
}
inserted_node->parent = n;
}
insert_case1(t, inserted_node);
verify_properties(t);
}
【问题讨论】:
-
我认为这不是您的代码。你不应该在 C++ 中使用
void*,因为我们有模板。你看到的叫explicit casting -
你有什么不明白的?它是一个存储
void*的通用树,它是常用的预模板,并且仍然在C 中。(如果该代码是通过将其包装在一层薄薄的C++ 类中而从C“移植”出来的,我不会感到惊讶。 )
标签: c++ tree void-pointers red-black-tree