Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate the level of a node in a perfect binary tree from its depth-first order index?

I have a perfect binary tree, i.e. each node in the tree is either a leaf node, or has two children, and all leaf nodes are on the same level. Each node has an index in depth-first order.

(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.

      0
    /   \
  1      4
 / \    / \
2   3  5   6

)

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?

like image 920
hrehfeld Avatar asked May 23 '12 14:05

hrehfeld


People also ask

How do you find the level of node in a binary tree?

To calculate the level of a binary tree, we can traverse the tree level-by-level. We start with the root node as level 0. Then we visit every node on a level before going to another level. For example, the level-by-level traversal sequence of the above example tree is 1, 2, 3, 4, 5.

How do you determine the level of each node in the given tree?

The BFS can be used to determine the level of each node from a given source node. Algorithm: Create the tree, a queue to store the nodes and insert the root or starting node in the queue. Create an extra array level of size v (number of vertices) and create a visited array.

How do you find the level of a node in a binary tree in C?

Algorithm to find level of a given node in binary treeLet node be the pointer to any node at level L. If node is equal to NULL, return. If node is equal to N, then print the level of node(L) on screen else continue traversal of sub trees at level L+1.


2 Answers

Here is another suggestion that would make the solution to this question easier:

If you label the nodes with an index in breadth-first order, you can compute the level without any traversal in O(1) time. So if you are doing multiple queries, you can do an O(N) BFT and have each query answered in O(1) time.

The formula for the level is:

level = floor(log(index + 1))

Where the log is to the base 2

Try it out on this tree:

       0
     /    \
    /      \
   1        2
  / \      / \
 /   \    /   \
3     4  5     6

Cheers.

like image 90
amahfouz Avatar answered Sep 30 '22 17:09

amahfouz


let i be the index you are looking for and n be the total number of nodes.

This algorithm do what you want:

level = 0
while i != 0 do
    i--
    n = (n-1)/2
    i = i%n
    level++
done

0 is the index of the root, if i = 0 then you are at the good level, else you can remove the root and you obtain two subtrees n = (n-1)/2 updates the number of nodes is the new tree (which is a subtree of the old one) and i = i%n selects only the good subtree.

like image 21
Thomash Avatar answered Sep 30 '22 16:09

Thomash