1,662 views
0 votes
0 votes
If I am given an array $X$ of $n$ distinct integers which is interpreted as a complete binary tree, so if the parent is at index $i$, then it's left child would be at index $2i$ and it's right child would be at index $2i+1$. If root is considered to be at level $0$, then how can we find the level of an element $X[i]$?

I followed the approach of first considering the fact that at a level $k$, I would be having $2^k$ nodes. So I just considered that in the array starting from certain index $a$ up to $b$, I have all the numbers at the same level $k$. That is, in a particular portion I have $2^k$ nodes inclusive of $a$ and $b$.

Thus, before $a$ the total no of nodes is $2^0+2^1+2^2+ \dots +2^{k-1}$, since index $a$ is at level $k$, and before it there would be $k-1$ levels.

Now I am stuck while calculating the value of $b$.

Please guide about how to calculate the value of $b$?

5 Answers

Best answer
4 votes
4 votes

Simply take the $\log$! So,

$$\text{level } = \left \lfloor \log_2{(\text{index})} \right \rfloor$$

  • The root (index$=1$) is at level $\log_2 1 = 0$
  • The elements at index $2, 3$ are both at level $\left \lfloor \log_2{2} \right \rfloor = \left \lfloor \log_2{3} \right \rfloor = 1$
  • The elements at index $4,5,6,7$ are all at level $\left \lfloor \log_2{4} \right \rfloor = \left \lfloor \log_2{5} \right \rfloor = \left \lfloor \log_2{6} \right \rfloor = \left \lfloor \log_2{7} \right \rfloor = 2$
  • And so on.

If you wish to continue with your approach:


Let both $a$ and $b$ be at the level $k$.

Number of nodes before $a$ can be given as: $2^0 + 2^1 + 2^2 + \dots + 2^{k-1}$. This huge sum is just the sum of a Geometric Progression, and is equal to $2^k - 1$.

So, $a$, the first element at level $k$ is infact the $2^k$th element of the tree! (This is a nice property of complete trees)

Since $b$ is the last element of level $k$, the index of $b$ is simply $2^{k+1}-1$.


Now, let us assume that the element $X[i]$ is on the level $L$.

Since the indices of the first and last elements on the level $L$ are $2^L$ and $\left (2^{L+1}-1 \right )$ respectively, we know that $i$ must be within that range. So, $$2^L \quad \leq \quad i \quad \leq \quad 2^{L+1}-1$$

Taking $\log_2$, we get: $$L \quad \leq \quad \log_2{i} \quad \leq \quad L+1 - \varepsilon$$

$\varepsilon$ is a small error which was introduced when we ignored the $-1$ while taking $\log$ of $2^{L+1}-1$


So, we have: $$L = \left \lfloor \log_2 i \right \rfloor$$

selected by
1 votes
1 votes

Since for an index i you can visit its parent by Floor(i/2).

So go recursively upwards till u reach  root. So max complexity of finding level of a particular index node O(logN) if N is no of element in tree.

0 votes
0 votes
the value of b is a+2^k  but this is not the best  way to find out the level of a node.
0 votes
0 votes
as u stated that the left child at 2i array is starting at 1.

so simply take floor(log i) where i is the index.

Related questions

1 votes
1 votes
0 answers
1
Rohit Gupta 8 asked Dec 25, 2017
1,136 views
If the average depth of a node in an $n$-node binary search tree is $O(\log{n})$, then the height of the tree is$O(n\log{n})$$O(n)$$O(\log{n})$$O(\sqrt(n\log{n}))$
0 votes
0 votes
2 answers
3
sripo asked Dec 25, 2018
4,785 views
In a 3-array tree if internal nodes have exactly 3 children,the number of leaf nodes will be __ ?Does it vary for binary tree?What do you mean by internal nodes? Non roo...
11 votes
11 votes
5 answers
4
Vikrant Singh asked Dec 28, 2014
3,687 views
What is the complexity of finding $50^{th}$ smallest element in an already constructed binary min-heap?$\Theta(1)$$\Theta (\log n)$$\Theta (n)$$\Theta (n \log n)$