0% found this document useful (0 votes)
21 views58 pages

AVL TREES With Insert and Delete Examples 22

AVL trees are a type of balanced binary search tree that maintain a height difference of at most 1 between sub-trees to ensure O(log N) time complexity for search, insertion, and deletion operations. They achieve balance through rotations during insertions and deletions, with four specific cases for rebalancing. The depth of AVL trees remains logarithmic, making them efficient for dynamic data environments.

Uploaded by

Piyush Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views58 pages

AVL TREES With Insert and Delete Examples 22

AVL trees are a type of balanced binary search tree that maintain a height difference of at most 1 between sub-trees to ensure O(log N) time complexity for search, insertion, and deletion operations. They achieve balance through rotations during insertions and deletions, with four specific cases for rebalancing. The depth of AVL trees remains logarithmic, making them efficient for dynamic data environments.

Uploaded by

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

Algorithms

AVL Tree

1
Balanced binary tree
● The disadvantage of a binary search tree is that its height
can be as large as N-1
● This means that the time needed to perform insertion and
deletion and many other operations can be O(N) in the
worst case
● We want a tree with small height
● A binary tree with N node has height at least (log N)
● Thus, our goal is to keep the height of a binary search
tree O(log N)
● Such trees are called balanced binary search trees.
Examples are AVL tree, red-black tree.
Binary Search Tree - Best Time

● All BST operations are O(h), where d is


tree depth
● minimum d is
h log2N for a binary
tree with N nodes
■ What is the best case tree?
■ What is the worst case tree?
● So, best case running time of BST operations
is O(log N)
Binary Search Tree - Worst Time
● Worst case running time is O(N)
■ What happens when you Insert elements in
ascending order?
○ Insert: 2, 4, 6, 8, 10, 12 into an empty BST
■ Problem: Lack of “balance”:
○ compare depths of left and right subtree
■ Unbalanced degenerate tree
Balanced and unbalanced BST

1 4
2 2 5
3
1 3
4
4 Is this “balanced”?
5
2 6 6

1 3 5 7 7
Approaches to balancing trees

● Don't balance
■ May end up with some nodes very deep
● Strict balance
■ The tree must always be balanced perfectly
● Pretty good balance
■ Only allow a little out of balance
● Adjust on access
■ Self-adjusting
Balancing Binary Search Trees

● Many algorithms exist for keeping


binary search trees balanced
■ Adelson-Velskii and Landis (AVL) trees
(height-balanced trees)
■ Splay trees and other self-adjusting trees
■ B-trees and other multiway search trees
AVL Tree is…
● Named after Adelson-Velskii and Landis
the first dynamically balanced trees to
be propose
● Binary search tree with balance condition in
which the sub-trees of each node can differ
by at most 1 in their height
Definition of a balanced tree
● Ensure the depth = O(log N)
● Take O(log N) time for searching, insertion,
and deletion
● Every node must have left & right sub-trees
of the same height
An AVL tree has the following
properties:

1. Sub-trees of each
node can differ by
at most 1 in their
height
2. Every sub-trees
is an AVL tree
AVL tree?

YES NO
Each left sub-tree has Left sub-tree has height 3,
height 1 greater than but right sub-tree has
each right sub-tree height 1
AVL Trees
10
10
5 20
5
3 20

3 43
2
1
1 3
AVL Trees

12

8 16

4 10 14

2 6
AVL Tree

1 0

0 0 1 -1

0 0 0 0
AVL Tree 2 AVL Tree

1 0

0 1

0 Not an AVL Tree


AVL - Good but not Perfect
Balance
● AVL trees are height-balanced binary search
trees
● Balance factor of a node
■ height(left subtree) - height(right subtree)
● An AVL tree has balance factor calculated at
every node
■ For every node, heights of left and right subtree
can differ by no more than 1
■ Store current heights in each node
Insertion

Insert 6

Imbalance at 8
Perform rotation with
7
Deletion

Delete 4

Imbalance at 3
Perform rotation with
2

Imbalance at 5
Perform rotation with
Key Points
● AVL tree remain balanced by applying
rotations, therefore it guarantees O(log
N) search time in a dynamic
environment
● Tree can be re-balanced in at most O(log
N) time
Searching AVL Trees
● Searching an AVL tree is exactly the same as
searching a regular binary search tree
■ all descendants to the right of a node are greater
than the node
■ all descendants to the left of a node are less than
the node
Inserting in AVL Tree
● Insertion is similar to regular binary search
tree
■ keep going left (or right) in the tree until a null
child is reached
■ insert a new node in this position
○ an inserted node is always a leaf to start with
● Major difference from binary tree
■ must check if any of the sub-trees in the tree have
become too unbalanced
○ search from inserted node to root looking for any
node with a balance factor of 2
Inserting in AVL Tree

