Finding the minimum and maximum height in a AVL tree, given a number of nodes?

后端 未结 4 1592
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 06:37

Is there a formula to calculate what the maximum and minimum height for an AVL tree, given a certain number of nodes?

For example:
Textbook question

4条回答
  •  攒了一身酷
    2021-01-31 07:01

    It's important to note the following defining characteristics of an AVL Tree.

    AVL Tree Property

    • The nodes of an AVL tree abide by the BST property
    • AND The heights of the left and right sub-trees of any node differ by no more than 1.

    Theorem: The AVL property is sufficient to maintain a worst case tree height of O(log N).

    Note the following diagram. AVL Tree

    - T1 is comprised of a T0 + 1 node, for a height of 1.
    - T2 is comprised of T1 and a T0 + 1 node, giving a height of 2.
    - T3 is comprised of a T2 for the left sub-tree and a T1 for the right sub-tree + 1 node, for a height of 3.
    - T4 is comprised of a T3 for the left sub-tree and a T2 for the right sub-tree + 1 node, for a height of 4.

    If you take the ceiling of O(log N), where N represents the number of nodes in an AVL tree, you get the height.

    Example) T4 contains 12 nodes. [ceiling]O(log 12) = 4.

    See the pattern developing here??

    **The worst-case height is enter image description here

提交回复
热议问题