Given N tuples in the tree, at most di
children per inner node and at most dl
values per leaf, the minimum height of a B+ Tree would be h = ceil(log_di((N + dl - 1) / dl))
if I am not mistaken.
This is only true if /
denotes the integer division and I could probably replace (N + dl - 1) / dl
with static_cast<double>(N) / dl
.
#include <cmath>
int minHeight(int N)
{
constexpr int di = 256;
constexpr int dl = 255;
return std::lround(std::ceil(log((N + (dl - 1)) / dl) / log(di)));
}
My interest lies in the pattern: (N + d - 1) /d
. This seems to be used when calculating the smallest multiple of the divisor (d) that is greater or equal to the dividend (N).
(N + d - 1) / d
is a perfectly normal way of writing an integer expression in C++. All of the terms in this expression are of integer type so, in particular, both numerator and denominator of the division /
are also int
. Therefore, C++ will apply /
as the division operator on int
types.
I'm not entirely sure exactly what you're asking in either of your questions. This "pattern" doesn't have a particular name that I'm aware of, but I'm not sure why you think it should have one. It's simply a mathematical expression.
As for making it "easier to understand", that is of course subjective, but (aside from the fact that variables don't have informative names) I find it perfectly readable. If you're looking for an algebraic simplification of the expression, then I'd warn you against it. While (N/d) + (1/d) - 1
, for example, looks mathematically equivalent it is not in this case in general. This is mainly because of the aforementioned fact that these are integer divisions, but also because the int
type has finite precision which may affect the result in some cases (with integer overflow, for example).