You are on page 1of 12

Introduction to SML

AVL Trees
Michael R. Hansen
mrh@imm.dtu.dk
Informatics and Mathematical Modelling
Technical University of Denmark
c Michael R. Hansen, Fall 2004 p.1/10
Binary Search Trees
a
a
a
a
a
a
a
!
!
!
!
!
!
!
"
"
"
"
"
b
b
b
b
b

\
\
\
"
"
"
"
"
b
b
b
b
b
%
%
%
\
\
\
%
%
%
\
\
\
Br
Br Br
Br Lf Br Br
Lf 2 Lf
7
Lf 13 Lf Lf 25 Lf
21
9
Condition: for every node containing the value x: every value in the
left subtree is smaller then x, and every value in the right subtree is
greater than x.
c Michael R. Hansen, Fall 2004 p.2/10
SML Declaration of Binary Trees
datatype tree = Lf | Br of tree*int*tree;
The tree from the previous slide is denoted by:
Br(Br(Br(Lf,2,Lf),7,Lf),
9,
Br(Br(Lf,13,Lf),21,Br(Lf,25,Lf)))
The function
fun member(i, Lf) = false
| member(i, Br(t1,j,t2)) =
case Int.compare(i,j) of
EQUAL => true
| LESS => member(i,t1)
| GREATER => member(i,t2)
> val member = fn : int * tree -> bool
has a linear worst-case running time
c Michael R. Hansen, Fall 2004 p.3/10
Balanced binary search trees: AVL trees
An AVL tree (after Adelson-Velskii and Landis) is a search tree
satisfying the following balancing condition:

for every branching node: the difference of the heights of the left
and right subtrees should not be greater than 1.
AVL trees are generated from the empty tree Lf using insertion and
deletion operations.
Insertion and deletion must preserve balancing condition
c Michael R. Hansen, Fall 2004 p.4/10
Balanced binary search trees: AVL trees
An AVL tree (after Adelson-Velskii and Landis) is a search tree
satisfying the following balancing condition:

for every branching node: the difference of the heights of the left
and right subtrees should not be greater than 1.
AVL trees are generated from the empty tree Lf using insertion and
deletion operations.
Insertion and deletion must preserve balancing condition
c Michael R. Hansen, Fall 2004 p.4/10
Balanced binary search trees: AVL trees
An AVL tree (after Adelson-Velskii and Landis) is a search tree
satisfying the following balancing condition:

for every branching node: the difference of the heights of the left
and right subtrees should not be greater than 1.
AVL trees are generated from the empty tree Lf using insertion and
deletion operations.
Insertion and deletion must preserve balancing condition
c Michael R. Hansen, Fall 2004 p.4/10
Auxiliary functions
The height of a tree
fun height Lf = 0
| height(Br(lt, _, rt)) =
1 + Int.max(height lt, height rt);
is computed in linear time.
A function to test whether a search tree is an AVL tree:
fun isAVL Lf = true
| isAVL(Br(tl, v, tr)) =
isAVL tl andalso isAVL tr
andalso abs(height tl - height tr) <= 1;
c Michael R. Hansen, Fall 2004 p.5/10
Rotate from Left to Right: 1
v
vl
llt
rt
vl
llt
v
lrt rt
rotLeft
lrt
Preserves the search tree condition
c Michael R. Hansen, Fall 2004 p.6/10
Rotate from Left to Right: 2
Taking heights into account:
n
lrt n+1
llt
vl
v
rt
n
vl
n+1
llt
v
n+1
lrt
n
rt
n
rotLeft
Establishes AVL condition in some cases
fun rotLeft(Br(Br(llt, vl, lrt), v, rt))
= Br(llt, vl, Br(lrt, v, rt));
c Michael R. Hansen, Fall 2004 p.7/10
Double Rotatation from Left to Right
vl
v
rt
n
llt
vlr
lrlt
lrrt
n
n+1
n, n1
n, n1
n+1
n+1
vlr
vl
v
llt lrlt lrrt
rt
dRotLeftRight
In SML
fun dRotLeftRight(Br(Br(llt,vl,Br(lrlt,vlr,lrrt)),v,rt))
= Br(Br(llt,vl,lrlt),vlr,Br(lrrt,v,rt))
c Michael R. Hansen, Fall 2004 p.8/10
Insertion in AVL Trees
fun insert(i, Lf) = Br(Lf, i, Lf)
| insert(i, t as Br(lt, v, rt)) =
case Int.compare(i, v) of
LESS =>
let val nlt as Br(_,nvl,_) = insert(i,lt)
val nt = Br(nlt, v, rt)
in if height nlt - height rt = 2
then if i < nvl then rotLeft nt
else dRotLeftRight nt
else nt
end
| EQUAL => t
| GREATER => ... SIMILAR ...
Property: If t is an AVL tree, then so is insert(i, t)
Worst-case running time?
c Michael R. Hansen, Fall 2004 p.9/10
Height information in nodes
AVL trees with height information in nodes
datatype tree = Lf | Br of tree * int * int *tree;
Computation of height in constant time
fun height Lf = 0
| height(Br(_, _, h, _)) = h;
Rotations must maintain height information, e.g.
fun rotLeft(Br(Br(llt, vl, h, lrt), v, _, rt)) =
Br(llt, vl, h , Br(lrt, v, h-1, rt));
Insertion and deletions with logarithmic worst-case running time.
Programs on the course homepage.
c Michael R. Hansen, Fall 2004 p.10/10

You might also like