AVL Tree
AVL 
Adelson, VelskiLandis
Height of a BST = 1 if empty
else it’s
1+ max of (left subtree height, right subtree height)
worst case for search/insertion/deletion  O(n log n)
AVL Balance
difference in subtree heights at any node <= 1
search in AVL tree = search in nonAVL BST
‘_’ equal high  left and subtree have same height
‘\’ right high  right subtree height is one more than left subtree height
‘/’ left high
Parent
Left
Data
Right
Balance
40
\
30
\
‘_’
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Parent
Left
Data
Right
Balance
50
10
20
30
‘\’
Insert
part 1: insert new node as in regular BST
part 2: walk back toward root along the insertion path and at each node
•
at each node
o
change balance factor if already balanced
o
rebalance if not balanced
empty tree > insert 20 > insert 25 > insert 30
rebalance at x
let Q be the root of taller subtree of x (child of x on the taller side)
case 1: Bf of x = Bf of Q
•
action: rotate Qx (left rotate if bfs are ‘/’, else right rotate)
case 2: rebalancing at a node
rotation time is O(1)
Algorithm
Let Q be taller subtree of x
case 1: if bf of x = bf of Q, then rotate Qx
case 2: if bf of x != bf of Q, let R be root of taller subtree of Q
•
rotate RQ
•
rotate Rx
height of an AVL tree = 1*4 log n
1.
search = O(log n)
2.
insert
a.
prove that worst case running time is O(log n), given that height is O(log n)
a.i.
search to failure: O(log n)
a.ii.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '07
 Sesh
 Data Structures, Tree traversal, worst case, Nested set model

Click to edit the document details