I’m trying to compute the maximal weight in a heap with this function:
unsigned int left(unsigned int x)
{return 2*x+1;}
unsigned int right(unsigned int x)
{return 2*x+2;}
unsigned int max_way (unsigned int* feld, int x, int max_size)
{
if (x > max_size)
return 0;
else
return feld[x] + std::max(max_way(feld, left(x), max_size), max_way(feld, right(x), max_size));
}
So I try it with a small example:
unsigned int feld[] = {3,7,4,2,4,6,8,5,9,3};
std::cout << max_way(feld, 0, 10);
The result is 134514494. That’s a bit to big!
Any ideas?
should be
C++ arrays go from 0 .. max-1.