● A few points about tree inserts


■ the insert will be done recursively
■ the insert call will return true if the height of the
sub-tree has changed
○ since we are doing an insert, the height of the sub-
tree can only increase
■ if insert() returns true, balance factor of current
node needs to be adjusted
○ balance factor = height(right) – height(left)
 left sub-tree increases, balance factor decreases by 1

 right sub-tree increases, balance factor increases by 1

■ if balance factor equals 2 for any node, the sub-


tree must be rebalanced
Inserting in AVL Tree
M(1) M(0)

E(-1) P(0)
insert(V) E(-1) P(-1)

J(0) J(0) V(0)

M(1) M(2)

E(1) P(0)
insert(L) E(-2) P(0)

J(0) J(1)

L(0)

This tree needs to be


fixed!
Re-Balancing a Tree

● To check if a tree needs to be rebalanced


■ start at the parent of the inserted node and
journey up the tree to the root
○ if a node’s balance factor becomes 2 need to do a
rotation in the sub-tree rooted at the node
○ once sub-tree has been re-balanced, guaranteed that
the rest of the tree is balanced as well
 can just return false from the insert() method
■ 4 possible cases for re-balancing
○ only 2 of them need to be considered
 other 2 are identical but in the opposite direction
Insertions in AVL Trees
Let the node that needs rebalancing be

There are 4 cases:


Outside Cases (require single rotation) :
1. Insertion into left subtree of left child
2. Insertion into right subtree of right child .

Inside Cases (require double rotation) :


3. Insertion into right subtree of left child
4. Insertion into left subtree of right child

The rebalancing is performed through four separate


rotation algorithms.
28
L L Rotation –Left of Left
Right Rotation

Single Rotation

29
R R Rotation Right of Right

Left Rotation

30
L R Rotation - Left of Right

Double rotation is needed

31
R L Rotation - Right of Left

Double Rotation is needed

32
AVL Trees Example
AVL Trees Example
AVL Trees Example
AVL Trees Example

RL Rotation

50
AVL Trees Example
AVL Trees Example
AVL Trees Example

53
Example
● Insert 3 into the AVL tree

11 11

8 20 4 20

4 16 27 3 8 16 27

5/22/2012
Example
● Insert 5 into the AVL tree
11 11

8 20 5 20

4 16 27 4 8 16 27

5/22/2012
Examples

56
57
58
59
60
AVL Trees: Exercise
● Insertion order:
■ 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55

5/22/2012
Deletion X in AVL Trees
● Deletion:
■ Case 1: if X is a leaf, delete X
■ Case 2: if X has 1 child, use it to replace X
■ Case 3: if X has 2 children, replace X with its
inorder predecessor (and recursively delete
it)
● Rebalancing

5/22/2012
Delete 55 (case 1)

60

20 70

10 40 65 85

5 15 30 50 80 90

55

5/22/2012
Delete 55 (case 1)

60

20 70

10 40 65 85

5 15 30 50 80 90

55

5/22/2012
Delete 50 (case 2)

60

20 70

10 40 65 85

5 15 30 50 80 90

55

5/22/2012
Delete 50 (case 2)

60

20 70

10 40 65 85

50 80 90
5 15 30

55

5/22/2012
Delete 60 (case 3)

60

20 70

10 40 65 85

5 15 30 50 prev 80 90

55

5/22/2012
Delete 60 (case 3)

55

20 70

10 40 65 85

5 15 30 50 80 90

5/22/2012
Delete 55 (case 3)

55

20 prev 70

10 40 65 85

5 15 30 50 80 90

5/22/2012
Delete 55 (case 3)

50

20 70

10 40 65 85

5 15 30 80 90

5/22/2012
Delete 50 (case 3)

50

20 prev 70

10 40 65 85

5 15 30 80 90

5/22/2012
Delete 50 (case 3)

40

20 70

10 30 65 85

5 15 80 90

5/22/2012
Delete 40 (case 3)

40

20 prev 70

10 30 65 85

5 15 80 90

5/22/2012
Delete 40 : Rebalancing

30

20 70

10 Case ? 65 85

5 15 80 90

5/22/2012
Delete 40: after rebalancing

30

10 70

5 20 65 85

15 80 90

Single rotation is preferred!

5/22/2012
AVL Tree: analysis

● The depth of AVL Trees is at most logarithmic.


● So, all of the operations on AVL trees are also
logarithmic.
● The worst-case height is at most 44 percent
more than the minimum possible for
binary trees.

5/22/2012

You might also like