0% found this document useful (0 votes)
71 views17 pages

Ds 12-AVL

This document discusses balanced binary search trees, specifically AVL trees. It explains that AVL trees ensure searches take O(log N) time by restricting the height difference between left and right subtrees to be at most 1. Insertion and deletion may require rotations to rebalance the tree; different types of rotations are used depending on the position of the inserted/deleted node. Maintaining the balanced property through rotations allows AVL trees to support efficient searches, insertions and deletions.

Uploaded by

Sudha Shanker
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)
71 views17 pages

Ds 12-AVL

This document discusses balanced binary search trees, specifically AVL trees. It explains that AVL trees ensure searches take O(log N) time by restricting the height difference between left and right subtrees to be at most 1. Insertion and deletion may require rotations to rebalance the tree; different types of rotations are used depending on the position of the inserted/deleted node. Maintaining the balanced property through rotations allows AVL trees to support efficient searches, insertions and deletions.

Uploaded by

Sudha Shanker
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/ 17

Introduction

Insertion
Deletion
Balance Binary Search Tree
Worst case height of binary search tree: N
Insertion, deletion can be O(N) in the worst case
We want a tree with small height
Height of a binary tree with N node is at least (log
N)
Goal: keep the height of a binary search tree O(log N)
Balanced binary search trees
Examples: AVL tree, red-black tree
Balanced Tree?
Suggestion 1: the left and right subtrees of root have the
same height
Suggestion 2: every node must have left and right
subtrees of the same height
Our choice: for each node, the height of the left and
right subtrees can differ at most 1,-1,0.
AVL Tree
An AVL (Adelson-Velskii and Landis 1962) tree is a
binary search tree in which
for every node in the tree, the height of the left and
right subtrees differ by at most 1.

AVL tree AVL property


violated here
N1 = 1 N2 = 2 N3 =4 N4 = N2+N3+1=7
Height of AVL Tree
Denote Nh the minimum number of nodes in an AVL
tree of height h

N1=1, N2 =2 (base)
Nh= Nh-1 + Nh-2 +1 (recursive relation)

 many operations (i.e. searching) on an AVL tree will


take O(log N) time
Insertion in AVL Tree
Basically follows insertion strategy of binary search
tree
But may cause violation of AVL tree property
Restore the destroyed balance condition if needed

6 8

6
Insert 6
Original AVL tree Restore AVL property
Property violated
Some Observations
After an insertion, only nodes that are on the path from
the insertion point to the root might have their balance
altered
 Because only those nodes have their subtrees altered
Rebalance the tree at the deepest such node guarantees
that the entire tree satisfies the AVL property

6 8

6
Node 5,8,7 might Rebalance node 7
have balance altered guarantees the whole tree be AVL
Different Rebalancing
Rotations
The rebalancing rotations are classified as LL, LR, RR
and RL as illustrated below, based on the position of
the inserted node with reference to α.
LL rotation: Inserted node is in the left subtree of the left
subtree of node α
RR rotation: Inserted node is in the right subtree of the
right subtree of node α
LR rotation: Inserted node is in the right subtree of the
left subtree of node α
RL rotation: Inserted node is in the left subtree of the
right subtree of node α
Insertion Analysis
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 single rotation is performed logN
Or, we’ve checked all nodes in the path
Time complexity for insertion O(logN)
 Basically follows deletion strategy of binary search tree
 But may cause violation of AVL tree property
 Restore the destroyed balance condition if needed

20 20

10 35 15 35

5 15 25 40 10 18 25 40

18 30 38 45 30 38 45

50 50

Delete 5, Node 10 is unbalanced Single Rotation


20 35

15 35 20 40

10 18 25 40 15 25 38 45

30 38 45 10 18 30 50

Continue to check parents 50


Single Rotation
Oops!! Node 20 is
unbalanced!!
For deletion, after rotation, we need to continue tracing
upward to see if AVL-tree property is violated at other node.
Different rotations are classified in R0, R1, R-1, L0, L1 And L-1
Different rotations
• An element can be deleted from AVL tree which
may change the BF of a node such that it results in
unbalanced tree.
• Some rotations will be applied on AVL tree to
balance it.
• R rotation is applied if the deleted node is in the
right subtree of node A (A is the node with
balance factor other than 0, 1 and -1).
• L rotation is applied if the deleted node is in the
left subtree of node A.
Different rotations cont…
• Suppose we have deleted node X from the tree.

• A is the closest ancestor node on the path from X


to the root node, with a balance factor -2 or +2.

• B is the desendent of node A on the opposite


subtree of deleted node i.e. if the deleted node is
on left side the B is the desendent on the right
subtree of A or the root of right subtree of A.
R Rotation
• R Rotation is applied when the deleted node is in the right
subtree of node A.
• There are three different types of rotations based on the
balanced factor of node B.
• R0 Rotation: When the balance Factor of node B is 0.
• Apply LL Rotation on node A.
• R1 Rotation: When the balance Factor of node B is +1.
• Apply LL Rotation on node A.
• R-1 Rotation: When the balance Factor of node B is -1.
• Apply LR Rotation(RR rotation on B and LL rotation on
node A).
L Rotation
• L Rotation is applied when the deleted node is in the
left subtree of node A.
• There are three different types of rotations based on the
balanced factor of node B.
• L0 Rotation: When the balance Factor of node B is 0.
• Apply RR Rotation on node A.
• L-1 Rotation: When the balance Factor of node B is -1.
• Apply RR Rotation on node A.
• L1 Rotation: When the balance Factor of node B is +1.
• Apply RL Rotation(LL rotation on B and RR
rotation on node A).

You might also like