有兩個整數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