AVL Tree Properties PDF
AVL Tree Properties PDF
Let an AVL Tree of height h, minimum number of node Nh are given as: Nh = Nh-1 + Nh-2 + 1
So let us see why minimum number of node Nh is/are equal to Nh-1 + Nh-2 + 1?
20
h-2
10
20
40
h-1
OR
15
40
h-2
h-1
10
50
In AVL tree difference between the heights of its two sub-trees cannot be
greater than 1 or less than -1. Above trees are AVL trees of height h
in worst case (h = 2).
So total minimum number of nodes in AVL tree = Minimum Number of node
in Left sub-tree + Minimum Number of node in Left sub-tree + Root Node
Therefore, Nh = Nh-1 + Nh-2 + 1
As we know Minimum number of nodes in AVL tree are (Nh) = Nh-1 + Nh-2 + 1
So Base cases exit when h = 0 and h = 1.
When h=0, n0 =1
When h=1, N1 = 2
40
40
OR
40
50
30
Example
B) 8
C) 23
D) 13
Solution
=2+1+1=4
N3 = N2 + N1 + 1
=4+2+1=7
N4 = 7 + 4 + 1 = 12
Minimum number of node in AVL tree of height 4 is 12.
So correct Option is A
Note:
AVL tree having smaller height we can use equation Nh = Nh-1 + Nh-2 + 1 to
get total minimum number of nodes. What if we want to get total minimum
number of nodes of an AVL tree with larger height (lets say 20).
No doubt we can use the equation Nh = Nh-1 + Nh-2 + 1 to get the minimum
number of nodes, but this method become very lengthy as height of AVL
increases.
Lets see how to calculate total minimum number of node in AVL trees having
larger heights.
Nh 2 1.6h
Note :
Use Nh = Nh-1 + Nh-2 + 1 to calculate minimum number of nodes when
height of AVL tree is small.
Use Nh 2 1.6h to calculate minimum number of nodes when height
of AVL tree is large.
Be careful while using the equation Nh 2 1.6h to get the minimum
number of node in an AVL tree of height h, because by using this equation
we will get approximate results not exact minimum number of nodes.
Example
B) 422
C) 320
D) 232
Solution
But as height of given AVL Tree is slightly larger, and to get minimum number
of nodes using above recursive equation become time consuming. So we will
use below given equation to get minimum number of nodes.
Nh 2 1.6h
But by using this equation we will not get exact minimum number of nodes,
but we get a clue or approximate minimum number of nodes.
Nh 2 1.6h
Nh 2 1.610
Nh 2 109.95
Nh 219.9 220
220 is very close to 232, so total minimum number of nodes in an AVL tree of
height 10 are 232.
So correct Option is D
If we dont have given any options then how to get minimum number of
nodes in an AVL tree of height 10? So we need to find exact minimum number
of nodes, not approximate.
Let me give you a trick to get minimum number of nodes in an AVL Tree using
the equation Nh = Nh-1 + Nh-2 + 1.
When h=0, n0 = 1
When h=1, n1=2
As we know that Nh = Nh-1 + Nh-2 + 1, and we need to find minimum number
of nodes in an AVL tree of height 10.
N0 =
N1 = 2
N2 = (2 + 1) + 1 = 4
// Nh = Nh-1 + Nh-2 + 1
N3 = (4 + 2) + 1 = 7
N4 = (7 + 4) + 1 = 12
N5 = (12 + 7) + 1 = 20
N6 = (20 + 22) + 1 = 33
N7 = (33 + 20) + 1 = 54
N8 = (54 + 33) + 1 = 88
N9 = (88 + 54) + 1 = 143
N10 = (143 + 288) + 1 = 232
Example
B) 128
C) 255
D) 256
Solution
Example
B) N2>N1
C) N1=N2
Solution
Let find the answer of this question with the help of an example. Lets take
height equal to 3.
40
40
40
50
40
60
70
40
40
40
40
E .g: AVL Tree of height 3 have at least 7 nodes, so with 7 nodes we can build
an AVL tree of height 3(which is maximum possible height with 7 nodes)
Nh 2 1.6h
Nh/2 = 1.6h
Taking log2 on Both Sides
log2 (1.6h) = log2 (N/2)
h log2(1.6) = log2 (N/2)
h =
log2 (N/2)
log2 (1.6)
h =
log2 (N/2)
0.678
log2 (N/2)
0.678
Example
Let T be an AVL tree storing 143 elements. Largest possible height of T is?
A) 6
B) 7
C) 9
D) 8
Solution
log2 (N/2)
0.678
9.07
log2 (71.5)
0.678
6. 15
0.678
// log mn = n log m
// log22 = 1
h + 1 = log2 (N +1)
h
= log2 (N +1) - 1
= log2 (N +1) - 1
Example
B) 7
C) 6
D) None of these.
Solution
h = 71=6
We can also solve this question or verify the result using below given method as:
Number of nodes at
a particular level
20 = 1
20 = 1
21 = 2
20 + 21 = 3
22 = 4
20 + 21 + 22 = 7
23 = 8
20 + 21 + 22 + 23= 15
24 = 16
20 + 21 + 22 + 23+ 24= 31
25 = 32
20 + 21 + 22 + 23+ 24 + 25= 63
26 = 64
So Correct option is C.