§4 AVL Trees
Target : Speed up searching (with insertion and deletion)
Tool : Binary search trees
root
small large
Problem : Although Tp = O( height ), but the height can be as bad as O( N ).
CHAPTER 4
Trees
1/19
§4 AVL Trees
〖 Example〗 2 binary search trees obtained for the months of the year
Nov
Oct
Sept
May
Mar
June
July
Dec
Aug
Apr
Feb
Jan
July
June
Mar
May
Oct
SeptNov
Jan
Feb
Aug
Apr Dec
Entered from Jan to Dec
A balanced tree
Discussion 1:Discussion 1: What are the aWhat are the average search tiverage search times of these twmes of these two trees?o trees?What if the moWhat if the months are enterenths are entered in alphabeticd in alphabetical order?al order?
2/19
§4 AVL Trees
Adelson-Velskii-Landis (AVL) Trees (1962)Adelson-Velskii-Landis (AVL) Trees (1962)【 Definition 】 An empty binary tree is height balanced. If T is a
nonempty binary tree with TL and TR as its left and right subtree
s, then T is height balanced iff
(1) TL and TR are height balanced, and
(2) | hL hR | 1 where hL and hR are the heights of TL and TR , respectively.
【 Definition 】 The balance factor BF( node ) = hL hR . In an
AVL tree, BF( node ) = 1, 0, or 1.5
82
1 4
3
7
7
82
1 4
3 5
4
5
6
3
2
1 7
The height of an empty tree is defined to be –1.
3/19
§4 AVL Trees
〖 Example〗 Input the months Mar
Mar01
May
May0
Nov
Nov0
1
2
May01
Nov00
21
Mar00
0
The trouble maker Nov is in the right subtree’s right subtree of the trouble finder Mar. Hence it is called an RR rotation.
In general:
A1
B0
BL BR
AL
RR
InsertionRR
Rotation
A2
B1
BL BR
AL
BL
B0
A
AL
BR
0
A is not necessarily the root of the tree
Single rotation
4/19
§4 AVL Trees
Aug
May01
Nov00
21
Mar01
1
Aug0
Apr
Apr0
1
2
2
5/19
Discussion 2:Discussion 2: What can we do What can we do now?now?
§4 AVL Trees
May01
Nov00
21
Aug01
1
1
Mar0
0
Apr0
Jan
Jan0
1
1
2
LR
Rotation
Mar01
May01
21
Aug01
0
1
Jan0
0
Apr0
Nov0
In general:
A1
B0
BL
ARC0
CRCL
LR
InsertionA2
B1
BL
ARC1
CRCL
OR
LR
Rotation
BL AR
C0
A1 or 0
CR
B0 or 1
CL
OR
Double Rotation
6/19
§4 AVL Trees
Dec July
Mar01
May01
21
Aug01
1
1
Jan0
Apr0
Nov0
July0
Dec0
Feb
Feb0
1
1
2
2
Discussion 3:Discussion 3: What can we do What can we do now?now?
7/19
§4 AVL Trees
July0
Dec01
Aug01
21
Jan01
0
1
Feb0
0
Apr0
Mar01
May01
211
Nov0
June Oct Sept
June0
1
1
1
2
Nov0
Dec01
Aug1
21
Feb0
1
July1
Apr0
Mar0
May1
June0
Jan0
Oct0
1
2
1
1
Oct0
Dec01
Aug1
21
Feb0
1
July1
Apr0
Mar0
Nov0
June0
Jan0
May0
Sept0
1
1
1
1
Note: Several bf’s might be changed even if
we don’t need to reconstructthe tree.
Another option is to keep a height field for each node.
8/19
Read the declaration and functions in Figures 4.42 – 4.48
Home work:
p.136 4.16 Create an AVL Tree
p.136 4.22
Double Rotation
§4 AVL Trees
One last question: Obviously we have Tp = O( h )
where h is the height of the tree.But h = ?
Let nh be the minimum number of nodes in a height balanced tree of height h. Then the tree must look like
A
h2 h1
A
h2h1OR nh = nh1 + nh2 + 1
Fibonacci numbers: F0 = 0, F1 = 1, Fi = Fi1 + Fi2 for i > 1
nh = Fh+2 1, for h 0
Fibonacci number theory gives thati
iF
2
51
5
1
)(ln12
51
5
12
nOhn
h
h
9/19
§5 Splay Trees
Target : Any M consecutive tree operations starting from an empty tree take at most O(M log N) time.
Does it mean that every operation takes O(log N) time?No. It means that the
amortized time is O(log N).
So a single operation might still take O(N) time? Then what’s the point?
The bound is weaker.But the effect is the same:
There are no bad input sequences.
But if one node takes O(N) time to access, we can keep accessing it
for M times, can’t we?
Sure we can – that only means that whenever a node is accessed,
it must be moved.
Discussion 4:Discussion 4: How shall we move the How shall we move the node?node?
10/19
k5
Fk4
Ek3
Dk2
Ak1
CB
§5 Splay Trees
k5
Fk4
Ek3
D
k2
BA
k1
C
k5
Fk4
E
k2
BA
k1
k3
DC
k5
F
k4
E
k2
BA
k1
k3
DC
k4
E
k5
F
k2
BA
k1
k3
DC
Does NOT work!
11/19
§5 Splay Trees
An even worse case:
Discussion 5:Discussion 5: Try to Try to InsertInsert 1, 2, ..., 1, 2, ..., NN in increasing order, and in increasing order, and then then FindFind them in the same order. What will them in the same order. What will happen? What is the total access time happen? What is the total access time TT((NN)?)?
12/19
§5 Splay Trees
Try again -- For any nonroot node X , denote its parent by P and grandparent by G :
Case 1: P is the root Rotate X and P
Case 2: P is not the root
Zig-zag G
DP
AX
B C
X
G
C D
P
A B
Double rotation
Zig-zig G
DP
CX
A B
Single rotation
X
AP
BG
DC
13/19
§5 Splay Trees
k5
Fk4
Ek3
Dk2
Ak1
CB
k5
Fk4
Ek1
k3
DC
k2
BA
k1
k2
BA
k4
k5
FE
k3
DC
Splaying not only moves the accessed node to the root, but also roughly
halves the depth of most nodes on the path.
14/19
§5 Splay Trees
Insert: 1, 2, 3, 4, 5, 6, 7
7
1
6
5
4
3
2
7
6
5
4
1
3
2
Find: 1
7
6
1
4
5
3
2
1
6
74
5
3
2
Read the 32-node example
given in Figures 4.52 – 4.60
15/19
Discussion 6:Discussion 6: Then what must we do? Then what must we do? Please complete the algorithm description for Please complete the algorithm description for DeletionDeletion..
§5 Splay Trees
Deletions:
Step 1: Find X ;
Are splay trees really better than AVL trees?
Home work:
p.136 4.23 Access a splay tree
16/19
Other Operations on Binary Search Trees
Sort: List the elements in increasing order
Solution: inorder traversal.
Get Height: Compute the heights of the nodes
Solution: postorder traversal.
Get Depth: Compute the depths of the nodes
Solution: preorder traversal.
17/19
Research Project 1Binary Search Trees (25)
This project requires you to implement operations on unbalanced binary search trees, AVL trees, and splay trees. You are to analyze and compare the performances of a sequence of insertions and deletions on these search tree structures.
Detailed requirements can be downloaded fromhttp://acm.zju.edu.cn/dsaa/
18/19
Research Project 2Population (25)
It is always exciting to see people settling in a new continent. As the head of the population management office, you are supposed to know, at any time, how people are distributed in this continent. The continent is divided into square regions, each has a center with integer coordinates (x, y). Hence all the people coming into that region are considered to be settled at the center position. Given the positions of the corners of a rectangle region, you are supposed to count the number of people living in that region. Note: there are up to 32768 different regions and possibly even more queries.
Detailed requirements can be downloaded fromhttp://acm.zju.edu.cn/dsaa/
19/19
Top Related