Lecture 7 AVL Trees Linda Shapiro Spring 2016 Announcements HW2 due Wednesday Help sessions this week Monday amp Thursday Binary Search Trees and AVL Trees Last lecture Binary Search ID: 760587
Download Presentation The PPT/PDF document "CSE373: Data Structures & Algorithms" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CSE373: Data Structures & AlgorithmsLecture 7: AVL Trees
Linda ShapiroSpring 2016
Slide2Announcements
HW2 due WednesdayHelp sessions this weekMonday & Thursday: Binary Search Trees and AVL TreesLast lecture: Binary Search TreesToday… AVL Trees
Spring 2016
CSE373: Data Structures & Algorithms
2
Slide3Review: Binary Search Tree (BST)
4
12
10
6
2
11
5
8
14
13
7
9
Structure
property (binary tree)
E
ach node has
2
children
R
esult: keeps operations simple
Order
property
A
ll keys in left
subtree smallerthan node’s keyAll keys in right subtree largerthan node’s keyResult: easy to find any given key
Spring 2016
3
CSE373: Data Structures & Algorithms
Slide4BST: Efficiency of Operations?
Spring 2016
CSE373: Data Structures & Algorithms
4
Problem: operations may be inefficient if BST is unbalanced. Find, insert, deleteO(n) in the worst caseBuildTreeO(n2) in the worst case
How?
1
1
2
1
2
3
Slide5ObservationBST: the shallower the better!Solution: Require and maintain a Balance Condition thatEnsures depth is always O(log n) – strong enough!Is efficient to maintain – not too strong!
Spring 2016
5
CSE373: Data Structures & Algorithms
How can we make a BST efficient?
When we
build
the tree, make sure it’s balanced.
BUT
…Balancing a tree
only
at build time is insufficient because sequences of operations can eventually transform our carefully balanced tree into the
dreaded list
So, we also need to also
keep
the tree balanced as we perform
operations.
Slide6Potential Balance Conditions
Left and right subtrees of the roothave equal number of nodes2. Left and right subtrees of the roothave equal height
Too weak!Height mismatch example:
Too weak!Double chain example:
Spring 2016
6
CSE373: Data Structures & Algorithms
Slide7Potential Balance Conditions
Left and right subtrees of every nodehave equal number of nodesLeft and right subtrees of every nodehave equal height
Too strong!Only perfect trees (2n – 1 nodes)
Too strong!Only perfect trees (2n – 1 nodes)
Spring 2016
7
CSE373: Data Structures & Algorithms
Slide88
The AVL Balance Condition
Left and right subtrees of every node have heights differing by at most 1Definition: balance(node) = height(node.left) – height(node.right)AVL property: for every node x, –1 balance(x) 1 Ensures small depthThis is because an AVL tree of height hmust have a number of nodes exponential in h Thus height must be log(number of nodes). Efficient to maintainUsing single and double rotations
Adelson-Velskii and Landis
Spring 2016
CSE373: Data Structures & Algorithms
Slide99
The AVL Tree Data Structure
An AVL tree is a self-balancing binary search tree.Structural propertiesBinary tree property (same as BST)Order property (same as for BST)Balance property:balance of every node is between -1 and 1Result: Worst-case depth is O(log n) Named after inventors Adelson-Velskii and Landis (AVL)First invented in 1962
Spring 2016
CSE373: Data Structures & Algorithms
Slide1011
1
8
4
6
10
12
7
0
0
0
0
1
1
2
3
Is this an AVL tree?
Spring 2016
CSE373: Data Structures & Algorithms
10
Yes!
Because the
l
eft
and right
subtrees
of
every node
have
heights
differing by
at most 1
Slide113
11
7
1
8
4
6
2
5
0
0
0
0
1
1
2
3
4
Is this an AVL tree?
Spring 2016
CSE373: Data Structures & Algorithms
11
Nope!
T
he
l
eft
and right
subtrees
of
some nodes (e.g. 1, 4, 6) have
heights
that differ
by
more than 1
Slide12What do AVL trees give us?
If we have an AVL tree, then the number of nodes is an exponential function of the height.Thus the height is a log function of the number of nodes!And thus find is O(log n)But as we insert and delete elements, we need to:Track balanceDetect imbalanceRestore balance
Spring 2016
12
CSE373: Data Structures & Algorithms
Slide13An AVL Tree
20
9
2
15
5
10
30
17
7
0
0
0
0
1
1
2
2
3
Track height at all
times!
Spring 2016
CSE373: Data Structures & Algorithms
13
…
3
value
height
children
10
key
Node object
Slide14AVL tree operations
AVL find: Same as BST findAVL insert: First BST insert, then check balance and potentially “fix” the AVL treeFour different imbalance casesAVL delete: The “easy way” is lazy deletionOtherwise, do the deletion and then check for several imbalance cases (we will skip this)
Spring 2016
CSE373: Data Structures & Algorithms
14
Slide15Insert: detect potential imbalance
Insert the new node as in a BST (a new leaf)For each node on the path from the root to the new leaf, the insertion may (or may not) have changed the node’s heightSo after insertion in a subtree, detect height imbalance and perform a rotation to restore balance at that nodeAll the action is in defining the correct rotations to restore balanceFact that an implementation can ignore:There must be a deepest element that is imbalanced after the insert (all descendants still balanced)After rebalancing this deepest node, every node is balancedSo at most one node needs to be rebalanced
Spring 2016
15
CSE373: Data Structures & Algorithms
Slide16Case #1: Example
Spring 2016
16
CSE373: Data Structures & Algorithms
Insert(6)Insert(3)Insert(1)Third insertion violates balance propertyhappens to be at the rootWhat is the only way to fix this?
6
3
1
2
1
0
6
3
1
0
6
0
6
3
1
0
1
0
Slide17Fix: Apply “Single Rotation”
Single rotation: The basic operation we’ll use to rebalanceMove child of unbalanced node into parent positionParent becomes the “other” child (always okay in a BST!)Other subtrees move in only way BST allows (next slide)
Spring 2016
17
CSE373: Data Structures & Algorithms
3
1
6
0
0
1
6
3
0
1
2
AVL Property violated
at node 6
Child’s new-height = old-height-before-insert
1
Slide18The example generalized: Left of Left
Insertion into left-left grandchild causes an imbalance1 of 4 possible imbalance causes (other 3 coming up!)Creates an imbalance in the AVL tree (specifically a is imbalanced)
Spring 2016
18
CSE373: Data Structures & Algorithms
a
Z
Y
b
X
h
h
h
h+1
h+2
a
Z
Y
b
X
h+1
h
h
h+2
h+3
Slide19The general left-left case
So we rotate at aMove left child of unbalanced node into parent positionParent becomes the right childOther sub-trees move in the only way BST allows: using BST facts: X < b < Y < a < Z
Spring 2016
19
CSE373: Data Structures & Algorithms
A single rotation restores balance at the nodeTo same height as before insertion, so ancestors now balanced
a
Z
Y
b
X
h+1
h
h
h+2
h+3
b
Z
Y
a
h+1
h
h
h+1
h+2
X
Slide20Another example:
insert(16)
Spring 2016
20
CSE373: Data Structures & Algorithms
10
4
22
8
15
3
6
19
17
20
24
16
10
4
8
15
3
6
19
17
20
16
22
24
Slide21The general right-right case
Mirror image to left-left case, so you rotate the other wayExact same concept, but needs different code
Spring 2016
21
CSE373: Data Structures & Algorithms
a
Z
Y
X
h
h
h+1
h+3
b
h+2
b
Z
Y
a
X
h
h
h+1
h+1
h+2
Slide22Two cases to go
Unfortunately, single rotations are not enough for insertions in the left-right subtree or the right-left subtreeSimple example: insert(1), insert(6), insert(3)First wrong idea: single rotation like we did for left-left
Spring 2016
22
CSE373: Data Structures & Algorithms
3
6
1
0
1
2
6
1
3
1
0
0
Violates order
property!
Slide23Two cases to go
Unfortunately, single rotations are not enough for insertions in the left-right subtree or the right-left subtreeSimple example: insert(1), insert(6), insert(3)Second wrong idea: single rotation on the child of the unbalanced node
Spring 2016
23
CSE373: Data Structures & Algorithms
3
6
1
0
1
2
6
3
1
0
1
2
Still
unbalanced!
Slide24Sometimes two wrongs make a right
First idea violated the order propertySecond idea didn’t fix balanceBut if we do both single rotations, starting with the second, it works! (And not just for this example.)Double rotation: Rotate problematic child and grandchildThen rotate between self and new child
Spring 2016
24
CSE373: Data Structures & Algorithms
3
6
1
0
1
2
6
3
1
0
1
2
1
0
0
1
3
6
Slide25The general right-left case
Spring 2016
25
CSE373: Data Structures & Algorithms
a
X
b
c
h-1
h
h
h
V
U
h+1
h+2
h+3
Z
a
X
c
h-1
h+1
h
h
V
U
h+2
h+3
Z
b
h
c
X
h-1
h+1
h
h+1
V
U
h+2
Z
b
h
a
h
Slide26Comments
Like in the left-left and right-right cases, the height of the subtree after rebalancing is the same as before the insertSo no ancestor in the tree will need rebalancingDoes not have to be implemented as two rotations; can just do:
Spring 2016
26
CSE373: Data Structures & Algorithms
a
X
b
c
h-1
h
h
h
V
U
h+1
h+2
h+3
Z
c
X
h-1
h+1
h
h+1
V
U
h+2
Z
b
h
a
h
Easier to remember than you may think:
Move c to grandparent’s position
Put a, b, X, U, V, and Z in the only legal positions for a BST
Slide27The last case: left-right
Mirror image of right-leftAgain, no new concepts, only new code to write
Spring 2016
27
CSE373: Data Structures & Algorithms
a
h-1
h
h
h
V
U
h+1
h+2
h+3
Z
X
b
c
c
X
h-1
h+1
h
h+1
V
U
h+2
Z
a
h
b
h
Slide28Insert, summarized
Insert as in a BSTCheck back up path for imbalance, which will be 1 of 4 cases:Node’s left-left grandchild is too tallNode’s left-right grandchild is too tallNode’s right-left grandchild is too tallNode’s right-right grandchild is too tallOnly one case occurs because tree was balanced before insertAfter the appropriate single or double rotation, the smallest-unbalanced subtree has the same height as before the insertionSo all ancestors are now balanced
Spring 2016
28
CSE373: Data Structures & Algorithms
Slide29Example
20
9
2
15
5
10
30
17
7
0
0
0
0
1
1
2
2
3
Spring 2016
CSE373: Data Structures & Algorithms
29
Slide30Insert a 6
20
9
2
15
5
10
30
17
7
0
0
0
0
1
1
2
2
3
Spring 2016
CSE373: Data Structures & Algorithms
30
6
4
What’s the deepest node
t
hat
is unbalanced?
What’s the case?
What do we do?
left-left
Slide31Insert a 6
20
7
2
15
5
10
30
17
6
0
0
0
0
1
0
1
2
2
Spring 2016
CSE373: Data Structures & Algorithms
31
9
3
Slide32Insert a 13
20
7
2
15
5
10
30
17
6
0
0
0
0
1
0
1
2
2
Spring 2016
CSE373: Data Structures & Algorithms
32
9
3
13
Slide33Insert a 14
20
7
2
15
5
10
30
17
6
0
0
0
0
2
0
1
3
2
Spring 2016
CSE373: Data Structures & Algorithms
33
9
4
13
14
0
1
What is the
deepest
unbalanced node?
Slide34Insert a 14
20
7
2
15
5
10
30
17
6
0
0
0
0
2
0
1
3
2
Spring 2016
CSE373: Data Structures & Algorithms
34
9
4
13
14
0
1
What is the
deepest
unbalanced node?
Which of the four
cases is this?
Still left-left!
Single rotation
Slide35Insert a 14
20
7
2
15
5
10
30
17
6
0
0
0
1
2
0
1
2
Spring 2016
CSE373: Data Structures & Algorithms
35
9
3
13
14
0
1
0
Slide36Now efficiency
Worst-case complexity of find: O(log n)Tree is balancedWorst-case complexity of insert: O(log n)Tree starts balancedA rotation is O(1) and there’s an O(log n) path to rootTree ends balancedWorst-case complexity of buildTree: O(n log n)Takes some more rotation action to handle delete…
Spring 2016
36
CSE373: Data Structures & Algorithms
Slide37Pros and Cons of AVL Trees
Spring 2016
CSE373: Data Structures & Algorithms
37
Arguments for AVL trees:
All operations logarithmic worst-case because trees
are
always
balanced
Height
balancing adds no more than a constant factor to the speed of
insert
and
delete
Arguments against
AVL
trees
:
More di
fficult
to program &
debug [but done once in a library!]
More
space for height
field
Asymptotically faster but rebalancing
takes a little time
If
amortized
(later) logarithmic time is enough, use splay trees (also in the text)
Slide38Practice
Spring 2016
CSE373: Data Structures & Algorithms
38
Insert 30, 20, 15, 10, 25, 40, 1, 2, 3, 4, 5
30
20
15
20
30
15
10
25
40
1
20
10
30
1
15
25
40
2
3
Slide39Spring 2016
CSE373: Data Structures & Algorithms
39
20
10
30
15
25
40
2
1
3
4
Which is the deepest unbalanced node?
Which case is it?
left-right
(left child of unbalanced node,
right side of that left child)
Can you do it?
Slide40Spring 2016
CSE373: Data Structures & Algorithms
40