0

Say I have an AVL tree such that it's nodes store their own balance factor as a single integer.

How can I calculate the balance factor of a node N if I know the balance factor of both it's left and right child.

Note that I DO NOT have the rHeight and lHeight, so bal(N) = lHeight - rHeight is not an option.

Kara
  • 6,115
  • 16
  • 50
  • 57
irfanka
  • 129
  • 1
  • 14
  • Perhaps you misinterpreted the concept of balance. You can balance a tree in two ways: height-balanced and weight-balanced. I just constructed a height-balanced tree that has leftHeight=4 and rightHeight=3. However, leftCount=15 while rightCount=4. I'm guessing the balance factor you're referring to has to do with weight more than height, meaning the item counts of the left and right differ by no more than 1, which is not always the case with a height-balanced tree. An AVL tree is height-balanced. –  Dec 27 '13 at 19:19
  • You can also balance a tree both ways, but I imagine it is much slower. –  Dec 27 '13 at 19:25

1 Answers1

1

Short answer - you can't.

Long answer:

Consider these two trees:

           A
        /     \
     B           C                   A
   /   \       /   \                / \
  D     E     F     G              B   C
 / \   / \   / \   / \
H   I J   K L   M N   O

They have the same balance factor, but they aren't the same height.

So, if you only have the balance factor of the child, you don't know how high that subtree is, thus you can't use only that to calculate the balance factor of the parent.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138