AVL tree average depth

Hey guys,

I have to build avl tree and find its average depth i.e. height/no.of nodes. I could not figure out any other way so i decided to enter manually height and node value and do simple math division to calculate depth but the result is inf. I don't even know what is it.
sample - int height=2
int node =3
float depth = height/node = inf ???????

could someone please help why it says so
Last edited on
height / node equals zero, you need to promote one of the variables to float:
(float)height / node

The max height of the tree can be found via recursion:
1
2
3
4
5
6
7
8
9
10
11
getHeight(AVL_Tree *ptr)
{
  if (!ptr) return 0;
  int leftHeight = getHeight(ptr->left);
  int rightHeight = getHeight(ptr->right);

  int max = leftHeight > rightHeight ? leftHeight : rightHeight;

  return max + 1; 
}
 
Last edited on
thanx for reply. Actually i already have the height same process as yours and the number of nodes. I can't figure out how to divide height/ number of nodes. since this is a function. please suggest.
Topic archived. No new replies allowed.