0% found this document useful (0 votes)
22 views22 pages

Lec 04

The document provides an overview of Binary Search Trees (BSTs) and their importance in maintaining balance for efficient operations. It discusses AVL trees, their properties, and the process of rotations to maintain balance during insertions and deletions. The lecture emphasizes the significance of balanced BSTs in achieving O(log n) time complexity for operations like insertion and search.

Uploaded by

screenwise121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views22 pages

Lec 04

The document provides an overview of Binary Search Trees (BSTs) and their importance in maintaining balance for efficient operations. It discusses AVL trees, their properties, and the process of rotations to maintain balance during insertions and deletions. The lecture emphasizes the significance of balanced BSTs in achieving O(log n) time complexity for operations like insertion and search.

Uploaded by

screenwise121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

6.

006- Introduction to Algorithms

m
co.
es
mot
cfon
psd.
stet
.njuo
wf
wpd
wst
.ijtu
wis
wv
we
or
rM

Lecture 4
Fo

Prof. Piotr Indyk

For More visit www.justpdfnotes.com


Lecture Overview
1
5

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

For More visit www.justpdfnotes.com


Binary Search Trees (BSTs)

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

– For all nodes y in the right subtree of x: 7


Fo

key[y] ≥ key[x]
leaf

height = 3

For More visit www.justpdfnotes.com


The importance of being balanced
for n nodes:

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)

For More visit www.justpdfnotes.com


Balanced BST Strategy

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

heights of its left child and right


child differ by at most 1
k k-1

For More visit www.justpdfnotes.com


AVL trees have height Θ(log n)
Invariant: for every node x, the heights of its left
child and right child differ by at most 1

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

• The constant “2” can be improved


Fo

How can we maintain the invariant ?


For More visit www.justpdfnotes.com
Rotations
B RIGHT-ROTATE(B) A

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

For More visit www.justpdfnotes.com


Insertions

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

• Similar issue/solution when


rM

u
Fo

deleting a node

For More visit www.justpdfnotes.com


Balancing

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

– Case 1: Right child y of x is


rM

right-heavy
Fo

– Case 2: Right child y of x is


balanced
– Case 3: Right child y of x is
left-heavy
For More visit www.justpdfnotes.com
Case 1: y is right-heavy

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

For More visit www.justpdfnotes.com


Case 2: y is balanced

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

For More visit www.justpdfnotes.com


Case 3: y is left-heavy

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 …

For More visit www.justpdfnotes.com


Fo
rM
or
we
wv
wis
.ijtu
wst
wpd
wf
.njuo
stet
psd.

For More visit www.justpdfnotes.com


cfon
mot
es
. co
m
Rotations
B RIGHT-ROTATE(B) A

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

For More visit www.justpdfnotes.com


Case 3: y is left-heavy
RIGHT-ROTATE (y)

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

And we are done!

For More visit www.justpdfnotes.com


Conclusions

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

For More visit www.justpdfnotes.com


Examples of insert/balancing
Insert(23) x = 29: left-left case

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

x=65: left-right case Done


Fo

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 φ

For More visit www.justpdfnotes.com


Balanced Search Trees …

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

• Treaps (Seidel and Aragon 1996)


we
or
rM

• ….
Fo

For More visit www.justpdfnotes.com


BST for runway reservation system
5-1

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

• 44 => reject (46 in R) 41 56


Fo

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.

For More visit www.justpdfnotes.com


cfon
mot
es
.
1

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

For More visit www.justpdfnotes.com

You might also like