Lec-13 AVL Trees
Transcript of Lec-13 AVL Trees
-
8/3/2019 Lec-13 AVL Trees
1/21
AVL Trees
-
8/3/2019 Lec-13 AVL Trees
2/21
AVL TreesFor some binary search tree, the heightcould be quite large, which is not good
for searching.
An AVL tree is a binary search tree witha balance condition
To ensure that the height of the tree is
O(logN)For every node in the AVL tree, the height
of left subtree and the height of the right
subtree can differ by at most one.
-
8/3/2019 Lec-13 AVL Trees
3/21
AVL Trees
The height of the left subtree minus the
height of the right subtree of a node is
called the balance of the node. For an
AVL tree, the balances of the nodes arealways -1, 0 or 1.
The height of an empty tree is defined to
be -1.Given an AVL tree, if insertions or
deletions are performed, the AVL tree
may notremain height balanced.
-
8/3/2019 Lec-13 AVL Trees
4/21
AVL Trees
5
2 8
1 4
3
7
An AVL Tree
7
2 8
1 4
3 5
Not an AVL Tree
-
8/3/2019 Lec-13 AVL Trees
5/21
AVL Trees
To maintain the height balancedproperty of the AVL tree after insertionor deletion, it is necessary to perform a
transformation on the tree so that(1) the inorder traversal of thetransformed tree is the same as for theoriginal tree (i.e., the new tree remains
a binary search tree).(2) the tree after transformation is heightbalanced.
-
8/3/2019 Lec-13 AVL Trees
6/21
After an insertion, only nodes that are on
the path from the insertion point to the
root might have their balance altered
Follow the path up to the root, find the
first node (i.e., deepest) whose new
balance violates the AVL condition. Call
this node a.
Rebalance the tree at node a.
This guarantees that the entire tree is
balanced.
AVL Trees
-
8/3/2019 Lec-13 AVL Trees
7/21
AVL Trees
Violation may occur when an
insertion into
1. left subtree of left child ofa (LL case)
2. right subtree of left child ofa (LR
case)
3. left subtree of right child ofa (RL
case)
4. right subtree of right child ofa (RR
case)
-
8/3/2019 Lec-13 AVL Trees
8/21
AVL Trees
Rotation
- to restore the AVL tree after
insertion- single rotation
For case 1 and 4
- double rotationFor case 2 and 3
-
8/3/2019 Lec-13 AVL Trees
9/21
AVL Trees: Single Rotation
Example: 3 2 1 4 5 6 7
construct binary search tree without
height balanced restriction
depth of tree = 4
i.e. LL casei.e. LL case
-
8/3/2019 Lec-13 AVL Trees
10/21
AVL Trees: Single Rotation
Construct AVL tree (height balanced)
-
8/3/2019 Lec-13 AVL Trees
11/21
AVL Trees: Single Rotation
Insert 4, 5Insert 4, 5
Insert 6Insert 6
-
8/3/2019 Lec-13 AVL Trees
12/21
AVL Trees: Single Rotation
Insert 7Insert 7
-
8/3/2019 Lec-13 AVL Trees
13/21
AVL Trees: Double Rotation
Single rotation fails to fix cases 2 and 3
(i.e. LR and RL cases)
-
8/3/2019 Lec-13 AVL Trees
14/21
-
8/3/2019 Lec-13 AVL Trees
15/21
AVL Trees: Double Rotation
case 3 (RL case)
-
8/3/2019 Lec-13 AVL Trees
16/21
AVL Trees: Double Rotation
Example:
-
8/3/2019 Lec-13 AVL Trees
17/21
AVL Trees: Double Rotation
Insert 14
-
8/3/2019 Lec-13 AVL Trees
18/21
AVL Trees: Double Rotation
Insert 13 (This is single rotation: RR
Case)
-
8/3/2019 Lec-13 AVL Trees
19/21
AVL Trees: Double Rotation
Insert 12
-
8/3/2019 Lec-13 AVL Trees
20/21
AVL Trees: Double Rotation
Insert 11 and 10 (single rotation), then 8
-
8/3/2019 Lec-13 AVL Trees
21/21
AVL Trees: Double Rotation
Inserting 9