avl tree properties
TRANSCRIPT
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.
h-2
h-1
h-2
h-1 h
20
10 40
50
15 40
20
h
10
OR
40
50 30
40
When h=0, n0 =1 When h=1, N1 = 2
40
Minimum number of nodes in AVL tree of height h
OR
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?
Minimum number of nodes in an AVL tree of height 4?
A) 12 B) 8
C) 23 D) 13
Minimum number of node Nh in an AVL tree are
Nh = Nh-1 + Nh-2 + 1
We have to find minimum number of node in AVL tree of height 4.
So N4 = N3 + N2 + 1
We need to find the value of N3, N2
N2 = N1 + N0 + 1 // N0 = 0, N1 = 2 (from base cases)
= 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.
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.
Solution
Example
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 (let’s say 20).
Let’s see how to calculate total minimum number of node in AVL trees having
larger heights.
As we know minimum number of nodes in AVL tree of height h are:
Nh = Nh-1 + Nh-2 + 1
Solving above recurrence relation
Nh ≃ 2 × Φh // where Φ = 1.6 and h = AVL tree Height
Nh ≃ 2 × 1.6h
The Equation Nh ≃ 2 × 1.6h is also used to get total minimum number of
node. (Not Exact Minimum number of nodes but approximate)
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.
Note :
Minimum number of nodes in an AVL tree of height 10?
A) 180 B) 422
C) 320 D) 232
To get Minimum number of node Nh in an AVL tree we can use following
Nh = Nh-1 + Nh-2 + 1
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 //given height (h) is 10
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.
Solution
Example
So correct Option is D
If we don’t 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 = 1
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
So Use any method according to the requirement.
Maximum number of nodes in AVL tree of height h is 2h+1 - 1
AVL tree of height h contain maximum number of nodes when AVL tree is
perfectly balanced.
Maximum number of Non-leaf Node in an AVL tree of height 7 is?
A) 127 B) 128
C) 255 D) 256
Maximum number of nodes in an AVL tree of height h = 2h+1 – 1.
As we know number of leaf nodes in AVL tree (Perfectly balanced Tree) of
height h = 2h.
So Number of Non-leaf nodes = Total number of nodes – Number of leaf node
Number of Non-leaf nodes = (2h+1 − 1) − 2h = 2h − 1
So Number of Non-leaf nodes = 2h – 1 = 27-1 = 128 -1 = 127
So Correct option is A.
This AVL Tree is a perfectly balanced tree, and
hence contain maximum number of nodes.
Height of AVL Tree =3
Maximum number of nodes= 2h+1 -1
23+1 - 1 = 24 – 1 = 16 -1 =15
Example
Solution
Maximum number of nodes in AVL tree
Let N1 be the minimum number of nodes in a binary search tree of height h
and let N2 be the minimum number of nodes in an AVL tree of same height h.
Which of the following is always true (Assume h>1)
A) N1>N2 B) N2>N1
C) N1=N2 D) None of the these
Let’ find the answer of this question with the help of an example. Let’s take
height equal to 3.
So condition hold between N1 and N2 is: N2>N1
So Correct option is B.
Example
Solution
40
50
60
70
40 40
40 40 40
40
40
Minimum number of node in
a Binary Search Tree of height
(h=3) is equal to (N1=4)
Minimum number of node in
an AVL tree of height (h=3) is
equal to (N2=7)
log2 (1.6)
0.678
0.678 // log2 (1.6) = 0.678
Minimum number of nodes in an AVL tree of height h Leads to a maximum
possible height of AVL tree with N number nodes (least as possible ).
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)
As we know Minimum number of nodes in an AVL tree of height h are:
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)
log2 (N/2)
log2 (N/2)
So Maximum possible height h of AVL tree with N number nodes is:
log2 (N/2)
Where N is the number of nodes
h =
h =
Maximum possible height of AVL tree with n number nodes
h =
0.678
0.678
0.678
0.678
9.07
Let T be an AVL tree storing 143 elements. Largest possible height of T is?
A) 6 B) 7
C) 9 D) 8
Maximum possible height h of AVL tree with n number nodes is:
log2 (N/2)
Given number of node N = 143
log2 (143/2)
log2 (71.5) // log2 (71.5) = 6.15
6. 15
Example
Solution
h =
h =
h =
h =
h =
h = 9
Maximum number of nodes in an AVL tree of height h Leads to a minimum
possible height h of AVL tree with n number nodes.
As we know Maximum number of nodes in an AVL tree of height h are:
N = 2h+1 - 1 N+1 = 2h+1
Taking log2 on Both Sides
log2 (N+1) = log2 (2h+1)
log2 (N +1) = h+1 log22 // log mn = n log m
log2 (N +1) = h+1 // log22 = 1
h + 1 = log2 (N +1)
h = log2 (N +1) - 1
So Minimum possible height h of AVL tree with n number nodes is:
h = log2 (N +1) - 1
Where N is the number of nodes
Minimum possible height of AVL tree with n number nodes
Let T be an AVL tree storing 98 elements. Smallest possible height of T is?
A) 5 B) 7
C) 6 D) None of these.
So Minimum possible height h of AVL tree with n number nodes is:
h = log2 (N +1) - 1
h = log2 (99) – 1
h = 6.62 – 1 // Value of log2 (99) = 6.629
h = 7 – 1 = 6 // Round of 6.62 as 6.62 ≃ 𝟕
20 = 1
21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
Number of nodes at
a particular level
20 = 1
20 + 21 = 3
20 + 21 + 22 = 7
20 + 21 + 22 + 23= 15
20 + 21 + 22 + 23+ 24= 31
20 + 21 + 22 + 23+ 24 + 25= 63
20 + 21 + 22 + 23+ 24 + 25 + 26= 127
Total number of nodes up-to
that particular level
98 lies between 63 and 127, completely
filled up-to 5th level (63 nodes) and some
nodes (35 nodes) are at 6th level. So height of AVL Tree is 6.
Example
Solution
We can also solve this question or verify the result using below given method as:
So Correct option is C.