1 cse 326: data structures trees lecture 7: wednesday, jan 23, 2003
Post on 22-Dec-2015
214 views
TRANSCRIPT
1
CSE 326: Data Structures Trees
Lecture 7: Wednesday, Jan 23, 2003
2
Outline
• Finish discussion on random binary search trees (BST)
• AVL trees
• Reading assignment for this week:Weiss: 4.3, 4.4, 4.5, and 4.7
3
The Average Depth of a BST
• Insert the elements 1 <2 < ... < n in some order, starting with the empty tree
• For each permutation, :– T = the BST after inserting (1), (2) , ... ,
(n)
• The Average Depth:))/n!height(T(H(n)
ππ
4
The Average Depth of a BST
• The average depth of a BST is:
H(n) = (log n)
• For some , height(T) = O(log n)• For other , height(T) = O(n)• But the average is O(log n)
• Please read the proof in the book and/or slides !
5
log versus n n
Why is average depth of BST's made from
random inputs different from the average
depth of all pos
log
Because there are more ways to build shallow
trees than d
sible
eep t
BST
ee
?
r
's
n n
s!
6
Random Input vs. Random Trees
Inputs
1,2,3
3,2,1
1,3,2
3,1,2
2,1,3
2,3,1
TreesFor three items, the shallowest tree is twice as likely as any other – effect
grows as n increases. For n=4,
probability of getting a shallow
tree > 50%
7
Average cost
• The average, amortized cost of n insert/find operations is O(log(n))
• But the average, amortized cost of n insert/find/delete operations can be as bad as (n)– Deletions make life harder (recall stretchy arrays)
• Need guaranteed cost O(log n) – next
8
Beauty is Only (log n) Deep
• Binary Search Trees are fast if they’re shallowe.g.: complete
• Problems occur when one branch is much longer than the otherHow to capture the notion of a “sort of” complete
tree?
9
Balance
balance = height(left subtree) - height(right subtree)• convention: height of a “null” subtree is -1• zero everywhere perfectly balanced• small everywhere balanced enough: (log n)
– Precisely: Maximum depth is 1.44 log n
t
56
10
AVL Tree (Adelson-Velskii Landis)
4
121062
115
8
14137 9
• Binary search tree properties
• Balance of every node is -1 b 1
• Tree re-balances itself after every insert or delete
15What is the balance of each node in this tree?
11
AVL Tree Data Structure
15
92 12
5
10
20
17
0
0
100
1 2
3 10
3
data
height
children
300
12
Not An AVL Tree
15
92 12
5
10
20
17
0
1
200
1 3
4 10
4
data
height
children
300
180
13
Bad Case #1
Insert(small)
Insert(middle)
Insert(tall)
T
M
S
0
1
2
14
Single Rotation
T
M
S
0
1
2
M
S T00
1
Basic operation used in AVL trees:
A right child could legally have its parent as its left child.
15
General Case: Insert Unbalancesa
X
Y
b
Z
h h - 1
h + 1 h - 1
h + 2a
X
Y
b
Zh-1 h - 1
h h - 1
h + 1
a
XY
b
Z
h
h - 1
h
h - 1
h + 1
16
Properties of General Insert + Single Rotation
• Restores balance to a lowest point in tree where imbalance occurs
• After rotation, height of the subtree (in the example, h+1) is the same as it was before the insert that imbalanced it
• Thus, no further rotations are needed anywhere in the tree!
17
Bad Case #2
Insert(small)
Insert(tall)
Insert(middle)
M
T
S
0
1
2
Why won’t a single rotation (bringing T up to the top) fix this?
18
Double Rotation
M
S T00
1
M
T
S
0
1
2
T
M
S
0
1
2
19
General Double Rotation
• Initially: insert into X unbalances tree (root height goes to h+3)• “Zig zag” to pull up c – restores root height to h+2, left subtree height
to h
a
Z
b
W
c
Y
a
Z
b
W
c
Y
h+1
h
h
h
h + 3
h + 2
hh
h+1
h + 2
h+1
h
XX
20
Another Double Rotation Case
• Initially: insert into Y unbalances tree (root height goes to h+2)• “Zig zag” to pull up c – restores root height to h+1, left subtree height
to h
a
Z
b
W
c
Y
a
Z
b
W
c
Y
h+1
h
h
h
h + 3
h + 2
hh
h+1
h + 2
h+1
h
XX
21
Insert Algorithm
• Find spot for value
• Hang new node
• Search back up looking for imbalance
• If there is an imbalance:“outside”: Perform single rotation and exit
“inside”: Perform double rotation and exit
22
AVL Insert AlgorithmNode insert(Comparable x, Node root){if ( root == NULL ) return new Node(x);if (x == root.key) return root;
if (x < root.key){root.left = insert( x, root.left );if (root unbalanced) { rotate... } }
else { // x > root.keyroot.right = insert( x,
root.right ); if (root unbalanced) { rotate... } }
root.height = max(root.left.height, root.right.height)+1;
return root;}
Node insert(Comparable x, Node root){if ( root == NULL ) return new Node(x);if (x == root.key) return root;
if (x < root.key){root.left = insert( x, root.left );if (root unbalanced) { rotate... } }
else { // x > root.keyroot.right = insert( x,
root.right ); if (root unbalanced) { rotate... } }
root.height = max(root.left.height, root.right.height)+1;
return root;}
23
Deletion (Really Easy Case)
2092
155
10
30173
121
0
100
2 2
3
00
Delete(17)
24
Deletion (Pretty Easy Case)
2092
155
10
30173
121
0
100
2 2
3
00
Delete(15)
25
Deletion (Pretty Easy Case cont.)
2092
175
10
303
121 100
2 2
3
00
Delete(15)
26
Deletion (Hard Case #1)
2092
175
10
303
121 100
2 2
3
00
Delete(12)
27
Single Rotation on Deletion
2092
175
10
303
1 10
2 2
3
00
3092
205
10
17
3
1 00
2 1
3
0
0
What is different about deletion than insertion?
28
Deletion (Hard Case)
Delete(9)
2092
175
10
303
121 220
2 3
4
0
33
15
130 0
1
0
20
30
12
33
15
13
1
0 0
110
180
29
Double Rotation on Deletion
2
3
0
202
175
10
30
121 22
2 3
4
33
15
13
1
0 0
111
0180
2052
173
10
30
120 220
1 3
4
33
15
13
1
0 0
111
01800
Not finished!
30
Deletion with Propagation
We get to choose whether to single or double rotate!
2052
173
10
30
120 220
1 3
4
33
15
13
1
0 0
111
0180
What different about this case?
31
Propagated Single Rotation
0
30
20
17
33
12
15
13
1
0
52
3
10
4
3 2
1 2 1
0 0 011
0
2052
173
10
30
120 220
1 3
4
33
15
13
1
0
111
0180
180
32
Propagated Double Rotation
0
17
12
11
52
3
10
4
2 3
1 0
0 0
2052
173
10
30
120 220
1 3
4
33
15
13
1
0
111
0180
151
0
20
30
33
1180
130
2
33
AVL Deletion Algorithm• Recursive1. If at node, delete it
2. Otherwise recurse to find it in
3. Correct heights
a. If imbalance #1,
single rotate
b. If imbalance #2 (or don’t
care),
double rotate
• Iterative1. Search downward for node, stacking parent nodes2. Delete node3. Unwind stack, correcting heights a. If imbalance #1, single rotate b. If imbalance #2
(or don’t care) double rotate
34
Pro:
• All operations guaranteed O(log N) • The height balancing adds no more than a
constant factor to the speed of insertion
Con:
• Space consumed by height field in each node• Slower than ordinary BST on random data
Can we guarantee O(log N) performance with less overhead? Splay trees next time
Pros and Cons of AVL Trees