0% found this document useful (0 votes)
38 views

Lecture - 7.6 (AVL Trees)

The document discusses degenerate binary search trees and how they can become unbalanced. It then introduces AVL trees which use rotations to keep the tree balanced by ensuring the heights of left and right subtrees differ by at most one. An example is provided of building an AVL tree through successive insertions and required rotations.

Uploaded by

Ali Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lecture - 7.6 (AVL Trees)

The document discusses degenerate binary search trees and how they can become unbalanced. It then introduces AVL trees which use rotations to keep the tree balanced by ensuring the heights of left and right subtrees differ by at most one. An example is provided of building an AVL tree through successive insertions and required rotations.

Uploaded by

Ali Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 66

In the Name of Allah the Most

Beneficent the Most Merciful

Subject : Data Structures & Algorithms

Lecture : 07
Saturday, May 25, 2024 1
Degenerate Binary Search Tree

BST for 14, 15, 4, 9, 7, 18, 3, 5, 16, 20, 17


14

4 15

3 9 18

7 16 20

5 17
Degenerate Binary Search Tree

BST for 3 4 5 7 9 14 15 16 17 18 20
Degenerate Binary Search Tree

BST for 3 4 5 7 9 14 15 16 17 18 20

3
4
5
7
9
14
15
16
17
18
20
Degenerate Binary Search Tree

BST for 3 4 5 7 9 14 15 16 17 18 20

3
4
5
7
9
14
15
16
Linked List! 17
18
20
Balanced BST

• We should keep the tree balanced.


• One idea would be to have the left and
right subtrees have the same height
Balanced BST

14
9 15
7 16
5 17
4 18
3 20

Does not force the tree to be shallow.


Balanced BST

• We could insist that every node must have left


and right subtrees of same height.
• But this requires that the tree be a complete
binary tree
• To do this, there must have (2d+1 – 1) data
items, where d is the depth of the tree.
• This is too rigid a condition.
AVL Tree

• AVL (Adelson-Velskii and Landis) tree.


• An AVL tree is identical to a BST except
 height of the left and right subtrees
can differ by at most 1.
 height of an empty tree is defined to
be (–1).
AVL Tree

• An AVL Tree height


5 0

2 8 1

1 4 7 2

3 3
AVL Tree

• Not an AVL tree height


6 0

1 8 1

1 4 2

3 5 3
Balanced Binary Tree

• The height of a binary tree is the maximum


level of its leaves (also called the depth).
• The balance of a node in a binary tree is
defined as the height of its left subtree minus
height of its right subtree.
• Here, for example, is a balanced tree. Each
node has an indicated balance of 1, 0, or –1.
Balanced Binary Tree

-1

1 0

0 0 1 -1

0 0 0 0 0 0

0 0 0 0
Balanced Binary Tree
Insertions and effect on balance

-1

1 0

0 0 1 -1

0 0 B B 0 0 0 0

U1 U2 U3 U4 0 0 B B B B 0 0

U5 U6 U7 U8 U9 U10 U11 U12


Balanced Binary Tree

• Tree becomes unbalanced only if the newly


inserted node

 is a left descendant of a node that


previously had a balance of 1 (U1 to
U8),
 or is a descendant of a node that
previously had a balance of –1 (U9 to
U12)
Balanced Binary Tree
Insertions and effect on balance

-1

1 0

0 0 1 -1

0 0 B B 0 0 0 0

U1 U2 U3 U4 0 0 B B B B 0 0

U5 U6 U7 U8 U9 U10 U11 U12


Balanced Binary Tree
Consider the case of node that was previously 1

-1

1 0

0 0 1 -1

0 0 B B 0 0 0 0

U1 U2 U3 U4 0 0 B B B B 0 0

U5 U6 U7 U8 U9 U10 U11 U12


Inserting New Node in AVL Tree

A 1

B 0

T3

T1 T2 1
Inserting New Node in AVL Tree

A 2

B 1

T3

T1 T2 1
2

ne
w
Inserting New Node in AVL Tree

A 2 B 0

A
B 1 0

T1
T3

T1 T2 1 T2 T3
2
ne
w

ne
w

Inorder: T1 B Inorder: T1 B
T2 A T3 T2 A T3
AVL Tree Building Example

• Let us work through an example that inserts


numbers in a balanced search tree.
• We will check the balance after each insert
and rebalance if necessary using rotations.
AVL Tree Building Example

Insert(1)
1
AVL Tree Building Example

Insert(2)
1

2
AVL Tree Building Example

Insert(3) single left rotation


1 -
2

3
AVL Tree Building Example

Insert(3) single left rotation


1 -
2

3
AVL Tree Building Example

Insert(3)
2

1 3
AVL Tree Building Example

Insert(4)
2

1 3

4
AVL Tree Building Example

Insert(5)
2

1 3 -2

5
AVL Tree Building Example

Insert(5)
2

1 4

5
3
AVL Tree Building Example

Insert(6)
2 -2

1 4

5
3
6
AVL Tree Building Example

Insert(6)
4

2 5

6
1 3
AVL Tree Building Example

Insert(7)
4

2 5

6
1 3
7
AVL Tree Building Example

Insert(7)
4

2 5 -2

6
1 3
7
AVL Tree Building Example

Insert(7)
4

2 6

7
1 3 5
AVL Tree Building Example

Insert(16)
4

2 6

7
1 3 5
16
AVL Tree Building Example

Insert(15)
4

2 6

7
1 3 5
16

15
AVL Tree Building Example

Insert(15)
4

2 6

7 -2
1 3 5
16

15
AVL Tree Building Example

Insert(15)
4

2 6

16
1 3 5

15
AVL Tree Building Example

Insert(15)
4

2 6

16 -2
1 3 5

15
AVL Tree Building Example

• Single rotation does not seem to restore the


balance
• The problem is the node 15 is an inner subtree
that is too deep.
• Let us revisit the rotation
AVL Tree Building Example

• Let us call the node that must be rebalanced α


• Since any node has at most two children, and
height imbalance requires that α’s node two
subtrees differ by two (or -2), the violation
will occur in four cases
AVL Tree Building Example

i. An insertion into left subtree of the left child of α

ii. An insertion into right subtree of the left child of α

iii. An insertion into left subtree of the right child of α

iv. An insertion into right subtree of the right child of α


AVL Tree Building Example

• The insertion occurs on the “outside” (i.e., left-left


or right-right) in cases 1 and 4.
• Single rotation can fix the balance in cases 1 and 4
• Insertion occurs on the “inside” in cases 2 and 3
which single rotation cannot fix.
AVL Tree Building Example

AL 44
AVL Tree Building Example

AL 45
AVL Tree Building Example

AL 46
AVL Tree Building Example

AL 47
AVL Tree Building Example

AL 48
AVL Tree Building Example

AL 49
AVL Tree Building Example

AL 50
AVL Tree Building Example

AL 51
AVL Tree Building Example

AL 52
AVL Tree Building Example

AL 53
AVL Tree Building Example

AL 54
AVL Tree Building Example

AL 55
AVL Tree Building Example

AL 56
AVL Tree Building Example

AL 57
AVL Tree Building Example

AL 58
AVL Tree Building Example

AL 59
AVL Tree Building Example

AL 60
AVL Tree Building Example

AL 61
AVL Tree Building Example

AL 62
AVL Tree Building Example

AL 63
AVL Tree Building Example

AL 64
AVL Tree Building Example

AL 65
AVL Tree Building Example

AL 66

You might also like