答复: [PATCH] rbtree: stop iteration early in rb_find_first

From: Li,Rongqing
Date: Wed Aug 25 2021 - 12:08:11 EST




>>
>>
>>                          10
>>                        /
>>                       5
>>                        \
>>                         10
>>
>> The search would stop after visiting node 5, and miss the leaf which
>> is the expected node to be returned.

thanks for explanation.

> Just to clarify; the current code *does* work here. The proposed patch
> breaks it.

true, my patch is wrong.

but rb_find_first seems have other issue.  when the key is equal, we should search right leaf, not left leaf
rb_find_first should like below

while (node) {
int c = cmp(key, node);

if (c < 0) {
node = node->rb_left;
} else {
if (!c)
match = node;
node = node->rb_right;
}
}
 
since the node  is inserted to right when the key is equal, see rb_add()

-Li