Lec 04
Lec 04
m
co.
es
mot
cfon
psd.
stet
.njuo
wf
wpd
wst
.ijtu
wis
wv
we
or
rM
Lecture 4
Fo
m
co
6
.
es
• Review: Binary Search Trees
mot
7
cfon
psd.
• Importance of being balanced
stet
10
.njuo
wf
wpd
• Balanced BSTs
wst
wis
.ijtu 12
– AVL trees
wv
7
we
or
• definition
rM
Fo
5 10
• rotations, insert
1 6 12
m
co
• Each node x has: root
.
es
mot
– key[x]
cfon
psd.
– Pointers: left[x], right[x], p[x] 10
stet
.njuo
wf
wpd
5 12
• Property: for any node x:
wst
.ijtu
– For all nodes y in the left subtree of x:
wis
1 6
wv
key[y] ≤ key[x]
we
or
rM
key[y] ≥ key[x]
leaf
height = 3
m
co.
es
1
mot
cfon
7
psd.
5
stet
.njuo
6
wf
wpd
5 10
wst
7 .ijtu
wis
wv
1 6 12
we
10
or
rM
12
Fo
h = Θ(log n) h = Θ(n)
m
co
• Augment every node
.
es
mot
with some data
cfon
psd.
stet
• Define a local invariant
.njuo
wf
wpd
on data
wst
.ijtu
• Show (prove) that
wis
wv
we
invariant guarantees
or
rM
Θ(log n) height
Fo
• Design algorithms to
maintain data and the
invariant For More visit www.justpdfnotes.com
AVL Trees: Definition
[Adelson-Velskii and Landis’62]
m
7 32
co
• Data: for every node, maintain its
.
es
height (“augmentation”)
mot
5 21 10 1
cfon
psd.
stet
.njuo
– Leaves have height 0
wf
1 10 6 0 12 0
wpd
– NIL has “height” -1
wst
.ijtu
wis
wv
4 0
we
or
rM
x
• Invariant: for every node x, the
Fo
m
co
• Let nh be the minimum number of
.
es
nodes of an AVL tree of height h
mot
h
cfon
psd.
• We have nh ≥ 1+ nh-1 + nh-2
stet
.njuo
wf
⇒ nh > 2nh-2
wpd
h-1 h-2
wst
.ijtu
⇒ nh > 2h/2
wis
wv
we
⇒ h < 2 lg nh
or
rM
m
co
A LEFT-ROTATE(A) B
.
es
mot
cfon
γ α
psd.
stet
.njuo
α β β γ
wf
wpd
wst
Rotations maintain the inorder ordering of keys:
.ijtu
wis
• a ∈ α, b ∈ β, c ∈ γ ⇒ a ≤ A ≤ b ≤ B ≤ c.
wv
we
or
rM
1
Fo
2
LEFT-ROTATE(1) 2
1 3
m
co
• Insert new node u as in the
.
es
mot
simple BST h+1
cfon
psd.
stet
– Can create imbalance
.njuo
wf
wpd
• Work your way up the tree, h-1 h-2
wst
.ijtu
restoring the balance
wis
wv
we
or
u
Fo
deleting a node
m
• Let x be the lowest “violating”
co
x
.
node
es
mot
– We will fix the subtree of x and
cfon
y
psd.
move up
stet
A
.njuo
• Assume the right child of x is
wf
wpd
deeper than the left child of x (x is
B C
“right-heavy”) wst
.ijtu
wis
• Scenarios:
wv
we
or
right-heavy
Fo
m
x y
co
LEFT-ROTATE(x)
.
es
x
ot
y
m
k+1
cfon
k-1 k
psd.
A C
stet
.njuo
k
wf
wpd
k-1 B C k-1 A B k-1
wst
.ijtu
wis
wv
we
or
rM
Fo
m
x y
co
LEFT-ROTATE(x)
.
es
x
ot
y
m
k+1
cfon
k-1 k
psd.
A C
stet
.njuo
k
wf
wpd
k B C k-1 A B k
wst
.ijtu
wis
wv
we
or
rM
Fo
Same as Case 1
m
x y
co
LEFT-ROTATE(x)
.
es
x
ot
y
m
k+1
cfon
k-1 k-1
psd.
A C
stet
.njuo
k-1
wf
wpd
k B C k-1 A B k
wst
.ijtu
wis
wv
we
or
rM
Fo
Need to do more …
m
co
A LEFT-ROTATE(A) B
.
es
mot
cfon
γ α
psd.
stet
.njuo
α β β γ
wf
wpd
wst
Rotations maintain the inorder ordering of keys:
.ijtu
wis
• a ∈ α, b ∈ β, c ∈ γ ⇒ a ≤ A ≤ b ≤ B ≤ c.
wv
we
or
rM
1
Fo
2
LEFT-ROTATE(1) 2
1 3
m
x
LEFT-ROTATE(x)
co.
es
y
ot
k+1
m
k-1 z
cfon
psd.
A
stet
y
.njuo
k x
k z k-1
wf
C
wpd
wst
.ijtu k-1
k-1
wis
or
wv
k-1 A B D C
we
k-2
or
B D
rM
or
k-2 k-1
Fo
m
co
• Can maintain balanced BSTs in O(log n)
.
es
mot
time per insertion
cfon
psd.
stet
• Search etc take O(log n) time
.njuo
wf
wpd
wst
.ijtu
wis
wv
we
or
rM
Fo
m
3 41 41
co
2 20 65 1 20 65 1
.
es
50 φ
mot
50 φ
φ 11 1 29 φ 11 2 29
cfon
psd.
26 1 26
φ
stet
φ 23
.njuo
Done Insert(55)
wf
wpd
3 41 3 41
wst
2 20 65 1
2 20 .ijtu 65 1
wis
50 φ 50 φ
φ 11
wv
φ 11 1 26 1 26
we
or
φ 23 29 φ φ 23 29 φ
rM
41 3 41
2 20 2 65 2 20 1 55
50 1 φ 50 φ 65
11 1 26 11 1 26
φ φ
55 φ
φ 23 29 φ φ 23 29 φ
m
co
• AVL trees (Adelson-Velsii and Landis 1962)
.
es
mot
cfon
• Red-black trees (see CLRS 13)
psd.
stet
.njuo
• Splay trees (Sleator and Tarjan 1985)
wf
wpd
• wst
Scapegoat trees (Galperin and Rivest 1993)
.ijtu
wis
wv
• ….
Fo
49
• R = (37, 41, 46, 49, 56) current landing times
m
3-1
co
1
37 41 46 49 56
.
es
time (mins) 41 56
mot
cfon
now x x x x 1 1
psd.
stet
37 46
.njuo
• remove t from the set when a plane lands
wf
wpd
4+1
wst
R = (41, 46, 49, 56) .ijtu
49
wis
• add new t to the set if no other landings are
wv
we
2 1+1
scheduled within < 3 minutes from t
or
rM
1 1
• 53 => ok
• delete, insert, conflict checking take O(h), where 46 53
h is the height of the tree
For More visit www.justpdfnotes.com
Fo
rM
or
we
wv
wis
.ijtu
wst
wpd
wf
.njuo
stet
psd.
co
m
1
4
25
0
6
7
0
3
110
0 12
m
co
And some people
.
es
mot
cfon
psd.
stet
like to do nothing
.njuo
wf
wpd
wst
.ijtu
wis
wv
we
or
rM
Fo