Self-Adjusting Tree

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 80

AVL tree

self-adjusting tree

Department of Computer Science and Information Engineering


National Taipei University
莊東穎教授
AVL tree
• discovers: Adelson-Velskii and Landis
• balanced Binary search tree
• the depth of the tree: O(log 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.

2
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=?

3
Binary Search Tree for The
Months of The Year

4
A Balanced Binary Search Tree For
The Months of The Year

5
Degenerate Binary Search Tree

6
Minimize The Search Time of Binary Search
Tree In Dynamic Situation
• From the above three examples, we know that the average and
maximum search time will be minimized if the binary search tree
is maintained as a complete binary search tree at all times
• However, to achieve this in a dynamic situation, we have to pay a
high price to restructure the tree to be a complete binary tree all
the time
• In 1962, Adelson-Velskii and Landis introduced a binary tree
structure that is balanced with respect to the heights of subtrees.
As a result of the balanced nature of this type of tree, dynamic
retrievals can be performed in O(log n) time if the tree has n
nodes. The resulting tree remains height-balanced. This is called
an AVL tree.

7
AVL Trees

• Definition: An empty tree is height-balanced. If T is a


nonempty binary tree with TL and TR as its left and right
subtrees respectively, 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(T) , of a node T is a
binary tree is defined to be Hl–Hr , where Hl and Hr,
respectively, are the heights of left and right subtrees of
T. For any node T in an AVL tree, BF(T) = -1, 0, or 1.

8
AVL Trees

• The addition of a node to a balanced binary search


tree could unbalance it
• The rebalancing was carried out using four different
kinds of rotations:
– LL: Y is inserted in the left subtree of the left subtree of A
– LR: Y is inserted in the right subtree of the left subtree of
A
– RR: Y is inserted in the right subtree of the right subtree of
A
– RL: Y is inserted in the left subtree of the right subtree of
A

9
AVL Trees
• These rotations are characterized by the
nearest ancestor, A, of the inserted node,
Y, whose balance factor becomes ±2.

10
AVL Trees

11
Rebalancing Rotation LL

12
Rebalancing Rotation RR

13
Rebalancing Rotation LR(a)

14
Rebalancing Rotation LR(b)

15
Balanced Trees Obtained for The
Months of The Year

16
Balanced Trees Obtained for The
Months of The Year (Cont.)

17
Balanced Trees Obtained for The
Months of The Year (Cont.)

18
Balanced Trees Obtained for The
Months of The Year (Cont.)

19
Balanced Trees Obtained for The
Months of The Year (Cont.)

20
Balanced Trees Obtained for The
Months of The Year (Cont.)

21
Balanced Trees Obtained for The
Months of The Year (Cont.)

22
AVL Trees
• Once rebalancing has been carried out on
the subtree in question, examining the
remaining tree is unnecessary
• To perform insertion, binary search tree
with n nodes could have O(n) in worst case.
But for AVL, the insertion time is O(log n)

23
Balance or Not Balance?

• Don't balance
– May end up with some nodes very deep
• Strict balance
– The tree must always be balanced perfectly
• Pretty good balance
– Only allow a little out of balance

24
Balancing Binary Search Trees
• Many algorithms exist for keeping binary
search trees balanced
– Adelson-Velskii and Landis (AVL) trees
(height-balanced trees)
– Red-black trees
– B-trees

25
Perfect Balance
• Want a complete tree after every operation
– Tree is nearly full except possibly in the lower
right
• This is expensive
– For example, insert 2 in the tree on the left and
then rebuild as a complete tree
6 5
Insert 2 &
4 9 complete tree 2 8

1 5 8 1 4 6 9

26
AVL Tree - Good but not Perfect
Balance
• AVL trees are height-balanced binary search
trees.
– Balance factor F of a node
– F = height(left subtree) - height(right subtree)
• An AVL tree has balance factor |F| ≦1.
– For every node, heights of left and right subtree
can differ by no more than 1.
– What is the height of an AVL tree with n nodes?

27
Minimum number of nodes
• 由於 AVL tree t 的定義 , 他的 subtrees 一定
要擁有高度為 h-1 或 h-2. ( 因為要滿足 AVL
tree 的條件且要最少 elements)
• 如果 t 在它的高度下有最少 elements 個數 ,
其中一棵 subtree 必須有高度為 h-1 與 minh-1
個 elements, 而另一棵 subtree 必須有高度為 h-
2 與 minh-2 個 elements.
As shown next page:
minh = minh-1+minh-2+1
28
28
h=2 minh=4 h=3 minh=7

50 90 h=3-2
min=2

39 80 50 100

39 80 129
60

h=3-1
60 min=4

29
29
Height of an AVL Tree
• N(h) = minimum number of nodes in an AVL
tree of height h.
• Let’s try
– N(0) = 1, N(1) = 2, N(2) = 4, …
• Induction
h
– N(h) = N(h-1) + N(h-2) + 1
• Solution (recall Fibonacci analysis) h-1 h-2

– N(h) > h (  1.62)

30
Height of an AVL Tree
• N(h) > h (  1.62)
• Suppose we have n nodes in an AVL tree of
height h.
– n > N(h) because N(h) was the minimum
– n > h hence log n > h (relatively well
balanced tree!!)
– h < O(log n)

31
接著 , 讓 maxh 為
在一棵高度為 h(h=0,1,2,…) 的 AVL tree

最多的 elements 個數

32
32
在一棵高度為 h(h=0,1,2,…) 的 AVL tree

最多的 elements 個數 maxh 相當於一個
binary tree 的最大值
50
maxh = 2 -1
h+1

39 80

30 45 60 100

33
33
Probability of Each Type of
Rebalancing Rotation
• Research has shown that a random insertion
requires no rebalancing, a rebalancing
rotation of type LL or RR, and a
rebalancing rotation of type LR and RL,
with probabilities 0.5349, 0.2327, and
0.2324, respectively.

34
Comparison of Various
Structures

1. Doubly linked list and position of x known.


2. Position for insertion known

35
AVL Tree Delete
• Complications arise from the fact that
deleting a node can unbalance a number of
its ancestors.
– insert only required you find the first
unbalanced node
– delete will require you to go all the way back to
the root looking for imbalances
• We must balance any node with a ±2 balance factor

36
Deletion from AVL Tree
• Delete a node x as in ordinary binary search
tree.
– But trace the path from the new leaf towards
the root
• For each node x encountered, check if
heights of left(x) and right(x) differ by at
most 1.
– If yes, proceed to parent(x).
– If no, perform an appropriate rotation at x
– Continue to trace the path until we reach the
root.
37
Deletion Example (Single Rotation)

20 20

10 35 15 35

5 15 25 40 10 18 25 40

18 30 38 45 30 38 45

50 50

Delete 5, Node 10 is unbalanced Single Rotation

38
Deletion Example (Single Rotation)

20 35

15 35 20 40

10 18 25 40 15 25 38 45

30 38 45 10 18 30 50

Continue to check parents 50


Single Rotation
Node 20 is unbalanced!!

For deletion, after rotation, we need to continue tracing upward to


see if AVL-tree property is violated at other node

39
Deletion Example (Double Rotation)

M
Q

J S delete(L) S
M

L Q U U
J N

Deleting a node in the left sub-tree (M’s balance becomes 2).


Need to rotate M with it’s right sub-tree.
S’s balance factor is 1 before rotate (need double-rotation).
Do a right rotate of Q with S and then a left rotate of Q with M.
40
Deletion Example (Double Rotation)

M
Q

J S delete(L) S
M

L Q U R U
J

Deleting a node in the left sub-tree (M’s balance becomes 2).


Need to rotate M with it’s right sub-tree.
S’s balance factor is 1 before rotate.
Need to do a right rotate of Q with S and then a left rotate of Q with M.
Notice that the height of the tree changes.
41
Deletion Example 2

Double rotation Replace with the predecessor

42
Deletion Example 2 Cont’d

How to rotate?

43
One New Case
• New case: two subtrees of y are of the same
height

rotate with left child

rotate with right child

44
Deletion Example 2 Cont’d

New case

45
LL

2
0
78 78
63
1
63 95 63 95
1
1 51 78
Right rotate
51 73 51 73
31 73 95
31

46
RR

-2
78 78
-1 95
63 95 63 95
1 Left rotate 78 105
83 105 83 105
63 83 100

100

47
插入 66

2
0
78
66
-1
63 0 0
63 78
0
66

第一種 LR 型

48
插入 65
2
0
78 78
-1 70
63 95 70 95 0 -1
1 63 78
51 70 90 63 73 90 -1 1
-1 0 51 64 73 95
36 55 64 73 51 64
0
0 73 64 65 90
65 73 64 65

第二種 LR 型

49
插入 75

2
78 78
-1 70
1
63 95 70 95
-1 63 78
51 70 90 63 73 90
-1 51 65 73 95
36 55 65 73 51 65 75
36 55 75 90
75 36 55

第三種 LR 型

50
插入 90

-2
78 78
90
+1
96 90
78 96
90 96

第一種 RL 型

51
插入 80

-2 83
78 78
+1 78 95
63 95 63 83
1 63 79 85 105
51 83 105 51 79 95
-1 51 80 100 200
79 85 100 200 80 85 105

80 100 200

第二種 RL 型

52
插入 88

-2
0
78
+1 83
0
63 95 1
78 95
-1 0
51 83 105 1 -1
63 79 85 105
-1
79 85 100 200
51 88 100 200
88

第三種 RL 型

53
1. 刪除 7 5
5
5
2 8 RR
Del 7 2 10
2 8 1 3 10
1 3 8 11
1 3 7 10
9 11 9
9 11

或是
5 5
5
2 8 Del 7 2 8 RL 2 9
1 3 7 10 1 3 10
1 3 8 10

9 11 9 11 11

54
2. 刪除 5

5 3
Del 5
2 10 2 10

1 3 8 11 1 8 11

9 9

8
RL 2 10

1 3 9 11

55
case 1 (single L-L rotation, 單一左左迴轉 )

+2
K2 K1 0

+1 K1
single rotation
K2 0

C
B A B C
A

56
case 1

12 12
k2
8 16 k1 4 16
k1 4 c 10 14 A 2 K2 8 14

6 10
A 2 6B 1
B C
1
Insert “1”

57
case 2: double rotation

+2 0
K3 K3 K2

K1 -1 K2
K1 K3

K2
D K1 C D

A A B C D
B C A B
58
case 2: double rotation

12 12
k3 8 16 K2 6 16
k1 4 D10 14 k1 4 K3 8 14

k2 5 10
A 2 6 2
A B C D
B 5 C

Insert “5”

59
case 3

0
K1 -2 K1 K2

K3 +1 K2
K1 K3

K2 K3
A A

D B A B C D
B C C D
60
R-L type

-2
80 80
0 +1 90
70 95 70 90
80 95
+1/-1 90 99 85 95
70 85 92 99
92 99
85 92

Insert 85 or 92

61
case 4 (single R-R rotation, 單一右右迴轉 )

K1 -2 K2 0

single rotation
-1 K2 K1

A
C
B A B
C

62
R-R type

-1 -1
80 80
-1 0 0 0
70 95 60 95

0 0 0
60 50 70

50
Insert 50

63
After delete 40,…

60
40 80
Double R-L
30 70 90 rotations

65 75

64
After delete 85,…

80
40 85 Double L-R
30 rotations
70 90

65 75

65
After delete 40,…

80
40 85 Single R-R
rotations
90

66
After delete 85,…

80
40 85 Single L-L
rotations
20

67
After delete 85,…

80
40 85 Single L-L
rotations
30 50

68
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)

69
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”

70
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

71
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
72
3 cases of splaying
Accessing node R

73
Examples of splaying

Restructuring a tree with splaying (a-c) after accessing T


and (c-d) then R
74
Algorithm of splaying
Splaying(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;

75
Rotations In A Splay Beginning At Shaded Node

1 1
a 9 a 9
8 j 8 j
2 2 i
i
b 7 b 7
6 h 6 h
3 g 5 g
c 4 4 f
d 5 3 e
e f c d

(a) Initial search tree (b) After RR rotation

76
Rotations In A Splay Beginning At Shaded Node (Cont.)

1
1
a 9
a 9
5 j
8 j
2 8
2 i
b 4 6 i
b 5
3 e f 7
4 6
3 ef 7 c d g h

c d g h

(c) After LL rotation (d) After LR rotation

77
Rotations In A Splay Beginning At Shaded Node (Cont.)

1 9

a 2 8 j
b 4 6 i

3 e f 7

c d g h

(e) After RL rotation

78
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.

79
Homework 2
• Insert the following data into the empty
AVL tree,
“Mar,May,Nov,Aug,Apr,Jane,Dec,July,Feb,
June,Oct,Sept”

80

You might also like