0% found this document useful (0 votes)
9 views51 pages

Lec 14 AVL Edited

Uploaded by

f230599
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)
9 views51 pages

Lec 14 AVL Edited

Uploaded by

f230599
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/ 51

CS-2001

Data Structures
Fall 2023

Balanced Tree (AVL)

Rizwan Ul Haq
Assistant Professor
FAST-NU
[email protected]
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

FAST-NU, Faisalabad 2
Balanced Tree
• Want a (almost) complete tree after every operation
• Tree is full except possibly in the lower right

• Maintenance of such as tree is expensive


• For example, insert 2 in the tree on the left and then rebuild as a complete
tree
6 5
Insert 2 &
4 9 complete tree 2 8

1 5 8 1 4 6 9

FAST-NU, Faisalabad 3
AVL Trees – Good but not Perfect Balance
• Named after Adelson, Velskii and Landis

• Balance is defined by comparing the height of the two sub-


trees

• Recall:
• An empty tree has height –1
• A tree with a single node has height 0

FAST-NU, Faisalabad 4
AVL Trees
• A binary search tree is said to be AVL balanced if:
• The difference in the heights between the left and right sub-trees
is at most 1, and
• Both sub-trees are themselves AVL trees

• AVL trees with 1, 2, 3 and 4 nodes

FAST-NU, Faisalabad 5
Proposition for AVL
• Proposition: Let T be an AVL tree and x be a node in T and
hleft and hright be the height of left and right sub tree
respectively. Then |hleft - hright| ≤ 1, where |hleft - hright|
denotes the absolute value of hleft - hright.
• Let x be a node in the AVL tree T.
• If hleft > hright, we say that x is left high. In this case, hleft = hright + 1
• If hleft = hright, we say that x is equal high
• If hright > hleft, we say that x is right high. In this case, hright = hleft + 1

FAST-NU, Faisalabad 6
AVL Trees – Example
5 7

2 8 2 8

1 4 7 1 4

3 3 5

An AVL Tree Not an AVL Tree

FAST-NU, Faisalabad 7
AVL Trees – Balance Factor
• An AVL tree has balance factor calculated at every node
• Height of the left subtree minus the height of the right subtree
• For an AVL tree, the balances of the nodes are always -1, 0 or 1.

Height of node =h
Balance Factor (BF) = hleft - hright
Empty height = -1
Tree A (AVL) Tree B (AVL)
Height = 2 BF = 1-0 = 1
1
6 6
4 9 0 4 9
0 0
1 5 -1 -1 1 5 8 -1

-1 -1 -1 -1 FAST-NU, Faisalabad -1 -1 -1 -1 -1 -1 8
AVL Trees – Example
• Here is a larger AVL tree (42 nodes)

FAST-NU, Faisalabad 9
AVL Trees – Example
• The root node is AVL-balanced
• Both sub-trees are of height 4 (i.e., at root BF = 0)

FAST-NU, Faisalabad 10
AVL Trees – Example
• All other nodes (e.g., AF and BL) are AVL balanced
• The sub-trees differ in height by at most one

FAST-NU, Faisalabad 11
AVL Trees – Example
• Consider this AVL tree

FAST-NU, Faisalabad 12
AVL Trees – Example
• Consider inserting 15 into this tree
• In this case, the heights of none of the trees change
• Tree remains balanced

FAST-NU, Faisalabad 13
AVL Trees – Example
• Consider inserting 42 into this tree

FAST-NU, Faisalabad 14
AVL Trees – Example
• Consider inserting 42 into this tree
• Height of two sub-trees rooted at 44 and 38 have increased by one
• The tree is still balanced

FAST-NU, Faisalabad 15
AVL – Tree Example

Tree A (AVL) Tree B (not AVL)


3 balance factor
1-(-1) = 2
6 6
2
4 9 4 9
1 -1
1 5 8 1 5 8
0
7
Height of node = h
Balance factor = hleft-hright
Empty height = -1

