The best way to calculate the height in a binary search tree? (balancing an AVL-tree)

前端 未结 9 678
渐次进展
渐次进展 2020-12-12 10:39

I\'m looking for the best way to calculate a nodes balance in an AVL-tree. I thought I had it working, but after some heavy inserting/updating I can see that it\'s not worki

9条回答
  •  醉梦人生
    2020-12-12 11:25

    You do not need to calculate tree depths on the fly.

    You can maintain them as you perform operations.

    Furthermore, you don't actually in fact have to maintain track of depths; you can simply keep track of the difference between the left and right tree depths.

    http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_avl.aspx

    Just keeping track of the balance factor (difference between left and right subtrees) is I found easier from a programming POV, except that sorting out the balance factor after a rotation is a PITA...

提交回复
热议问题