0% found this document useful (0 votes)
47 views30 pages

Advanced Algorithms Analysis and Design

The document discusses AVL trees, which are self-balancing binary search trees. It defines AVL trees as binary search trees where the heights of any node's two child subtrees differ by at most one. It describes how AVL trees maintain this balance property through rotations after insertions or deletions, keeping the tree height logarithmic. The key operations of single and double rotations are explained in detail through examples.

Uploaded by

Muhammad Arish
Copyright
© Attribution Non-Commercial (BY-NC)
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)
47 views30 pages

Advanced Algorithms Analysis and Design

The document discusses AVL trees, which are self-balancing binary search trees. It defines AVL trees as binary search trees where the heights of any node's two child subtrees differ by at most one. It describes how AVL trees maintain this balance property through rotations after insertions or deletions, keeping the tree height logarithmic. The key operations of single and double rotations are explained in detail through examples.

Uploaded by

Muhammad Arish
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 30

Advanced Algorithms Analysis and Design

Lecture 10

Elementry Data Structures AVL Trees

By: Huma Ayub

AVL Search Trees


What is an AVL Tree?

AVL Tree Implementation.

Why AVL Trees?

Rotations.

What is an AVL Tree?


An AVL tree is a binary search tree with a height balance property: For each node v, the heights of the subtrees of v differ by at most 1.
A subtree of an AVL tree is also an AVL tree. For each node of an AVL tree: Balance factor = height(right subtree) - height(left subtree) An AVL node can have a balance factor of -1, 0, or +1.

7
-1
1 3

-1 10 1

7
-2 0
1 1 2 3

-1
10 1 13 0

1 2
0

13

AVL Tree

Not an AVL Tree

Why AVL Trees?


Insertion or deletion in an ordinary Binary Search Tree can cause large imbalances. In the worst case searching an imbalanced Binary Search Tree is O(n).

An AVL tree is rebalanced after each insertion or deletion. The height-balance property ensures that the height of an AVL tree with n nodes is O(log n). Searching, insertion, and deletion are all O(log n).

Rebalancing
Suppose the node to be rebalanced is X. There are 4 cases that we might have to fix (two are the mirror images of the other two):
1. An insertion in the left subtree of the left child of X, 2. An insertion in the right subtree of the left child of X, 3. An insertion in the left subtree of the right child of X, or 4. An insertion in the right subtree of the right child of X.

Balance is restored by tree rotations.


5

What is a Rotation?
A rotation is a process of switching children and parents among two or three adjacent nodes to restore balance to a tree. An insertion or deletion may cause an imbalance in an AVL tree. The deepest node, which is an ancestor (parent) of a deleted or an inserted node, and whose balance factor has changed to -2 or +2 requires rotation to rebalance the tree. 50 -1
-1 45 0 40 0 35 78 0 -1 40 Deepest unbalanced node

Insert 35
-2 45

-2 50 78 0

What is a Rotation? (contd.)


-2 -2 45 -1 40

50
rotation 78 0

-1 50 0 40 0
35 45 0 78 0

35

There are two kinds of single rotation: Right Rotation. Left Rotation.

A double right-left rotation is a right rotation followed by a left rotation. A double left-right rotation is a left rotation followed by a right rotation.

Single Right Rotation


Single right rotation: The left child x of a node y becomes y's parent. y becomes the right child of x. The right child T2 of x, if any, becomes the left child of y.
deepest unbalanced node y x T3 T1 T2 T1 T2 T3

a right rotation of x about y

x y

Note: The pivot of the rotation is the deepest unbalanced node

Single Left Rotation


Single left rotation: The right child y of a node x becomes x's parent. x becomes the left child of y. The left child T2 of y, if any, becomes the right child of x.
deepest unbalanced node x y

a left rotation of y about x

y x T3

T1
T2 T3 T1 T2

Note: The pivot of the rotation is the deepest unbalanced node

Single Rotation
A single rotation switches the roles of the parent and child while maintaining the search order. Single rotation handles the outside cases (i.e. 1 and 4).i.e
1: An insertion in the left subtree of the left child of X, 4: An insertion in the right subtree of the right child of X.

We rotate between a node and its child.


Child becomes parent. Parent becomes right child in case 1, left child in case 4.

The result is a binary search tree that satisfies the AVL property.
10

Double Rotation
Single rotation does not fix the inside cases (2 and 3).
2. An insertion in the right subtree of the left child of X, 3. An insertion in the left subtree of the right child of X,

These cases require a double rotation

11

Figure 19.25

Single rotation fixes an AVL tree after insertion of 1.

CENG 213 Data Structures

12

Example : Start with an empty AVL tree and insert the items 3,2,1, and then 4 through 7 in sequential order.

Example
Start with an empty AVL tree and insert the items 3,2,1, and then 4 through 7 in sequential order. Answer:

14

Double Right-Left Rotation


x z deepest unbalanced node right rotation of y about z T1 T4 T2 T3 y
Note: First pivot is the right child of the deepest unbalanced node; second pivot is the deepest unbalanced

x y z

T1

T2

T3

T4

left rotation of Y about X z

T1

T2

T3

T4

Double Left-Right Rotation


x v w T1 deepest unbalanced node x w v T3 T4

left rotation of w about v T4

T2

T3
w

T1

T2 left rotation of W about X x

Note: First pivot is the left child of the deepest unbalanced node; second pivot is the deepest unbalanced

T1

T2

T3

T4

BST ordering property after a rotation


A rotation does not affect the ordering property of a BST (Binary Search Tree).
y x T3 T1 a right rotation of x about y x y

T1

T2

T2

T3

BST ordering property requirement: T1 < x < y


x < T2 < y x < y < T3 Similarly for a left rotation.

BST ordering property requirement: T1 < x < y


x < T2 < y x < y < T3

Similar

Insertion Analysis

logN

Insert the new key as a new leaf just as in ordinary binary search tree: O(logN) Then trace the path from the new leaf towards the root, for each node x encountered: O(logN) Check height difference: O(1) If satisfies AVL property, proceed to next node: O(1) If not, perform a rotation: O(1) The insertion stops when A rotation is performed Or, weve checked all nodes in the path Time complexity for insertion O(logN)

Deletion from AVL Tree


Delete a node x as in ordinary binary search tree Note that the last (deepest) node in a tree deleted is a leaf or a node with one child Then trace the path from the new leaf towards the root

For each node x encountered, check if heights of left(x) and right(x) differ by at most 1.
If yes, proceed to parent(x)
If no, perform an appropriate rotation at x Continue

to trace the path until we reach the root

Deletion Example 1

20 10 5 15 18 25 30 35 40 38 45 50 Delete 5, Node 10 is unbalanced 10 15 18

20 35 25 40

30

38

45 50

Single Rotation

Contd
20 15 10 18 25 30 35 40 38 45 10 15 18 20 25 30 35 40 38 45 50

Continue to check parents 50 Oops!! Node 20 is unbalanced!!

Single Rotation

For deletion, after rotation, we need to continue tracing upward to see if AVL-tree property is violated at other node.

How much you Learn Today ????

Balance by yourself

Solution

NEXT WEEK

You might also like