AVL tree self-adjusting tree Lai Ah Fur. AVL tree discovers: Adelson-Velskii and Landis balanced...
-
Upload
clyde-sherman -
Category
Documents
-
view
228 -
download
0
description
Transcript of AVL tree self-adjusting tree Lai Ah Fur. AVL tree discovers: Adelson-Velskii and Landis balanced...
AVL tree self-adjusting tree
Lai Ah Fur
AVL tree discovers: Adelson-Velskii and Landis balanced Binary search tree the depth of the tree: O(lg N) definition: An AVL tree is a binary search
tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can be differ by at most 1.
Balance factor (BF) 如果 node T 之 the height of left subtree
is Hl, the height of right subtree is Hr, then the BF of node T is Hl- Hr
高度平衡之 BST: BF(t)<2 …for any node t AVL tree: BF(t)<2 Full binary tree: BF=? complete binary tree: BF=?
51 73
9563
78
31
51 73
9563
78
31
51 78
95
63
73
2
1
1
0
1
Right rotate
LL 型
10583
9563
78
10583
9563
78
100
-2
-1
1 10578
95
63 83 100
Left rotate
RR 型
插入 6678
63
66
63
66
78
2
-1
0
0
0 0
第一種 LR 型
插入 65
6455 7336
51 70 90
9563
78
6555
64
36
51
63 78
90
95
70
73
6555
64
36
51
63 73 90
9570
78
65
第二種 LR 型
2
-1
1
-1 0
0
0
-1
1-1
0
0
插入 7578
63 95
90
55 73
70
65
51
36
75
78
70 95
90
65
55
73
36
63
51 75
70
63 78
73
55
9565
90
51
36 75
第三種 LR 型
2
-1
-1
-1
1
插入 90
96
78
90 96
78
90
90
78 96
第一種 RL 型
-2
1
0
插入 80
51
6378
95
85
80
100
83 105
79 200
51
6378
83
200
80
100
79 95
85 105
63
78
83
95
51 80 100
85 10579
200
第二種 RL 型
-2
1
1
-1
插入 88
51
6378
95
85
88
100
83 105
79 20063
7883
95
8851 100
85 10579
200
第三種 RL 型
-2
1
-1
-1
0
01
-110
Del 7
1
2
5
8
9
7 103
11
1
2
5
8
3
9
10
11
RR
1
2
5
10
8 113
9
1
2
5
8
9
7 103
11
Del 71
2
5
8
3
9
10
11
RL
1
2
5
9
8 10311
1. 刪除 7
或是
1
2
5
10
8 113
9
2. 刪除 5
Del 5
1
2
3
10
8 11
9
1
2
8
10
9 113
RL
Insertion
case 1: an insertion into the left subtree of the left child of X
case 2: an insertion into the right subtree of the left child of X
case 3: an insertion into the left subtree of the right child of X
case 4: an insertion into the right subtree of the right child of X
case 1: single rotation left subtree 較高
.A single rotation switches the role of the parent and the child while maintaining the search order
.rotate binary tree with left child static BinaryNode withLeftChild (BinaryNode k2) { BinaryNode k1=k2.left; k2.left=k1.right; k1.right=k2; return k1; }
K2
K1
CB
A
K2
K1
CBA
case 1 (single L-L rotation, 單一左左迴轉 )
single rotation
+2
+1
0
0
case 1
128 16
1410462
1
12
816
1410
4
62
1
k2
k1
BA
c A
k1K2
B C
Insert “1”
case 2: double rotation (double L-R rotation)
double rotate binary tree node: first left child with its right child; then, node k3 with new left child.
static BinaryNode doubleWithLeftChild(BinaryNode k3) { k3.left=withRightChild(k3.left); return withLeftChild(k3); }
case 2: double rotationK3
K1
D
AB
K2
C
K1
DC
K3
K2
A B
K3
K2
D
A B
K1 C
+2
-1
0
case 2: double rotation
128 16
1410462
5
12
816
1410
6
54
2k2
k1
B
A
D
A
k1
K2
B D
Insert “5”
k3
CC
K3
case 3:double rotation (double R-L rotation)
double rotate binary tree node: first right child with its left child; then, node k1 with new right child.
static BinaryNode doubleWithRightChild(BinaryNode k1){ k1.right=withLeftChild(k1.right); return withRightChild(k1);}
case 3
K3
K1
DB
K2
C
K1
DC
K3
K2
A B
K1
K2
D
B
C
A K3A
-2
+1
0
R-L type
Insert 85 or 92
-280
9570
85
+1
+1/-1
0
90 99
92
80
907085 95
92 99
80
90
70 85
95
92 99
case 4: single rotation right subtree 較高
rotate binary tree with right child static BinaryNode withRightChild (BinaryNode k1){ BinaryNode k2=k1.right; k1.right=k2.left; k2.left=k1; return k2;}
case 4 (single R-R rotation, 單一右右迴轉 )
K2
K1
CB
A
K2
K1
CBA
single rotation
-2
-1
0
R-R type
80
9560
50 70
80
9570
60
50Insert 50
-10-1
0
-1
00
0 0
exercise Insert the following data into the
empty AVL tree, 90 80 70 60 50 40
exercise Insert the following data into the
empty AVL tree, “Mar,May,Nov,Aug,Apr,Jane,Dec,July,Feb,June,Oct,Sept”
3-1May
Nov
Apr Mar+1
-1
Jan
+2
Insert Jan
Aug
2
May
Nov
Aug
Apr
+1
+2
Insert Aug, Apr
Mar
1
Mar
Nov
-1
-2
Insert Mar, May, Nov
May
3-2
May
Nov
Apr Jan
Mar
Aug
4-1
May
Nov
Aug
Apr
Mar
July
Feb
Jan
Dec-1
+1
-2
Insert Dec, July, Feb
4-2
May
Nov
Aug
Apr
Mar
July
Dec
Jan
Feb
5-1
May
Nov
Apr
Mar
July
Dec
Jan
Feb
Aug
June
-1
-1
-1
+2
Insert June
5-2
May
Nov
Apr
Mar
JulyDec
Jan
FebAug June
6
May
NovApr
July
Dec
FebAug
June
Jan
Mar
OctInsert Oct
-1
-2
The Answer:7
Apr
Dec
FebAug
June
Jan
Insert Sept
July
May
Nov
Mar
Sept
Oct
JanDec Mar
Aug Feb July Nov
Apr June May Oct
Sept
-1+1
-1 -1
-1
0
+1 0
0 00
-1
After delete 40,…60
4030
80
70 90
65 75
Double R-L rotations
After delete 85,…80
4030
85
70 90
65 75
Double L-Rrotations
After delete 40,…80
40 85
90Single R-Rrotations
After delete 85,…80
40 85
20Single L-Lrotations
After delete 85,…
Single L-Lrotations
8040 85
30 50
How a newly arriving element enters… If a newly arriving element
endangers the tree balance, how to rectify the problem immediately?
By restructuring the tree locally (the AVL method) or by re-creating the tree (the DSW method)
self-adjusting tree But, not all elements are used with
the same frequency. 在低層之 node若 infrequently accessed, 則對於程式效能影響不大
The strategy in self-adjusting tree is to restructure trees only by moving up the tree those elements that are used more often, creating a kind of “priority tree”
Self-restructuring tree Proposed by Brian Allen and Ian
Munro and James Bitner Strategy:
Single rotation: rotate a child about its parent if an element in a child is accessed unless it is the root
Moving to the root: repeat the child-parent rotation until the element being accessed is in the root
splaying A modification of the “move to the root” Apply single rotation in pairs in an order
depending on the links between the child, parent and grandparent. (node R is accessed) Case 1:node R’s parent is the root Case 2:homogeneous configuration: node R is the
left child of its parent Q and Q is the left child of its parent P, or R and Q are both right children.
Case 3: heterogeneous configuration: node R the right child of its parent Q and Q is the left child of its parent P, or R is the left child of Q and Q is the right child of P
3 cases of splayingAccessing node R
Examples of splaying
Restructuring a tree with splaying (a-c) after accessing T and (c-d) then R
Algorithm of splayingSplaying(P,Q,R) while R is not the root if the R’s parent is the root perform a singular splay, rotate R about its parent; else if R is in homogeneous configuration with its predecessor perform a homoegeous splay, first rotate Q about P and then R about Q; else if R is in heterogeneous configuration with its predecessor perform a heterogeneous splay, first rotate R about Q and then about P;
The problem of splaying Splaying is a strategy focusing upon the
elements rather than the shape of the tree. It may perform well in situation in which some elements are used much more frequently than others
If the elements near the root are accessed with about the same frequency as elements on the lowest levels, then splaying may not be the best choice.
semisplaying A modification that requires only one
rotation for a homogeneous splay and continues splaying with the parent of the accessed node.
Example of semisplaying
(a)-(c) accessing T and restructuring the tree with semisplaying(c ) -(d)accessing T again