Upload
others
View
15
Download
0
Embed Size (px)
Citation preview
Data Structures and Algorithms(12)
Instructor: Ming ZhangTextbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao
Higher Education Press, 2008.6 (the "Eleventh Five-Year" national planning textbook)
https://courses.edx.org/courses/PekingX/04830050x/2T2014/
Ming Zhang "Data Structures and Algorithms"
2
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Chapter 12 Advanced data structure
• 12.1 Multidimensional Array
• 12.2 Generalized Lists
• 12.3 Storage management
• 12.4 Trie
• 12.5 Improved binary search tree
– 12.5.1 Balanced binary search tree
• Concept and insertion operation of AVL tree
• Deletion operation and efficiency analysis of
AVL tree
– 12.5.2 Splay Tree
3
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Deletion in an AVL Tree
• Deletion is the reverse operation of
insertion. Deletion in an AVL tree is
almost the same with the deletion of
BST.
• Deletion in an AVL tree is a little bit
complicated.
12.5 Improved binary search tree
4
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Adjustment after Deletion
• Conditions of adjustment
– Height and balance factor has changed.
– Adjust these changes from the current node to the
root
• Balance factor needs to be modified
– Modify it
• If modification is not needed then stop the adjustment
– Use bool variable modified to mark, initialize it with
TRUE
– when modified = FALSE, stop the adjustment.
12.5 Improved binary search tree
5
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Case 1
• Balance factor of the current node a is 0
– Its left or right subtree is cut, balance factor is 1 or -1
– modified = FALSE
– Modification will not influent the nodes above it. So
the adjustment is over.
12.5 Improved binary search tree
0
hh
delete
1
hh-1
6
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Case 2
• Balance factor of the current node is not 0, but the taller
subtree is cut.
– Modify its balance factor to 0
– modified = TRUE
– Keep modifying upward
12.5 Improved binary search tree
-1
hh+1
0
hhHeight
reduced
by1
delete
7
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Case 3.1
• Balance factor of the current node a is not zero. And the
shorter subtree is cut. So node a isn’t balanced.
• Assume the root of the taller subtree is b. There are three
cases.
• case 3.1:balance factor of b is zero
• Single rotation
• modified = FALSE
12.5 Improved binary search tree
1
h
h
h
0
zag
1
hh-1
h
-1a
ba
b
delete
8
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Case 3.2
• Case 3.2: balance factor of b is equal to that of
a.
– Single rotation
– Balance factors of a and b change to zero
– modified =TRUE
12.5 Improved binary search tree
1
h-1 h
1 0
h-1h-1
h
0
zagb
ba
a
h
delete
9
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Case 3.3
• Case 3.3: balance factor of b is opposite to that of a
– Double rotation, rotate b and c, then rotate c and a
– Because of balance factor of the new root is 0
– Other nodes need adjustment
– Besides modified = TRUE
12.5 Improved binary search tree
1
h
h-1
-1
h-2
/h-1
Double
rotation
Height
reduced
by one
h-1 h-1
h-1 or h-2
0
h-1
/h-2
a
a
b
b
c
c
delete
10
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Series of adjustments after deletion• Series of adjustments
– Ancestor nodes may be unbalanced after the
adjustment
– Keep adjusting. These adjustments might
pass to the root.
• Find the grandfather of the node
deleted just now.
– Start single rotation or double rotation
– Rotation times is O(log n)
12.5 Improved binary search tree
11
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Examples of deletion
12.5 Improved binary search tree
-1
01
-10 0 0
0 00 0 -1 0
0
i
d m
b g k n
a c f h j l
e
-1
-11
-10 -1 0
00 0 -1 0
0
i
d l
b g k n
a c fh j
e
(a) Delete node m, replace m with node I (case 1)
12
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Examples of deletion
12.5 Improved binary search tree
-1
-11
-10 -1 0
00 0 -1 0
0
i
d l
b g k n
a c f h j
e
(b) delete n (Case 3.2)
-1
-21
-10 -1
00 0 -1 0
0
i
dl
b g k
a c f h j
e
(c) Regard l as the root, doing LL
single rotation (Case 3.2)
13
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Examples of deletion
12.5 Improved binary search tree
-2
0
1
-10
0
0
0 0 -1 0
0
i
d
lb g
k
a c
f h
j
e
(d) After LL single rotation, then
adjust its father node I, doing LR
double rotation (Case 3.3)
0
0
0
0
0
0 -1
0 0 0
g
k
d
h
l
b f
a cj
e
0
1
i
(e) Adjustment is done, AVL tree
become balanced
14
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Height of AVL tree
• Height of AVL tree with n nodes must be
O(log n)
• The maximum height of AVL tree with n
nodes is no more than K log2n
– K is a small factor
– AVL tree that is the most close to
unbalanced
– Construct a series of AVL tree T1, T
2, T
3,…
12.5 Improved binary search tree
15
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
T1
T3
T2
T4
Ti-1Ti-2
Ti
Ti ‘s height is i
Ti is the most unbalanced AVL tree. Deleting
any node will cause it to be unbalanced.
T1
Any other AVL
trees with height I
have more nodes
than Ti
12.5 Improved binary search tree
16
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Proof of height
• We can see that:
t(1) = 2
t(2) = 4
t(i) = t(i-1) + t(i-2) + 1
(t(i)+1) = (t(i-1)+1) + (t(i-2)+1)
• As for i>2 these relations are very similar to
Fibonacci numbers:
F(0) = 0
F(1) = 1
F(i) = F(i-1) + F(i-2)
12.5 Improved binary search tree
17
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Proof of height
• As for i>l, we have
t (i) = F (i+3) - 1
• Fibonacci number has this formula
• So the approximate formula is
12.5 Improved binary search tree
2
51 here ,
5
1)(
iiF
15
1)i(t 3i
18
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Proof of height (result)
• Relation between height I and the number of nodes t(i)
• Use formula logφX = log2X / log
2φ and log
2φ ≈ 0.694 ,
calculate the approximate upper bound.
– t (i) = n
– So height of AVL tree with n nodes must be O (log n)
12.5 Improved binary search tree
)1)i(t(53i
)1)i(t(log5log3i
2
3log (n 1) 1
2i
19
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Efficiency of AVL tree
• Time cost of searching, inserting and
deleting is O(1og2
n)
– Height of AVL tree with n nodes must be
O(log n)
• AVL tree is fit to data of small scale in
memory
• For large scale data stored in external
memory
– B tree or B+ tree
12.5 Improved binary search tree
20
目录页
Ming Zhang “Data Structures and Algorithms"
Chapter 12
Advanced Data
Structure
Discussion
• Compare AVL tree with black-red tree,
which is better?
– Height of tree in the worst case
– Efficiency of operation in statistics
– Which one is easier to implement?
12.5 Improved binary search tree
Data Structures and Algorithms
Thanks
the National Elaborate Course (Only available for IPs in China)http://www.jpk.pku.edu.cn/pkujpk/course/sjjg/
Ming Zhang, Tengjiao Wang and Haiyan ZhaoHigher Education Press, 2008.6 (awarded as the "Eleventh Five-Year" national planning textbook)
Ming Zhang “Data Structures and Algorithms”