# AVL tree self-adjusting tree - PowerPoint PPT Presentation

1 / 47

AVL tree self-adjusting tree. Lai Ah Fur. AVL tree. discovers: A delson- V elskii and L andis balanced Binary search tree the depth of the tree: O(lg N)

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Lai Ah Fur

### AVL tree

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

2

0

78

78

63

1

1

63

95

63

95

51

78

1

Right rotate

51

73

51

73

73

95

31

31

LL型

-2

78

78

-1

95

63

95

63

95

78

105

1

105

83

105

83

63

83

100

100

Left rotate

RR型

78

63

66

2

0

66

-1

0

0

63

78

0

78

78

70

70

95

63

95

63

78

90

63

73

90

73

95

51

70

51

64

51

64

36

90

55

65

36

55

64

73

65

36

55

65

2

0

-1

0

-1

1

-1

1

-1

0

0

0

70

78

78

63

78

63

95

70

95

51

65

73

95

51

70

90

63

73

90

36

55

75

90

36

55

65

73

75

51

65

75

36

55

2

-1

1

-1

-1

78

78

90

96

90

78

96

90

96

-2

1

0

83

78

78

78

95

63

95

63

83

105

63

85

79

95

51

79

105

51

83

100

200

51

80

80

85

105

79

85

100

200

100

200

80

-2

1

1

-1

83

78

78

95

63

95

105

105

63

85

79

51

83

88

100

200

79

85

100

200

51

88

-2

0

1

0

1

-1

0

-1

1

-1

5

5

5

5

2

2

8

8

2

2

8

8

10

10

1

1

3

3

10

10

1

1

7

7

3

3

5

9

9

11

11

9

9

11

11

2

10

11

1

8

3

9

1.刪除7

RR

Del 7

5

Del 7

RL

2

9

10

1

8

3

11

3

5

2

10

2

10

11

1

8

11

1

8

3

9

9

8

2

10

11

1

9

3

2.刪除5

Del 5

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

• staticBinaryNode withLeftChild (BinaryNode k2)

• {

BinaryNode k1=k2.left;

k2.left=k1.right;

k1.right=k2;

return k1;

}

K1

single rotation

K2

A

B

C

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

+2

K2

0

+1

K1

0

C

B

A

12

12

k2

k1

8

16

4

16

K2

k1

14

A

8

14

4

c

10

2

10

6

2

6

1

A

B

B

C

1

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

}

K2

K3

K1

A

B

C

D

+2

0

K3

K3

K1

K2

-1

D

D

C

K2

K1

A

B

C

B

A

12

12

k3

K2

8

16

6

16

k1

14

8

14

4

D

10

k1

4

K3

k2

10

5

2

6

2

A

D

B

C

A

5

B

C

Insert “5”

### 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);

}

K2

K3

K1

A

B

C

D

0

-2

K1

K1

K2

K3

+1

A

A

K2

K3

B

D

B

C

D

C

80

90

90

70

80

95

85

95

99

92

70

85

99

92

-2

80

+1

0

95

70

99

90

+1/-1

85

92

Insert 85 or 92

### case 4: single rotation rightsubtree較高

• rotate binary tree with right child

• static BinaryNode withRightChild (BinaryNode k1)

{

BinaryNode k2=k1.right;

k1.right=k2.left;

k2.left=k1;

return k2;

}

single rotation

-2

K1

K2

0

A

K1

K2

-1

C

B

A

B

C

-1

80

0

-1

95

70

0

60

50

Insert 50

-1

80

0

0

95

60

0

0

50

70

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

-2

1

2

3-1

+2

Mar

May

May

-1

-1

+2

May

Aug

Nov

Mar

Nov

Nov

+1

Apr

Mar

Aug

+1

Insert Mar, May, Nov

Insert Aug, Apr

Jan

Apr

Insert Jan

Mar

4-1

3-2

May

-2

May

Aug

Mar

Nov

Nov

Apr

Jan

+1

Aug

Dec

July

-1

Insert Dec, July, Feb

Apr

Jan

Feb

Mar

4-2

5-1

+2

Mar

May

Aug

-1

May

Dec

Nov

Apr

Dec

Aug

-1

Nov

Jan

Jan

Apr

Feb

July

-1

Feb

July

Insert June

June

5-2

6

Mar

Jan

Jan

May

Dec

Mar

-2

Dec

July

Nov

Aug

May

Feb

July

Aug

Feb

June

-1

Nov

Apr

June

Apr

Oct

Insert Oct

7

Jan

Dec

Mar

Aug

Feb

July

Nov

Apr

June

May

Oct

Sept

Insert Sept

-1

Jan

+1

-1

Dec

Mar

-1

+1

0

-1

Aug

Feb

July

Nov

0

0

0

-1

Apr

June

May

Oct

0

Sept

60

40

80

30

70

90

65

75

Double R-L

rotations

80

40

85

30

70

90

65

75

Double L-R

rotations

80

40

85

90

Single R-R

rotations

80

40

85

20

Single L-L

rotations

80

40

85

30

50

Single L-L

rotations

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

• 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

Accessing node R

### Examples of splaying

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

and (c-d) then R

### 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

else if R is in heterogeneous configuration with its predecessor

perform a heterogeneous splay, first rotate R about Q

### 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