Professional Documents
Culture Documents
Categories of BSTs
E.G.M. Petrakis
AVL Trees
AVL (Adelson, Lelskii, Landis): the height
E.G.M. Petrakis
1)
2
AVL Trees
AVL Trees
E.G.M. Petrakis
//
\\
E.G.M. Petrakis
T3
T1
E.G.M. Petrakis
T1
T2
T2
T3
T1
T2
E.G.M. Petrakis
T3
T1
T3
T2
insert 1
single right
rotation
1
4
4
insert 9
9
E.G.M. Petrakis
10
5
4
11
E.G.M. Petrakis
T4
T4
T3
T2
E.G.M. Petrakis
or
T1
T3
T4
T2
T3
or
T1
or in Main Memory
Trees
10
Critical node
4 \\
4
2
2 =
/ 8 =
2
6
/ 7 =
insert 6
E.G.M. Petrakis
11
T3
T4
T2
T3
E.G.M. Petrakis
or
T4
T1
T1
T1
T2
T3
or
T2
Trees in MainorMemory
12
13
Rebalancing
For every node reached coming up from its
left sub-tree after insertion readjust
balance factor
= becomes / => no operation
\ becomes = => no operation
/ becomes // => must be rebalanced!!
14
Rebalancing (cont.)
The balance factor of the critical node
determines what rotation is to take place
single or double
15
Performance
Performance of membership operations on
AVL trees:
easy for the worst case!
16
E.G.M. Petrakis
17
Fibonacci Trees
Th: tree of height h
Th has two sub-trees, one with height h-1
and one with height h-2
else it wouldnt have minimum number of nodes
E.G.M. Petrakis
18
Nh number of nodes of Th
Nh = Nh-1 + Nh-2 + 1
N0 = 1
N1 = 2
1
Nh
5
h 2
1 5
h 2
1 5
19
More Examples
single rotation
7
insert 9
9
E.G.M. Petrakis
20
Examples (cont.)
double rotation
7
insert 7
E.G.M. Petrakis
7
Trees in Main Memory
21
Examples (cont.)
double rotation
8
8
6
6
insert 7
E.G.M. Petrakis
7
Trees in Main Memory
22
Examples (cont.)
single rotation
7
7
6
8
9
E.G.M. Petrakis
delete 6
23
Examples (cont.)
single rotation
5
6
8
8
7
9
7
delete 5
E.G.M. Petrakis
24
Examples (cont.)
double rotation
5
6
8
8
7
delete 5
E.G.M. Petrakis
25
General Deletions
5
5
3
6
E.G.M. Petrakis
2
delete 4
1
10
11
delete 8
10
11
26
5
2
delete 8
10
delete 5
delete 6
1
E.G.M. Petrakis
10
11
11
27
10
11
9
E.G.M. Petrakis
28
current node
20
Search(10)
30
15
second rotation
13
14
14
20
10
30
15
first rotation 10
13
10
20
10
30
15
13
third rotation
20
15
30
13
14
14
E.G.M. Petrakis
30
Splay Cases
b
E.G.M. Petrakis
a
Trees in Main Memory
c
31
q
c
b
E.G.M. Petrakis
RL
gp
gp
LL
c
q
b c
LL symmetric of RR
RL symmetric of RL
b
gp
p
d
32
current
node
5
E.G.M. Petrakis
LL
RR
7
6
33
1
a
4
3
RL
3
7
q
5
LR
6
e
f
g
a, b, c are sub-trees
E.G.M. Petrakis
34
5
1
a
2
b
E.G.M. Petrakis
4
3
i
7
f
e
35
Splay Performance
Splay trees adapt to unknown or changing
probability distributions
Splay trees do not guarantee logarithmic
cost for each access
36
Optimal BST
Static environment: no insertions or
deletions
Keys are accessed with various
frequencies
Have the most frequently accessed
keys near the root
Application: a symbol table in main
memory
E.G.M. Petrakis
37
Searching
Given symbols a1 < a2 < .< an and their
probabilities: p1, p2, pn minimize
cost
n
Successful search cos t pi level (ai )
i 1
E1
Ei= (i , i+1 )
E.G.M. Petrakis
E2
Ei
E0= (- , 1 )
Trees in Main Memory
En
En= (n , )
38
Unsuccessful Search
an
an-1
an-2
Ei
an-3
E.G.M. Petrakis
39
Search Cost
If pi is the probability to search for
ai and qi is the probability to search in
Ei then
n
n
pi qi 1
i 1
i 1
i 1
i 1
E.G.M. Petrakis
unsuccessful
successful
search
search
Trees in Main Memory
40
Optimal Tree
The binary search tree with the least cost
Minimizes the average cost for searching
given the search probabilities for a static
set of given keys
The nodes are known in advance
No insertions or deletions
41
Example
(a1, a2, a3) = (do, if, read)
p i = q i = 1/7
if
do
ifif
read
do
read
if
read
do
cost = 13/7
Optimal BST
cost = 15/7
E.G.M. Petrakis
cost = 15/7
42
read
do
read
do
if
if
cost = 15/7
cost = 15/7
E.G.M. Petrakis
43
Observation 1
In a BST, a subtree has nodes
that are consecutive in a sorted
sequence of keys (e.g. [5,26])
20
10
5
13
12
E.G.M. Petrakis
25
24
26
14
Trees in Main Memory
44
Observation 2
If Tij is a sub-tree of an optimal
BST holding keys from i to j then
Tij must be optimal among all
possible BSTs that store the same
keys
optimality lemma: all sub-trees of
an optimal BST are also optimal
E.G.M. Petrakis
45
10
0,2
20
0,1
40
0,4
10
0.2
20
0.1
optimal
40
0.4
optimal
10
10
cost=0.2 1+0,3.2=0.8
E.G.M. Petrakis
20
20
cost=0.2+0.1 2=0.4
10
1
range 20-40
20
10
cost=0.1+0.2 2=0.5
40
cost=0.1+0.8=0.9
40
optimal
20
cost=0.4+0.2=0.6
47
k=1-20
range 1-20
20
1
10
cost=0.1+2 0.3+3 0.2=1.3
10
20
1
20
cost=0.2+2(0.3+0.1)=1
1
10
40
cost=0.1+2(0.2+0.4)=1.3
optimal
10
10
20
20
cost=0.4+2 0.2+30.1=1.1
40
48
10
20
10
1
40
20
cost=0.2+2(0.3+0.4)+3 0.1=1.9
OPTIMAL BST
20
40
cost=0.1+2(0.3+0.4)+3 0.2=2.1
10
40
1
E.G.M. Petrakis10
20
49
Complexity
Compute all optimal BSTs for all Cij, i,j=1,2..n
Let m=j-i: number of keys in range Cij
n-m-1 Cijs must be computed
The one with the minimum cost must be
found, this takes O(m(n-m-1)) time
2
3
(nm
m
)
O(n
)
For all Cijs it takes
1 m n
There is a better O(n2) algorithm by Knuth
There is also an O(n) greedy algorithm
E.G.M. Petrakis
50
Optimal BSTs
High probability keys should be near
the root
But, the value of a key is also a factor
It may not be desirable to put the
smallest or largest key close to the
root => this may result in skinny trees
(e.g., lists)
E.G.M. Petrakis
51