title: How can I calculate the level of a node in a perfect binary tree from its depth-first order index?
body: <p>I have a perfect binary tree, i.e. each node in the tree is either a leaf node, or has two children, and <em>all</em> leaf nodes are on the same level. Each node has an index in depth-first order.</p>
  
  <p>(E.g. in a tree with 3 levels the root node has index 0, the first child has 1, the first child of the first child has 2, the second child of the first child has 3, the second child has 4, the first child of the second child has 5, the second child of the second child has index 6.</p>
  
  <pre><code>      0
      /   \
    1      4
   / \    / \
  2   3  5   6
  </code></pre>
  
  <p>)</p>
  
  <p>I know the size of tree (number of nodes/maximum level), but only the index of a particular node, and I need to calculate its level (i.e. its distance to the rootnode). How do I do this most efficiently?</p>