FAST-NU, Faisalabad 16
AVL Trees
• To maintain the height balanced property of the AVL tree, it
is necessary to perform a transformation on the tree so that

• In-order traversal of the transformed tree is the same as for


the original tree (i.e., the new tree remains a binary search
tree).
• perform a transformation on the tree

FAST-NU, Faisalabad 17
Transformation (Rotation) of AVL Trees
• Insert operations may cause balance factor to become 2 or –2 for
some node

• Only nodes on the path from insertion point to root node have possibly
change in height

• Follow the path up to the root, find the first node (i.e., deepest) whose
new balance violates the AVL condition

• If a’s new balance factor (the difference hleft – hright) is not [-1,0,1]
• Adjust tree by rotation around the node a

FAST-NU, Faisalabad 18
Balancing AVL Trees – Example
• If a tree is AVL balanced, for an insertion to cause an
imbalance:
• The heights of the sub-trees must differ by 1
• The insertion must increase the height of the deeper sub-tree by 1

FAST-NU, Faisalabad 19
Balancing AVL Trees – Example
• Suppose we insert 23 into our initial tree

FAST-NU, Faisalabad 20
Balancing AVL Trees – Example
• The heights of each of the sub-trees from the insertion point
to the root are increased by one

FAST-NU, Faisalabad 21
Balancing AVL Trees – Example
• Only two of the nodes are unbalanced, i.e., 17 and 36
• Balance factor of 17 is -2
• Balance factor of 36 is 2

FAST-NU, Faisalabad 22
Balancing AVL Trees – Example
• We only have to fix the imbalance at the lowest node

FAST-NU, Faisalabad 23
Fixing Imbalance By Rotation
• Let the node that needs rebalancing be a
• Imbalance during insertion may be handled using four cases

• Outside cases (Single Rotation)


1. Right rotation (case RR)
2. Left rotation (case LL)

• Inside cases (Double Rotation)


3. Right-Left rotation (case RL)
4. Left-Right rotation (case LR)

FAST-NU, Faisalabad 24
Single Rotation in an AVL Tree
• After performing single right rotation the tree became and
AVL again 3
6 2
1 2 6
4 9 1 1

4 8
0 0 1
1 5 8 0 0 0 0
1 5 7 9
0
7
FAST-NU, Faisalabad 25
Right Rotation (RR) in an AVL Tree
• Node b will takes place of node c (it becomes root now)
• Node c will become root of the right sub-tree ( or right child
of new root)
• If there was any right child of node b, it will become left child
of node c now.
c b

b
a c
a
FAST-NU, Faisalabad 26
Right Rotation (RR) – Example
• Consider adding 6

FAST-NU, Faisalabad 27
Right Rotation (RR) – Example
• Height of each of the trees in the path back to the root are
increased by one

FAST-NU, Faisalabad 28
Right Rotation (RR) – Example
• Height of each of the trees in the path back to the root are
increased by one
• Only root node (i.e., 36) violates the balancing factor

FAST-NU, Faisalabad 29
Right Rotation (RR) – Example
• To fix the imbalance, we perform right rotation of root (i.e.,
36)

FAST-NU, Faisalabad 30
When to Perform Right Rotation (RR)
• Let the node that needs rebalancing be a
• Case RR
• Insertion into left subtree of left child of node a
• Left tree is heavy (i.e., hleft- hright > [-1,0,1])

FAST-NU, Faisalabad 31
When to Perform Right Rotation (RR)
• Let the node that needs rebalancing be a
• Case RR
• Insertion into left subtree of left child of node a (RR)
• Left tree is heavy (i.e., hleft > hright )

FAST-NU, Faisalabad 32
Right Rotation (RR) – Examples

FAST-NU, Faisalabad 33
AVL Rotation
Left-Left Rotation (LL)

