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

AVL Tree

The document discusses balanced binary trees, specifically focusing on height-balanced trees (AVL trees) and their properties. It explains the importance of maintaining a logarithmic height in relation to the number of nodes and details the process of rotations to restore balance after insertions or deletions. Additionally, it outlines the types of violations that can occur and the corresponding rotations needed to maintain AVL properties.

Uploaded by

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

AVL Tree

The document discusses balanced binary trees, specifically focusing on height-balanced trees (AVL trees) and their properties. It explains the importance of maintaining a logarithmic height in relation to the number of nodes and details the process of rotations to restore balance after insertions or deletions. Additionally, it outlines the types of violations that can occur and the corresponding rotations needed to maintain AVL properties.

Uploaded by

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

11.

Trees

Dr. Swati Agarwal


Agenda

1 Balanced Binary Trees

2 Height of Binary Balanced Trees

3 Rotations

March 28, 2020 Dr. Swati Agarwal 11. Trees 2/19


Balanced Binary Trees
1. Balanced Binary Trees

We have seen different trees with worst case complexity O(n)


where n is the number of nodes in the tree. (happens in the
case of Skew Trees).

We try to reduce the complexity by imposing restrictions on


the height of the tree and preserve the inductive structure of a
search tree after insertion/deletion operation.

A balanced tree is one in which, at each node, the sizes of the


left and right subtrees differ by at most one.

It is difficult to maintain this definition without incurring


a heavy re-balancing cost.

March 28, 2020 Dr. Swati Agarwal 11. Trees 4/19


1.1 Height Balanced Binary Trees

A more flexible definition will be that the heights of the left


and right subtrees differ by at most one.

The balance factor value can be {−1, 0, 1}

Such trees are called Height-Balanced trees or AVL trees


represented with HB(k) where k represents the Balance
Factor.

March 28, 2020 Dr. Swati Agarwal 11. Trees 5/19


1.2 Height vs. Size Balanced Binary Trees
13
7 16
4 14 31
19 50

For node 13, the size-based balance factor is 3 since the


number of nodes in left and right sub-trees are 2 and 5
respectively.
While, the height-based balance factor is 1 since the height of
left and right sub-trees are 2 and 3 respectively.
Given tree is height balanced but not size balanced.

March 28, 2020 Dr. Swati Agarwal 11. Trees 6/19


1.3 Calculate Balance Factor in AVL Tree
20
14 30
7 18 23 35
4 9 21 27
1 5

March 28, 2020 Dr. Swati Agarwal 11. Trees 7/19


2. Height of Binary Balanced Trees
To use height-balanced trees, we have to ensure that height of
the tree is logarithmic in the number of nodes in a tree.
Rephrase the problem and find that for a given height h, what
is the size S(h) of the smallest height-balanced tree with
height h?
h S(h) T(h)
0 1 * h S(h) T(h)
1 2 * 3 7 *

* * *
2 4 * * * *
* * *
*
March 28, 2020 Dr. Swati Agarwal 11. Trees 8/19
2. Height of Balanced Binary Tree
S(1) = 1
S(2) = 2
S(3) = 4
S(4) = 7
...

S(h) = S(h − 2) + S(h − 1) + 1, h > 2


is very similar to that for the Fibonacci numbers
F (k) = F (k − 1) + F (k − 2), k > 2.
In the case of Fibonacci numbers, we can show that F (k) is
exponentially large with respect to k.
Analogously, we can show that S(h) grows exponentially with
respect to h. This means that even the ”most skewed”
height-balanced tree with n nodes has height logarithmic in n.
March 28, 2020 Dr. Swati Agarwal 11. Trees 9/19
2.1 Number of Nodes in AVL Tree

Minimum number of nodes in AVL Tree:


S(h) = S(h − 2) + S(h − 1) + 1, h > 2
By solving the above recurrence equation:
S(h) = O(1.618h ) ⇒ h = 1.44 log n = O(log n)

Maximum number of nodes in AVL Tree:


S(h) = S(h − 1) + S(h − 1) + 1, h > 2
The above expression defines the case of full binary tree. By
solving the above recurrence equation:
S(h) = O(2h ) ⇒ h = log n = O(log n)

March 28, 2020 Dr. Swati Agarwal 11. Trees 10/19


3. Rotations

AVL Tree is a self-balancing binary search tree.


When the tree structure changes (insertion or deletion
operations), the tree needs be modified to maintain the AVL
properties.
This balancing can be done via rotations.
Since during insertion or deletion operations, only one node is
modified, this can only increase/decrease the height of some
sub-tree by 1.
If the AVL tree property is violated at node X that means that
height of left(X ) and right(X ) subtrees differ by exactly 2.
Rotations is the technique to restore the AVL tree property.

March 28, 2020 Dr. Swati Agarwal 11. Trees 11/19


Types of Violations
An insertion operation into the

left subtree of left child of X . LL Rotation


right subtree of left child of X . LR Rotation
left subtree of right child of X . RL Rotation
right subtree of right child of X . RR Rotation

After inserting the element in an AVL Tree:

Walk up to the root node


find first node that is violated
maintain the AVL property for that node using rotations
repeat.

March 28, 2020 Dr. Swati Agarwal 11. Trees 12/19


Example: same set keys
30, 20, 10 30, 10, 20 10, 30, 20 10, 20, 30
30 30 10 10
20 10 30 20
10 20 20 30
20, 10, 30 20, 30, 10
20 20
10 30 10 30
For left and right skewed trees, single rotations are enough.
However, for zigzag trees, single rotation converts the sub-tree into
it’s mirror image depicting the no use of rotations. For zig-zag
shaped trees, we need double rotations.

March 28, 2020 Dr. Swati Agarwal 11. Trees 13/19


Single Rotation- LL
Y

X X
t4

W W Y
t3

t1 t2 t1 t2 t3 t4
=⇒

March 28, 2020 Dr. Swati Agarwal 11. Trees 14/19


Single Rotation- RR
W

X X
t1

Y W Y
t2

t3 t4 t1 t2 t3 t4
=⇒

March 28, 2020 Dr. Swati Agarwal 11. Trees 15/19


Double Rotation: LR
Y Y

W X X
t4 t4

X W W Y
t1 t3

t2 t3 t1 t2 t1 t2 t3 t4
⇒ ⇒

March 28, 2020 Dr. Swati Agarwal 11. Trees 16/19


Double Rotation: RL
W W

Y X X
t1 t1

X Y W Y
t4 t2

t2 t3 t3 t4 t1 t2 t3 t4
⇒ ⇒

March 28, 2020 Dr. Swati Agarwal 11. Trees 17/19


Practice Question
Input sequence: 30, 20, 10, 40, 50, 60, 25, 35, 28, 38
The above sequence will have all four rotations.

March 28, 2020 Dr. Swati Agarwal 11. Trees 18/19


Deletion in AVL Tree

Apply BST Delete to find and delete the element


• Leaf Node
• Node with one child/sub-tree
• Node with both children/sub-trees
Maintain the AVL properties using rotations.

March 28, 2020 Dr. Swati Agarwal 11. Trees 19/19

You might also like