I am looking at big code base in C++. There is line mentioned as below
int capacity( ) const
{
return ( 1 << theTrees.size( ) ) - 1;
}
Here theTrees is
vector<int> theTrees
What is statement 1 << theTrees.size( ) likely trying to achieve? Assume we have tree size of 25 elements.
A left shift by n is basically multiplying by 2 to the nth power. Whenever you have
1 << nyou’re just calculating the nth power of 2. E.g.:Etc.
I suspect
theTrees.size()returns not the number of elements in the tree but the height of the tree (number of levels), because that’s the only way this makes sense. Given a full binary tree, the number of nodes is 2^N – 1, where N is the height of the tree. E.g., a tree with three levels (n = 3) can hold 2^3 – 1 = 7 nodes. Check it: The first level has one, the second has two and the third has four. 1 + 2 + 4 = 7.