[go: up one dir, main page]

100% found this document useful (1 vote)
34 views45 pages

Ds 12 AVL Tree

Uploaded by

Aryan Gour
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
100% found this document useful (1 vote)
34 views45 pages

Ds 12 AVL Tree

Uploaded by

Aryan Gour
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/ 45

AVL Tree

INTRODUCTION
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
AVL Tree with Minimum Number of Nodes

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
AVL Tree
INSERTION
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 Restore AVL property
Original AVL tree
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 subtree altered
Rebalance the tree at the deepest such node guarantees that the entire tree
satisfies the AVL property

6 8

6 Rebalance node 7
Node 5,8,7 might guarantees the whole tree be AVL
have balance altered
Different Rebalancing Rotations
LL Rotation The new node is inserted in the left sub-tree of the left sub-tree of the critical
node.
Different Rebalancing Rotations
LL Rotation

Courtesy : javatpoint.com/avl-tree
Different Rebalancing Rotations
RR Rotation The new node is inserted in the right sub-tree of the right sub-tree of the critical
node.
Different Rebalancing Rotations
RR Rotation

Courtesy : javatpoint.com/avl-tree
Different Rebalancing Rotations
LR Rotation The new node is inserted in the right sub-tree of the left sub-tree of the critical
node.
Different Rebalancing Rotations
LR Rotation

Courtesy : javatpoint.com/avl-tree
Different Rebalancing Rotations
RL Rotation The new node is inserted in the left sub-tree of the left sub-tree of the
critical node.
Different Rebalancing Rotations
RL Rotation

Courtesy : javatpoint.com/avl-tree
AVL Tree Construction
63, 9, 19, 27, 18, 108, 99, 81

Courtesy : javatpoint.com/avl-tree
AVL Tree Construction

Courtesy : javatpoint.com/avl-tree
AVL Tree Construction

Courtesy : javatpoint.com/avl-tree
1. Create AVL for following:
43, 10, 79, 90, 12, 10,54, 11, 9, 50
Solution step1:
Final Answer:
2. CREATE AVL:
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81, 68, 61,51,62
Final Answer:
3. Create a AVL tree with following data by inserting the following
elements in order of their occurrence.
200,150,350,100,70,110,250,500,400,550,450
4. Create a BST tree with following data by inserting the following
elements in order of their occurrence. [5 marks]

100,50,250,110,45,150,65,30,38,260,150,120,135
Insertion Analysis
Insert the new key as a new leaf just as in ordinary binary search tree:
O(log n)
Then trace the path from the new leaf towards the root, for each
node x encountered: O(log n)
 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
 Or, we’ve checked all nodes in the path log n
Time complexity for insertion O(log n)
AVL Tree
DELETION
Deletion from AVL Tree
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

Delete 5, Node 10 is unbalanced 50 50


Single Rotation
Deletion from AVL Tree
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.
• 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.
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).
L0 Rotation
Deleting 40
L-1 Rotation
Deleting 3
L1 Rotation
Deleting 3
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).
R0 Rotation
Deleting 72
R1 Rotation
Deleting 72
R-1 Rotation
Deleting 72

You might also like