

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


































CS 334 Analysis of Algorithms
10/11/
Carola Wenk
Slides courtesy of Charles Leiserson with small
changes by Carola Wenk
CS 334 Analysis of Algorithms
10/11/
A binary search tree is a binary tree. Each node stores a key. The tree fulfills the
binary search tree property
:
For every node
x
holds:
left(x)
≤
x
, if
x
’s left child
left(x)
exists
x
≤
right(x)
, if
x
’s right child
right(x)
exists
8 8
1515
1010
1818
2222
33
77
1212
1717
CS 334 Analysis of Algorithms
10/11/
Abstract data type (ADT) Dictionary(also called Dynamic Set): A data structure which supports operations• Insert• Delete• FindUsing
balanced binary search trees
we can
implement a dictionary data structure such thateach operation takes
log
n)
time. 8 8
1515
1010
1818
2222
33
77
1212
1717
CS 334 Analysis of Algorithms
10/11/
Balanced search tree:
A search-tree data
structure for which a height of
(log
n
) is
guaranteed when implementing a dynamicset of
n
items.
Examples:
CS 334 Analysis of Algorithms
10/11/
h
8 8
1111
1010
1818
2626
2222
33
77
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
CS 334 Analysis of Algorithms
10/11/
8 8
1111
1010
1818
2626
2222
33
77
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
CS 334 Analysis of Algorithms
10/11/
8 8
1111
1010
1818
2626
2222
33
77
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
black.
CS 334 Analysis of Algorithms
10/11/
x
, excluding
x
, to a descendant leaf have the same number of black nodes =
black-height
x
8 8
1111
1010
1818
2626
2222
33
77
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
NIL
bh
= 2
bh
= 1
bh
= 1
bh
= 2
bh
= 0
CS 334 Analysis of Algorithms
10/11/
Theorem.
A red-black tree with n keys has height
h
2 log(
n
Proof.
(The book uses induction. Read carefully.)
NTUITION
into their blackparents.
CS 334 Analysis of Algorithms
10/11/
Theorem.
A red-black tree with n keys has height
h
2 log(
n
Proof.
(The book uses induction. Read carefully.)
NTUITION
into their blackparents.
CS 334 Analysis of Algorithms
10/11/
Theorem.
A red-black tree with n keys has height
h
2 log(
n
Proof.
(The book uses induction. Read carefully.)
NTUITION
into their blackparents.
CS 334 Analysis of Algorithms
10/11/
Theorem.
A red-black tree with
n
keys has height
h
2 log(
n
Proof.
(The book uses induction. Read carefully.)
has 2, 3, or 4 children.
h
of leaves.
NTUITION
into their blackparents.
h
CS 334 Analysis of Algorithms
10/11/
Corollary.
The queries S
EARCH
IN
AX
UCCESSOR
, and P
REDECESSOR
all run in
(log
n
) time on a red-black
tree with
n
nodes.
8 8
1111
1010
1818
2626
2222
33
77
NIL
NIL
NIL NIL NIL NIL
NIL
NIL NIL
CS 334 Analysis of Algorithms
10/11/
The operations I
NSERT
and D
ELETE
cause
modifications to the red-black tree:1. the operation itself,2. color changes,3. restructuring the links of the tree
via
“rotations”