AVL Tree - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

AVL Tree

Description:

... up with some nodes very deep. Strict balance. The tree must always ... Only nodes on the path from insertion point to root node have possibly changed in height. ... – PowerPoint PPT presentation

Number of Views:223
Avg rating:3.0/5.0
Slides: 56
Provided by: yaotin
Category:
Tags: avl | deep | insertion | tree

less

Transcript and Presenter's Notes

Title: AVL Tree


1
AVL Tree
Instructor Yao-Ting Huang
Bioinformatics Laboratory, Department of Computer
Science Information Engineering, National Chung
Cheng University.
2
Binary Search Trees
  • BST property keyleft(x) ? keyx ?
    keyright(x)
  • Example

3
Binary Search Tree - Best Time
  • All BST operations are O(h), where h is tree
    height.
  • Minimum h is for a binary tree
    with n nodes
  • What is the best case tree?
  • What is the worst case tree?

4
Balanced and unbalanced BST
4
1
2
2
5
3
1
3
4
4
5
2
6
6
7
5
7
1
3
5
Balance or Not Balance?
  • Don't balance
  • May end up with some nodes very deep
  • Strict balance
  • The tree must always be balanced perfectly
  • Pretty good balance
  • Only allow a little out of balance

6
Balancing Binary Search Trees
  • Many algorithms exist for keeping binary search
    trees balanced
  • Adelson-Velskii and Landis (AVL) trees
    (height-balanced trees)
  • Red-black trees
  • B-trees

7
Perfect Balance
  • Want a complete tree after every operation
  • Tree is nearly full except possibly in the lower
    right
  • This is expensive
  • For example, insert 2 in the tree on the left and
    then rebuild as a complete tree

6
Insert 2 complete tree
4
9
8
1
5
8
AVL Tree - Good but not Perfect Balance
  • AVL trees are height-balanced binary search
    trees.
  • Balance factor F of a node
  • F height(left subtree) - height(right subtree)
  • An AVL tree has balance factor F ?1.
  • For every node, heights of left and right subtree
    can differ by no more than 1.
  • What is the height of an AVL tree with n nodes?

9
Height of an AVL Tree
  • N(h) minimum number of nodes in an AVL tree of
    height h.
  • Lets try
  • N(0) 1, N(1) 2, N(2) 3,
  • Induction
  • N(h) N(h-1) N(h-2) 1
  • Solution (recall Fibonacci analysis)
  • N(h) gt ?h (? ? 1.62)

h
h-1
h-2
10
Height of an AVL Tree
  • N(h) gt ?h (? ? 1.62)
  • Suppose we have n nodes in an AVL tree of height
    h.
  • n gt N(h) because N(h) was the minimum
  • n gt ?h hence log? n gt h (relatively well
    balanced tree!!)
  • h lt O(log n)

11
Node Heights
Tree A (AVL)
Tree B (AVL)
height2 BF1-01
2
6
6
1
1
1
0
4
9
4
9
0
0
0
0
0
1
5
8
1
5
height of node h balance factor hleft -
hright empty height -1
12
Node Heights after Insert 7
Tree A (AVL)
Tree B (AVL)
balance factor 1-(-1) 2
2
3
6
6
1
1
2
1
4
9
4
9
-1
0
0
1
0
0
1
5
8
1
5
height of node h balance factor hleft -
hright empty height -1
13
Insert and Rotation in AVL Trees
  • Insert operation may cause balance factor F ? 2
    for some node.
  • Only nodes on the path from insertion point to
    root node have possibly changed in height.
  • So after the Insert, go back up to the root node
    by node and update heights.
  • If a new balance factor (the difference hleft -
    hright) is 2 or 2, adjust tree by rotation
    around the node.

14
Single Rotation in an AVL Tree
2
2
6
6
2
1
1
1
4
9
4
8
0
0
1
0
0
0
0
9
7
8
1
5
1
5
0
7
15
Insertions in AVL Trees
Let the node that needs rebalancing be ?. There
are 4 cases Outside Cases (require single
rotation) 1. Insertion into left subtree
of left child of ?. 2. Insertion into right
subtree of right child of ?. Inside Cases
(require double rotation) 3. Insertion
into right subtree of left child of ?. 4.
Insertion into left subtree of right child of ?.
The rebalancing is performed through four
separate rotation algorithms.
16
AVL Insertion Outside Case
j
Consider a valid AVL subtree
Inserting into X destroys the AVL property at
node j
k
h
h1
Z
h
h
X
Y
17
AVL Insertion Outside Case
j
Inserting into X destroys the AVL property at
node j
k
h2
h
Z
h1
h
Y
X
18
AVL Insertion Outside Case
j
Do a right rotation
k
h
Z
h1
h
Y
X
19
Single Right Rotation
j
Do a right rotation
k
h
Z
h1
h
Y
X
20
Outside Case Completed
Right rotation done! (Left rotation is
mirror symmetric)
k
j
h1
h
h
X
Z
Y
AVL property has been restored!
21

