Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Data Structures and Algorithms

AVL Trees Sundaresan Raman


Need for AVL Trees
• BST Time complexity of Search and Update operations depend
drastically on height
• Average case O(log N)
• Worst case O(N)
• For faster worst case performance, AVL trees are useful!
AVL Tree: Balance Condition
• Balance the root alone ?

• Balance left and right subtree of every


node?
AVL Tree
• AVL (Adelson-Velskii and Landis) Tree
• An AVL Tree is a binary search tree T such that for every internal
node v of T, the heights of the children of v can differ by at most 1.

44 4

2 17 3 78

1 32 2 50 1 88

1 48 1 62

An example of an AVL tree where the


heights are shown next to the nodes
Height of an AVL Tree
Proposition: The height of an AVL tree T storing n keys is O(log n).

Justification: The easiest way to approach this problem is to find n(h): the minimum
number of nodes in an AVL tree of height h.
• n(1) = 1 and n(2) = 2
• For h ≥ 3, an AVL tree of height h contains the root node, one AVL subtree of
height h – 1 and the other AVL subtree of height h – 1 or h – 2.
• That is, n(h) = 1 + n(h – 1) + n(h – 2)
Height of an AVL Tree
• Knowing n(h – 1) ≥ n(h – 2)
• n(h) = 1 + n(h – 1) + n(h – 2) > 2n(h – 2)
n(h) > 2n(h – 2)
> 4n(h – 4)
> 8n(h – 6)

> 2in(h – 2i)


• When i = h/2 – 1, n(h) > 2h/2−1 n(2) = 2h/2
• Taking logarithms: h < 2 log n(h)
• The height of an AVL tree is O(log n)
AVL Tree Structure
• Consider an AVL tree T with n nodes.
• A leaf which is closest to the root at level k.

Theorem: The height of the T is at most 2k – 1.


AVL Tree Structure
Implications: 33
• In an AVL tree of height h,
the leaf closest to the root is
at level at least (h + 1)/2. 20 46

• On the first (h – 1)/2 levels, 12 28 41 51

the AVL tree is a perfect


binary tree. 7 17 25 31 38 44 49 53

• After (h – 1)/2 levels, the


AVL tree may start 4 10 15 19 23 27 30 32 36 40 43 45 48 50 52

“thinning out.” 2 6 9 11 14 16 18 22 24 26 29 35 37 39 42 47

• Number of nodes in
the AVL tree is at 1 3 5 8 13 21 34

least 2(h-1)/2 and at


0
most 2h.
Source: https://stackoverflow.com/questions/51052802/avl-tree-difference-between-leaves-depths
Thank You!

You might also like