有两个整数x和7是随机生成的整数。该程序使用红黑树成员函数插入将新值插入树中。红色黑树中的虚空指针
我不明白插入函数的参数,更具体的使用
(void*)x and (void*y)
下面是主要
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](http://en.cppreference.com/w/cpp/language/explicit_cast) – NathanOliver
你不明白的是什么?它是一个存储'void *'的通用树,这是模板前的常见做法,仍然在C中。(我不会感到惊讶,如果代码已经通过将它包装到C++类的一个薄层中而从C中“移植” 。) – molbdnilo