FAST-NU, Faisalabad 34
Left Rotation (LL) in an AVL Tree
• Node b will take place of node a (it will become new Root).
• Node a will become root of left sub-tree(or left child of new
Root)
• If there was any left child of node b, it will become right child
of node a now
a
b

b
a c
c FAST-NU, Faisalabad 35
Left Rotation (LL) – Example

FAST-NU, Faisalabad 36
When to Perform Left Rotation (LL)
• Let the node that needs rebalancing be a
• Case LL
• Insertion into right subtree of right child of node a (LL)
• Right tree is heavy (i.e., hleft < hright )

FAST-NU, Faisalabad 38
Double Rotations
Left-Right (LR)
Right-Left (RL)

FAST-NU, Faisalabad 39
Single Rotation may be Insufficient
• c becomes the new root. • a becomes the new root.
• a takes ownership of c's left child • c takes ownership of a's right
as its right child, in this case, b. child as its left child, b.
• c takes ownership of a as its left • a takes ownership of c as its
child. right child.

a C a

c Single Left
a Single Right c

b b b
FAST-NU, Faisalabad 40
Left-Right Rotation (LR) or Double Lefts
• perform a right rotation on the right • b becomes the new root.
subtree. • a takes ownership of b's left child as
its right child, in this case null
• b takes ownership of a as its left child

a
a
b
c Single Right b Single Left

b a c
c
FAST-NU, Faisalabad 41
Left-Right Rotation (LR) or "Double Left" – Example
• Consider adding 67
• To fix the imbalance, we perform left-right (LR) rotation of root

FAST-NU, Faisalabad 42
When to Perform Left-Right Rotation (LR)
• Let the node that needs rebalancing be a
• Case LR
• Insertion into left subtree of right child of node a

FAST-NU, Faisalabad 43
When to Perform Left-Right Rotation (LR)
• Let the node that needs rebalancing be a
• Case LR
• Insertion into left subtree of right child of node a (LR)

Right heavy

Left heavy

FAST-NU, Faisalabad 44
Right-Left Rotation (RL) or "Double Right"
• perform a left rotation on the • b becomes the new root.
left subtree. • c takes ownership of b's right
child as its left child, in this case
null.
• b takes ownership of c as its
right child.
c
c b
a Single left Single right
b
b a c
a
FAST-NU, Faisalabad 45
Right-Left Rotation (RL) or "Double Right" –
Example

FAST-NU, Faisalabad 46
When to Perform Right-Left Rotation (RL)
• Let the node that needs rebalancing be a
• Case RL
• Insertion into right subtree of left child of node a

FAST-NU, Faisalabad 47
When to Perform Right-Left Rotation (RL)
• Let the node that needs rebalancing be a
• Case RL
• Insertion into right subtree of left child of node a (RL)

Left heavy

Right heavy

FAST-NU, Faisalabad 48
Summary: How And When To Rotate?
• Let the node that needs rebalancing be a
• Violation during insertion may occur in four
cases
• Outside cases (Single Rotation)
1. Insertion into left subtree of left child of node a
(case RR) Case 1: RR

2. Insertion into right subtree of right child of node


a (case LL)
Case 2: LL

FAST-NU, Faisalabad 49
Summary: How And When To Rotate?
• Let the node that needs rebalancing be a
• Violation during insertion may occur in
four cases
• Inside cases (Double Rotation)
3. Insertion into right subtree of left child of Case 3: RL
a (case RL)

4. Insertion into left subtree of right child of


a (case LR)
Case 4: LR

FAST-NU, Faisalabad 50
Working Example
• Example: 40 30 20 10 5 15 65 33 31

FAST-NU, Faisalabad 51
Exercise-I
• Insert the following data into the empty AVL tree, “Mar, May,
Nov, Aug, Apr, Jan, Dec, July, Feb, June, Oct, Sept”

FAST-NU, Faisalabad 52

You might also like