# Thread: Suggestions on how to calculate something

1. ## Suggestions on how to calculate something

I am trying to calculate the height of nodes in a tree. I think that if I take the closest power of 2 that is larger than the number of nodes in a tree and subtract one from it, that would be the height of the node. For example,
if a tree has 15 nodes, the closest power of 2 higher than 15 is 4, so the height of that node with respect to the node is 3. So it seems that it works, but I am having trouble coding it. Can anyone provide tips or suggestions to get me on track? Thanks.

2. What kind of tree are you talking about? For example, a binary search tree could degenerate into a linked list type of structure, and a multiway tree could have 15 child nodes for the root, so the height would just be 1.

3. That would certainly work for a heap, but won't be 100% accurate for a binary tree. Even an AVL tree with 15 nodes can be a tiny bit unbalanced.

I guess the question is, do you want it to be accurate, or just fast?

4. It is for an AVL Tree and my class already has methods for rebalancing the tree. However, I am trying to store the height information within the node struct of each node, so that I can later get the internal path length of the tree.