AVL Insertion Inside Case
j
Consider a valid AVL subtree
k
h
Z
h
h
X
Y
22
AVL Insertion Inside Case
j
Inserting into Y destroys the AVL property at
node j
Does right rotation restore balance?
k
h
Z
h1
h
X
Y
23
AVL Insertion Inside Case
k
Right rotation does not restore balance now k
is out of balance
j
h
X
h
h1
Z
Y
24
AVL Insertion Inside Case
j
Consider the structure of subtree Y
k
h
Z
h1
h
X
Y
25
AVL Insertion Inside Case
j
Y node i and subtrees V and W
k
h
Z
i
h1
h
X
h or h-1
W
V
26
AVL Insertion Inside Case
j
We will do a left-right double rotation . . .
k
Z
i
X
W
V
27
Double rotation first rotation
j
left rotation complete
i
Z
k
W
V
X
28
Double rotation second rotation
j
Now do a right rotation
i
Z
k
W
V
X
29
Double rotation second rotation
Right rotation complete
Balance has been restored
i
j
k
h
h
h or h-1
V
Z
W
X
30
Implementation
balance (1,0,-1)
key
left
right
No need to keep the height just the difference
in height, i.e. the balance factor this has to
be modified on the path of insertion even if you
dont perform rotations Once you have performed a
rotation (single or double) you wont need to go
back up the tree
31
Implementation of Single Rotation
RotateFromRight(n reference node pointer) p
node pointer p n.right n.right
p.left p.left n n p
n
n.right
X
You also need to modify the heights or balance
factors of n and p
Insert
Y
Z
32
Double Rotation
  • Implement Double Rotation in two lines.

DoubleRotateFromRight(n reference node pointer)
????
n
X
Z
V
W
33
Insertion in AVL Trees
  • Insert at the leaf (as for all BST)
  • Only nodes on the path from insertion point to
    root node have possibly changed in height
  • So after the Insert, go back up to the root node
    by node and update heights/balanced factor.
  • If a new balance factor (the difference
    hleft-hright) is 2 or 2, adjust tree by singe or
    double rotation around the node.

34
Insert in AVL trees
Insert(T reference tree pointer, x element)
if T null then T new tree T.data x
height 0 return case T.data x return
//Duplicate do nothing T.data gt x
Insert(T.left, x) if
((height(T.left)- height(T.right)) 2)
if (T.left.data gt x ) then //outside
case T RotatefromLeft
(T) else
//inside case T
DoubleRotatefromLeft (T) T.data lt x
Insert(T.right, x) code similar
to the left case Endcase T.height
max(height(T.left),height(T.right)) 1
return
35
Example of Insertions in an AVL Tree
2
20
1
0
10
30
0
0
35
25
Insert 5, 40, 45, 34
36
Example of Insertions in an AVL Tree
Insert 5, 40
2
20
1
1
10
30
0
0
0
35
5
25
37
Single rotation (outside case)
4
20
3
1
10
30
0
2
0
35
5
25
40
1
Imbalance
0
45
38
Double rotation (inside case)
4
Imbalance
20
3
1
10
30
0
2
0
40
5
25
1
45
35
0
0
34
Insertion of 34
39
Time Complexity of Insertion in AVL Tree
  • What is the time complexity of the insertion
    operation in AVL tree?

2
Insert 5, 40, 45, 34
20
1
0
10
30
0
0
35
25
40
Pros and Cons of AVL Trees
  • Arguments for AVL trees
  • Search is O(log n) since AVL trees are always
    balanced.
  • Insertion are also O(log n)
  • The height balancing adds no more than a constant
    factor to the speed of insertion.
  • Arguments against using AVL trees
  • Difficult to program debug more space for
    balance factor.
  • Asymptotically faster but rebalancing costs time.

41
Double Rotation Solution
DoubleRotateFromRight(n reference node pointer)
RotateFromLeft(n.right) RotateFromRight(n)
n
n.right
X
Z
V
W
42
AVL Trees Deletion
43
AVL Tree Deletion
  • Similar but more complex than insertion
  • Rotations and double rotations are still needed
    to rebalance the tree.
  • But imbalance may propagate upward so that many
    rotations are needed.

44
AVL Tree Delete
M
M
delete(L) requires a rotate left-right of node G
J
R
G
R
E
U
Q
L
E
U
Q
J
X
G
T
N
X
T
N
V
V
Notice that even after fixing J, M is still out
of balance
45
AVL Tree Deletion
  • An AVL deletion is similar to a regular binary
    tree delete
  • search for the node
  • remove it
  • zero children replace it with null
  • one child replace it with the only child
  • two children replace it with right-most node
    (Successor) in the left subtree

46
AVL Tree Delete
  • Complications arise from the fact that deleting a
    node can unbalance a number of its ancestors.
  • insert only required you find the first
    unbalanced node
  • delete will require you to go all the way back to
    the root looking for imbalances
  • We must balance any node with a 2 balance factor

47
Deletion from AVL Tree
  • Delete a node x as in ordinary binary search
    tree.
  • But 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.

48
Deletion Example (Single Rotation)
20
10
35
40
15
5
25
18
38
30
45
50
Delete 5, Node 10 is unbalanced
49
Deletion Example (Single Rotation)
20
15
35
25
40
18
10
38
30
45
50
Continue to check parents 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.W
50
Deletion Example (Double Rotation)
M
J
S
delete(L)
U
Q
L
N
Deleting a node in the left sub-tree (Ms balance
becomes 2). Need to rotate M with its right
sub-tree. Ss balance factor is 1 before rotate
(need double-rotation). Do a right rotate of Q
with S and then a left rotate of Q with M.
51
Deletion Example (Double Rotation)
M
J
S
delete(L)
U
Q
L
R
Deleting a node in the left sub-tree (Ms balance
becomes 2). Need to rotate M with its right
sub-tree. Ss balance factor is 1 before
rotate. Need to do a right rotate of Q with S and
then a left rotate of Q with M. Notice that the
height of the tree changes.
52
Deletion Example 2
Double rotation
Replace with the predecessor
53
Deletion Example 2 Contd
How to rotate?
54
One New Case
  • New case two subtrees of y are of the same height

rotate with left child
rotate with right child
55
Deletion Example 2 Contd
New case
Write a Comment
User Comments (0)
About PowerShow.com