Calculate binary tree node number and layer number generated from $10^{100}$

binarytrees

Trying to learn about calculations related to binary trees, and would like to know how many binary tree node layers there are in a binary tree generated from $10^{100}$, and more generally, how to calculate it for any number.

By generated from x I mean you take that number like $10^{100}$ and create a binary trie that can store all of these numbers. By store I mean, the first node stores 1, the next 2 nodes store 0 and 1, which means it stores 10 and 11 or 2 and 3. Then 11 stores 1 and 0, so 110 and 111 or 6 and 7, etc.. I am not sure how to calculate how many nodes it will have, but that would be part of the answer, how to do that.

The main part of the answer would be, given the tree with $n$ nodes, the number of layers $l$ in the tree.

$$f(x) = \{n, l\}$$
$$f(10^{100}) = \{?,\ ?\}$$

Best Answer

If you take $n > 0$, and remove the last binary digit to create the number $m$, then $m < n$, so in particular if $n$ is in your trie so is $m$. Thus your tree has $10^{100}$ nodes. In fact, if you write down the infinite binary tree, with the root on top, then you'll see that the numbers just appear in order, left to right, top to bottom. First, a row with 1; then a row with $2, 3$; then a row with $4, 5, 6, 7$; and so on.

From this you can also see that the first $k$ rows take up the first $2^{k} - 1$ numbers. This means that the number of layers in your tree is $$ \min \{k \mid 10^{100} \leq 2^k - 1\}. $$ That $-1$ is annoying: if it weren't there, we could simply say, well, we are looking for the least $k$ such that $100 \log(10) \leq k \log(2)$, i.e., we are looking for $$ k = \left\lceil 100 \frac{\log(100)}{\log(2)}\right\rceil. $$ But it might be that $10^{100} + 1 = 2^k$ for some $k$, in which case our $k$ is an overestimate. But fortunately, we see that this is not possible, because $2^k$ is even, and $10^{100} + 1$ is odd, so indeed our estimate of the depth is correct.