Skip to content

Commit 066602e

Browse files
committed
Refine rbtree comment for clarity and accuracy
The comment now explains that each node requires at least one byte of storage and contrasts the theoretical maximum node count with the practical depth bound of 2 * log₂(n). The rationale for using a fixed-size array to track the tree path during operations is clearly described.
1 parent 92fd789 commit 066602e

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/map.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ struct map_internal {
3030
map_cmp_t (*comparator)(const void *, const void *);
3131
};
3232

33-
/* Each node in the red-black tree consumes at least 1 byte of space (for the
34-
* linkage if nothing else), so there are a maximum of sizeof(void *) << 3
35-
* red-black tree nodes in any process (and thus, at most sizeof(void *) << 3
36-
* nodes in any red-black tree). The choice of algorithm bounds the depth of
37-
* a tree to twice the binary logarithm (base 2) of the number of elements in
38-
* the tree; the following bound applies.
33+
/* Each red–black tree node requires at least one byte of storage (for its
34+
* linkage). A one-byte object could support up to 2^{sizeof(void *) * 8} nodes
35+
* in an address space. However, the red–black tree algorithm guarantees that
36+
* the tree depth is bounded by 2 * log₂(n), where n is the number of nodes.
37+
*
38+
* For operations such as insertion and deletion, a fixed-size array is used to
39+
* track the path through the tree. RB_MAX_DEPTH is conservatively defined as 16
40+
* times the size of a pointer to ensure that the array is large enough for any
41+
* realistic tree, regardless of the theoretical maximum node count.
3942
*/
4043
#define RB_MAX_DEPTH (sizeof(void *) << 4)
4144

0 commit comments

Comments
 (0)