Is there a mathematical formula to calculate the number of nodes in the Left and Right of a complete binary tree?
There exists a similar problem on Stack Overflow - Find number of nodes in left and right subtree of a complete binary tree if total number of nodes are given in O(1).
But here I want to calculate the number of nodes in the left (L) and right (R) subtreee of a complete binary Tree programmatically.
I suspect there must be a concrete math formula to calculate L and R, given N, where N is the total number of nodes. Does anyone know of it?
I've been trying to calculate the height of the binary tree and then use height to calculate L and R, but I'm unsuccessful so far.
This is what I tried, but it's wrong:
h = math.floor(math.log(N,2))
L = min(N-1, 1+2**(h-1))
R = N - 1 - L