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

前端 未结 9 737
渐次进展
渐次进展 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:12

    Here's where it gets confusing, the text states "If the balance factor of R is 1, it means the insertion occurred on the (external) right side of that node and a left rotation is needed". But from m understanding the text said (as I quoted) that if the balance factor was within [-1, 1] then there was no need for balancing?

    R is the right-hand child of the current node N.

    If balance(N) = +2, then you need a rotation of some sort. But which rotation to use? Well, it depends on balance(R): if balance(R) = +1 then you need a left-rotation on N; but if balance(R) = -1 then you will need a double-rotation of some sort.

提交回复
热议问题